From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1Qk2E1-0006pa-82 for garchives@archives.gentoo.org; Thu, 21 Jul 2011 23:00:13 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id B00CC21C191; Thu, 21 Jul 2011 23:00:05 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 8161221C191 for ; Thu, 21 Jul 2011 23:00:05 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 06F8B1BC013 for ; Thu, 21 Jul 2011 23:00:05 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id CA7ED80042 for ; Thu, 21 Jul 2011 23:00:03 +0000 (UTC) From: "Michał Górny" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Michał Górny" Message-ID: <337953535fecf3ea86579537415bb598e6df2388.mgorny@gentoo> Subject: [gentoo-commits] proj/gentoopm:master commit in: gentoopm/ X-VCS-Repository: proj/gentoopm X-VCS-Files: gentoopm/util.py X-VCS-Directories: gentoopm/ X-VCS-Committer: mgorny X-VCS-Committer-Name: Michał Górny X-VCS-Revision: 337953535fecf3ea86579537415bb598e6df2388 Date: Thu, 21 Jul 2011 23:00:03 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 0f83264233fd6dc18d1383dd2ebaa5f6 commit: 337953535fecf3ea86579537415bb598e6df2388 Author: Micha=C5=82 G=C3=B3rny gentoo org> AuthorDate: Thu Jul 21 22:24:41 2011 +0000 Commit: Micha=C5=82 G=C3=B3rny gentoo org> CommitDate: Thu Jul 21 22:24:41 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/gentoopm.git;= a=3Dcommit;h=3D33795353 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. """ =20 +import collections from abc import ABCMeta =20 try: @@ -118,3 +119,37 @@ class SpaceSepTuple(tuple): =20 def __str__(self): return ' '.join(self) + +def EnumTuple(name, *keys): + """ + Create a namedtuple factory for an enumerated type. The resulting facto= ry + function shall be called with keyword argument with names resembling + enumerated value names and values evaluating to True or False. + + >>> MyTestEnum =3D EnumTuple('MyTestEnum', 'bad', 'good') + >>> i =3D 4 + >>> MyTestEnum(bad =3D i <=3D 3, good =3D i > 3) + MyTestEnum(bad=3DFalse, good=3DTrue) + + @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 =3D 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 |=3D a + if not res: + raise ValueError('All values passed to EnumTuple are False') + return kwargs + + nt =3D collections.namedtuple(name, keys) + return lambda **kwargs: nt(**_check_args(kwargs))