public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Michał Górny" <mgorny@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/gentoopm:master commit in: gentoopm/
Date: Thu, 21 Jul 2011 23:00:03 +0000 (UTC)	[thread overview]
Message-ID: <337953535fecf3ea86579537415bb598e6df2388.mgorny@gentoo> (raw)

commit:     337953535fecf3ea86579537415bb598e6df2388
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Thu Jul 21 22:24:41 2011 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Thu Jul 21 22:24:41 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/gentoopm.git;a=commit;h=33795353

Add an EnumTuple type.

---
 gentoopm/util.py |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/gentoopm/util.py b/gentoopm/util.py
index 17a8d81..3005d36 100644
--- a/gentoopm/util.py
+++ b/gentoopm/util.py
@@ -7,6 +7,7 @@
 Utility functions for gentoopm.
 """
 
+import collections
 from abc import ABCMeta
 
 try:
@@ -118,3 +119,37 @@ class SpaceSepTuple(tuple):
 
 	def __str__(self):
 		return ' '.join(self)
+
+def EnumTuple(name, *keys):
+	"""
+	Create a namedtuple factory for an enumerated type. The resulting factory
+	function shall be called with keyword argument with names resembling
+	enumerated value names and values evaluating to True or False.
+
+	>>> MyTestEnum = EnumTuple('MyTestEnum', 'bad', 'good')
+	>>> i = 4
+	>>> MyTestEnum(bad = i <= 3, good = i > 3)
+	MyTestEnum(bad=False, good=True)
+
+	@param name: name of the resulting namedtuple
+	@type name: string
+	@param keys: list of enumerated values
+	@type keys: strings
+	@return: Factory function creating namedtuples.
+	@rtype: func(**kwargs)
+	"""
+
+	def _check_args(kwargs):
+		res = False
+		for a in kwargs.values():
+			if not isinstance(a, bool):
+				raise ValueError('Non-bool passed to EnumTuple')
+			if a and res:
+				raise ValueError('More than a single True passed to EnumTuple')
+			res |= a
+		if not res:
+			raise ValueError('All values passed to EnumTuple are False')
+		return kwargs
+
+	nt = collections.namedtuple(name, keys)
+	return lambda **kwargs: nt(**_check_args(kwargs))



             reply	other threads:[~2011-07-21 23:00 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-21 23:00 Michał Górny [this message]
  -- strict thread matches above, loose matches on Subject: below --
2013-08-08 23:02 [gentoo-commits] proj/gentoopm:master commit in: gentoopm/ Michał Górny
2013-02-16 11:42 Michał Górny
2012-12-10 21:04 Michał Górny
2012-10-14 21:35 Michał Górny
2012-01-09 16:09 Michał Górny
2011-12-24 10:36 Michał Górny
2011-11-15 18:58 Michał Górny
2011-11-14 21:44 Michał Górny
2011-09-09 21:13 Michał Górny
2011-08-23 20:39 Michał Górny
2011-08-15 16:00 Michał Górny
2011-08-15  8:48 Michał Górny
2011-08-13 18:49 Michał Górny
2011-08-13 18:49 Michał Górny
2011-08-13 18:49 Michał Górny
2011-08-13 10:31 Michał Górny
2011-08-13 10:31 Michał Górny
2011-08-13 10:31 Michał Górny
2011-08-12 21:10 Michał Górny
2011-08-12 20:07 Michał Górny
2011-08-12  9:28 Michał Górny
2011-08-12  8:53 Michał Górny
2011-08-12  8:24 Michał Górny
2011-08-12  8:24 Michał Górny
2011-08-12  7:28 Michał Górny
2011-07-29  6:18 Michał Górny
2011-07-27  8:26 Michał Górny
2011-07-23  9:27 Michał Górny
2011-07-22 13:16 Michał Górny
2011-07-21 23:00 Michał Górny
2011-07-21 23:00 Michał Górny
2011-07-20 18:30 Michał Górny
2011-07-19 12:07 Michał Górny
2011-07-17 22:19 Michał Górny
2011-07-17 13:58 Michał Górny
2011-07-10 12:34 Michał Górny
2011-07-10 12:34 Michał Górny
2011-07-10 12:34 Michał Górny

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=337953535fecf3ea86579537415bb598e6df2388.mgorny@gentoo \
    --to=mgorny@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox