Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Ethan Furman Newsgroups: comp.lang.python.announce Subject: [ANN] aenum 1.3.0 released Date: Sun, 13 Mar 2016 00:20:41 -0800 Lines: 183 Approved: python-announce-list@python.org Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de ImxzyQyfpeE+T+lgR6OR1gjLjDQfuTgOp1yA3fmKhtPw== Return-Path: X-Original-To: python-announce-list@python.org Delivered-To: python-announce-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'api.': 0.04; 'subject:ANN': 0.07; 'subject:released': 0.07; 'blue': 0.09; 'constants.': 0.09; 'derived': 0.09; 'descriptor': 0.09; 'enum': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'namespace': 0.09; 'notation.': 0.09; 'protocols,': 0.09; 'specifying': 0.09; 'subclass': 0.09; 'subclasses': 0.09; 'tuple': 0.09; 'python': 0.10; '2.7': 0.13; 'value.': 0.15; 'variables': 0.15; '1.3.0': 0.16; '``class``': 0.16; 'assigns': 0.16; 'callable,': 0.16; 'constants': 0.16; 'constants:': 0.16; 'creation.': 0.16; 'exceptions.': 0.16; 'inserting': 0.16; 'iterated': 0.16; 'member)': 0.16; 'method::': 0.16; 'namedtuples': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subclassing': 0.16; 'write.': 0.16; '~~~~~~~~~': 0.16; '~~~~~~~~~~': 0.16; 'attribute': 0.18; 'duplicate': 0.18; 'helper': 0.18; 'restrictions': 0.18; 'skip:` 10': 0.18; 'transform': 0.18; 'subject:] ': 0.19; '>>>': 0.20; 'versions': 0.20; '(usually': 0.22; 'arguments': 0.22; 'constant': 0.22; 'decorator': 0.22; 'symbolic': 0.22; 'insert': 0.23; 'import': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'appear': 0.26; 'define': 0.27; 'member.': 0.27; 'specify': 0.27; 'function': 0.28; 'values': 0.28; '---': 0.28; 'cat': 0.29; 'docstrings': 0.29; 'extending': 0.29; 'unique,': 0.29; 'raise': 0.29; 'creating': 0.30; 'compared': 0.30; 'announce': 0.32; 'functional': 0.32; 'class': 0.33; 'common': 0.33; 'accessible': 0.33; 'skip:~ 10': 0.33; 'values.': 0.33; 'similar': 0.33; 'skip:- 10': 0.34; 'members.': 0.34; 'previous': 0.34; 'add': 0.34; 'but': 0.36; 'there': 0.36; 'created': 0.36; 'possible': 0.36; 'keyword': 0.36; 'method': 0.37; 'release': 0.37; 'itself': 0.38; 'green': 0.38; 'names': 0.38; 'skip:- 20': 0.39; 'subject:[': 0.39; 'skip:- 60': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'easy': 0.60; 'advanced': 0.61; 'default': 0.61; 'providing': 0.62; 'skip:n 10': 0.62; 'within': 0.64; '3.4': 0.84; 'enumeration': 0.84; 'enumeration,': 0.84; 'lacks': 0.84 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 X-Mailman-Approved-At: Sun, 13 Mar 2016 07:58:28 -0400 X-BeenThere: python-announce-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Announcement-only list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python.announce:2067 I am thrilled to announce the release of aenum 1.3.0 aenum --- advanced enumerations, named tuples, and named constants ================================================================== aenum includes the new Python stdlib enum module available in Python 3.4 backported for previous versions of Python from 2.7 and 3.3+ tested on 2.7, and 3.3+ An ``Enum`` is a set of symbolic names (members) bound to unique, constant values. Within an enumeration, the members can be compared by identity, and the enumeration itself can be iterated over. A ``NamedTuple`` is a class-based, fixed-length tuple with a name for each possible position accessible using attribute-access notation. A ``Constant`` is a class of named values whose members cannot be rebound; it lacks all other ``Enum`` capabilities, however; consequently, it can have duplicate values. There is also a ``module`` function that can insert the ``Constant`` class into ``sys.modules`` where it will appear to be a module whose top-level names cannot be rebound. Module Contents --------------- ``NamedTuple`` ~~~~~~~~~~~~ Base class for ``creating NamedTuples``, either by subclassing or via it's functional API. ``Constant`` ~~~~~~~~~~ Constant class for creating groups of constants. These names cannot be rebound to other values. ``Enum`` ~~~~~~ Base class for creating enumerated constants. ``IntEnum`` ~~~~~~~~~ Base class for creating enumerated constants that are also subclasses of ``int``. ``AutoNumberEnum`` ~~~~~~~~~~~~~~~~ Derived class that automatically assigns an ``int`` value to each member. ``OrderedEnum`` ~~~~~~~~~~~~~ Derived class that adds ``<``, ``<=``, ``>=``, and ``>`` methods to an ``Enum``. ``UniqueEnum`` ~~~~~~~~~~~~ Derived class that ensures only one name is bound to any one value. ``unique`` ~~~~~~~~ Enum class decorator that ensures only one name is bound to any one value. ``constant`` ~~~~~~~~~~ Descriptor to add constant values to an ``Enum`` ``convert`` ~~~~~~~~ Helper to transform target global variables into an ``Enum``. ``enum`` ~~~~~~ Helper for specifying keyword arguments when creating ``Enum`` members. ``export`` ~~~~~~~~ Helper for inserting ``Enum`` members into a namespace (usually ``globals()``. ``extend_enum`` ~~~~~~~~~~~~~ Helper for adding new ``Enum`` members after creation. ``module`` ~~~~~~~~ Function to take a ``Constant`` or ``Enum`` class and insert it into ``sys.modules`` with the affect of a module whose top-level constant and member names cannot be rebound. ``skip`` ~~~~~~ Descriptor to add a normal (non-``Enum`` member) attribute to an ``Enum`` or ``Constant``. Creating an Enum ---------------- Enumerations can be created using the ``class`` syntax, which makes them easy to read and write. To define an enumeration, subclass ``Enum`` as follows:: >>> from aenum import Enum >>> class Color(Enum): ... red = 1 ... green = 2 ... blue = 3 The ``Enum`` class is also callable, providing the following functional API:: >>> Animal = Enum('Animal', 'ant bee cat dog') >>> Animal >>> Animal.ant >>> Animal.ant.value 1 >>> list(Animal) [, , , ] Creating NamedTuples -------------------- Simple ^^^^^^ The most common way to create a new NamedTuple will be via the functional API:: >>> from aenum import NamedTuple >>> Book = NamedTuple('Book', 'title author genre', module=__name__) Advanced ^^^^^^^^ The simple method of creating ``NamedTuples`` requires always specifying all possible arguments when creating instances; failure to do so will raise exceptions. However, it is possible to specify both docstrings and default values when creating a ``NamedTuple`` using the class method:: >>> class Point(NamedTuple): ... x = 0, 'horizontal coordinate', 0 ... y = 1, 'vertical coordinate', 0 ... >>> Point() Point(x=0, y=0) Creating Constants ------------------ ``Constant`` is similar to ``Enum``, but do not support the ``Enum`` protocols, and have no restrictions on duplications:: >>> class K(Constant): ... PI = 3.141596 ... TAU = 2 * PI ... >>> K.TAU 6.283192 ------------------------------------------------------------------- Some of the inspirations: An Enum of Enums: http://stackoverflow.com/q/30471864/208880 C-like Enums: http://stackoverflow.com/q/29264941/208880 Extending Enums: http://stackoverflow.com/q/28126314/208880 Named constants: http://stackoverflow.com/q/31537316/208880