Photo by rawpixel on Unsplash

Optenum

Photo by rawpixel on Unsplash

Optenum

Optenum is a Python Option/Enum library supports enum code, name, text, even (code, name) tuple list and so on.

Name “Optenum” comes from ‘option’ + ‘enumeration’.

Compatible with Python 2.7+ and Python 3.4+.

Check out from GitHub

Install

Python 3.x, 2.7

pip install optenum

For those probably missing six module:

pip install six optenum

Quick started

  1. Simple as Enum type

    Says we define a simple enum:

    from optenum import Options
     
    class Fruit(Options):
        APPLE = 1
        ORANGE = 2
        BANANA = 3 
    

    Try the following in Python command line:

    >>> from optenum import Option, Options
    >>> class Fruit(Options):
    ...     APPLE = 1
    ...     ORANGE = 2
    ...     BANANA = 3
    >>> 
    >>> Fruit.APPLE
    <Option code=1 name=APPLE text=None>
    >>> print(Fruit.APPLE)
    1
    >>> Fruit.APPLE.code
    1
    >>> Fruit.APPLE.name
    'APPLE'
    >>> Fruit.APPLE.text
    >>> print(Fruit.APPLE.text)
    None
    >>> Fruit.APPLE.get_text()
    'apple'
        
    
  2. Complex declaration

    You may declare your Options (Enums) in many annotations.

    from optenum import Option, Options
     
    class EnumCellPhone(Options):
        APPLE = 1
        SAMSUNG = Option(2, name='SAMSUNG')
        HUAWEI = 3, 'Huawei cellphone'     # tuple annotation. name = code, text
        
        
    class DoorState(Options):
        OPEN = 'O', 'Door is opened'       # tuple annotation. name = code, text
        CLOSED = ('C', 'Door is closed')   # tuple annotation, too.
        IN_OPENING = 'IO'
        IN_CLOSING = 'IC'
        
        _FLAG = False           # underscore leading name is not an option
        x = lambda y: y         # function/callable is not an option
    
  3. Operators

    Option support some operators. See more in operators.md.

    >>> class Favorite(Options):
    ...     APPLE = 1
    ...     BANANA = 3, 'Banana hot'
    ... 
    >>> 
    >>> Fruit.APPLE == Favorite.APPLE
    True
    >>> Fruit.BANANA == Favorite.BANANA
    False
    >>> Fruit.APPLE + 1 == Fruit.ORANGE
    True
    >>> Fruit.BANANA >> 2
    0
    >>> Fruit.BANANA << 2
    12
    >>> Fruit.BANANA > Favorite.APPLE
    True
    
  4. Collections

    Options provides some collections for accessing option and it’s fields. See section “Collections for Options” below for more information.

    >>> Fruit.codes
    [1, 2, 3]
    >>> Fruit.names
    ['ORANGE', 'APPLE', 'BANANA']
    >>> Fruit.all
    [<Option code=2 name=ORANGE text=None>, <Option code=1 name=APPLE text=None>, <Option code=3 name=BANANA text=None>]
    >>> Fruit.tuples
    [('ORANGE', 2, None), ('APPLE', 1, None), ('BANANA', 3, None)]
    >>> Favorite.items
    {'APPLE': <Option code=1 name=APPLE text=None>, 'BANANA': <Option code=3 name=BANANA text=Banana hot>}
    >>> Favorite.get_list('code','text')
    [(1, None), (3, 'Banana hot')]
    >>> Favorite.get_dict('name','text')
    {'APPLE': None, 'BANANA': 'Banana hot'}
        
    
  5. Django model choices

    To be written
    
Avatar
Samuel Chen
developer, architect

My interests include cloud as a software, distributed web system, mobile computing and programmable matter.