easy_enum-0.3.0/0000775000175000017500000000000013526445407014617 5ustar molejarmolejar00000000000000easy_enum-0.3.0/setup.py0000664000175000017500000000321113526445277016333 0ustar molejarmolejar00000000000000#!/usr/bin/env python # Copyright 2019 Martin Olejar # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from os import path from setuptools import setup from easy_enum import __version__, __license__, __author__, __contact__ def long_description(): try: import pypandoc readme_path = path.join(path.dirname(__file__), 'README.md') return pypandoc.convert(readme_path, 'rst').replace('\r', '') except (IOError, ImportError): return ( "More on: https://github.com/molejar/pyEnum" ) setup( name='easy_enum', version=__version__, license=__license__, author=__author__, author_email=__contact__, url='https://github.com/molejar/pyEnum', description='User friendly implementation of Enum in Python', long_description=long_description(), py_modules=['easy_enum'], test_suite="tests", classifiers=[ 'Operating System :: OS Independent', 'Programming Language :: Python :: 3', 'License :: OSI Approved :: Apache Software License', 'Topic :: Scientific/Engineering', 'Topic :: Software Development', 'Topic :: Utilities' ] )easy_enum-0.3.0/PKG-INFO0000664000175000017500000001225513526445407015721 0ustar molejarmolejar00000000000000Metadata-Version: 1.1 Name: easy_enum Version: 0.3.0 Summary: User friendly implementation of Enum in Python Home-page: https://github.com/molejar/pyEnum Author: Martin Olejar Author-email: martin.olejar@gmail.com License: Apache 2.0 Description: pyEnum ====== |Build Status| |Coverage Status| |PyPI Status| |Python Version| User friendly implementation of documented ``Enum`` type for Python language. Installation ------------ .. code:: bash $ pip install easy_enum To install the latest version from master branch execute in shell following commands: .. code:: bash $ pip install -U https://github.com/molejar/pyEnum/archive/master.zip In case of development, install pyEnum from sources: .. code:: bash $ git clone https://github.com/molejar/pyEnum.git $ cd pyEnum $ pip install -U -e . You may run into a permissions issues running these commands. Here are a few options how to fix it: 1. Run with ``sudo`` to install pyEnum and dependencies globally 2. Specify the ``--user`` option to install locally into your home directory (export "~/.local/bin" into PATH variable if haven't). 3. Run the command in a `virtualenv `__ local to a specific project working set. Usage ----- Following example is showing how easy you can use this Enum in your code: .. code:: python from easy_enum import Enum class TestEnum(Enum): # attribute with no description, the name will be 'FIRST_ITEM' and empty string as description FIRST_ITEM = 1 # attribute with description SECOND_ITEM = (2, 'Description for second item') # attribute with description and custom string name THIRD_ITEM = (3, 'third', 'Description for third item') # attribute with custom string name (the description must be specified as empty string) FOURTH_ITEM = (4, 'fourth', '') # Read attributes value and name print(TestEnum.SECOND_ITEM) # 2 print(TestEnum['FIRST_ITEM']) # 1 print(TestEnum[1]) # 'FIRST_ITEM' print(TestEnum[3]) # 'third' print(TestEnum['third']) # 3 # Use get method with default value if want skip exception print(TestEnum.get(8)) # None print(TestEnum.get('eight')) # None print(TestEnum.get(8, 'eight')) # 'eight' # Check if exist attribute with specific value print(1 in TestEnum) # True print(8 in TestEnum) # False # Check if exist attribute with specific name print('first' in TestEnum) # False print('third' in TestEnum) # True # Get attribute description (as parameter use attribute name or value) print(TestEnum.desc(1)) # '' print(TestEnum.desc(2)) # 'Description for second item' print(TestEnum.desc('third')) # 'Description for third item' # Get count of all attributes print(len(TestEnum)) # 4 # Get list with all attributes name names = [item[0] for item in TestEnum] print(names) # ['FIRST_ITEM', 'SECOND_ITEM', 'third', 'fourth'] # Get list with all attributes value values = [item[1] for item in TestEnum] print(values) # [1, 2, 3, 4] # Read all items for name, value, desc in TestEnum: print('{} = {} ({})'.format(name, value, desc)) .. |Build Status| image:: https://travis-ci.org/molejar/pyEnum.svg?branch=master :target: https://travis-ci.org/molejar/pyEnum .. |Coverage Status| image:: https://coveralls.io/repos/github/molejar/pyEnum/badge.svg?branch=master :target: https://coveralls.io/github/molejar/pyEnum?branch=master .. |PyPI Status| image:: https://img.shields.io/pypi/v/easy-enum.svg :target: https://pypi.python.org/pypi/easy-enum .. |Python Version| image:: https://img.shields.io/pypi/pyversions/easy-enum.svg :target: https://www.python.org Platform: UNKNOWN Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python :: 3 Classifier: License :: OSI Approved :: Apache Software License Classifier: Topic :: Scientific/Engineering Classifier: Topic :: Software Development Classifier: Topic :: Utilities easy_enum-0.3.0/easy_enum.py0000664000175000017500000000635413526445277017173 0ustar molejarmolejar00000000000000# Copyright 2019 Martin Olejar # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. __author__ = "Martin Olejar" __contact__ = "martin.olejar@gmail.com" __version__ = "0.3.0" __license__ = "Apache 2.0" __status__ = "Development" __all__ = ['Enum'] class MetaEnum(type): """ Meta Class for Enum Type """ def __new__(mcs, name, bases, attrs): cls = super().__new__(mcs, name, bases, attrs) cls._items_ = list() for attr, value in attrs.items(): if attr in set(dir(type(name, (object,), {}))) or (attr.startswith('_') and attr.endswith('_')): continue if isinstance(value, classmethod): continue if isinstance(value, tuple): if len(value) == 2: cls._items_.append((attr, value[0], value[1])) else: cls._items_.append((value[1], value[0], value[2])) setattr(cls, attr, value[0]) else: cls._items_.append((attr, value, '')) return cls def __getitem__(cls, key): if isinstance(key, str): for name, value, _ in cls._items_: if key.upper() == name.upper(): return value raise KeyError("\'%s\' has no item with name \'%s\'" % (cls.__name__, key)) elif isinstance(key, int): for name, value, _ in cls._items_: if key == value: return name raise KeyError("\'%s\' has no item with value \'%d\'" % (cls.__name__, key)) else: raise TypeError("\'%s\' has no item with type \'%r\'" % (cls.__name__, type(key))) def __iter__(cls): return (item for item in cls._items_) def __contains__(cls, item): if isinstance(item, str) and item in (item[0] for item in cls._items_): return True if isinstance(item, int) and item in (item[1] for item in cls._items_): return True return False def __len__(cls): return len(cls._items_) class Enum(metaclass=MetaEnum): """ Enum Type Class """ @classmethod def get(cls, key, default=None): try: return cls[key] except KeyError: return default @classmethod def desc(cls, key, default=''): if isinstance(key, str): for name, _, desc in cls._items_: if key.upper() == name.upper(): return desc return default elif isinstance(key, int): for _, value, desc in cls._items_: if key == value: return desc return default else: raise TypeError("\'%s\' has no item with type \'%r\'" % (cls.__name__, type(key))) easy_enum-0.3.0/setup.cfg0000664000175000017500000000004613526445407016440 0ustar molejarmolejar00000000000000[egg_info] tag_build = tag_date = 0 easy_enum-0.3.0/README.md0000664000175000017500000000664513526445277016116 0ustar molejarmolejar00000000000000pyEnum ====== [![Build Status](https://travis-ci.org/molejar/pyEnum.svg?branch=master)](https://travis-ci.org/molejar/pyEnum) [![Coverage Status](https://coveralls.io/repos/github/molejar/pyEnum/badge.svg?branch=master)](https://coveralls.io/github/molejar/pyEnum?branch=master) [![PyPI Status](https://img.shields.io/pypi/v/easy-enum.svg)](https://pypi.python.org/pypi/easy-enum) [![Python Version](https://img.shields.io/pypi/pyversions/easy-enum.svg)](https://www.python.org) User friendly implementation of documented `Enum` type for Python language. Installation ------------ ``` bash $ pip install easy_enum ``` To install the latest version from master branch execute in shell following commands: ``` bash $ pip install -U https://github.com/molejar/pyEnum/archive/master.zip ``` In case of development, install pyEnum from sources: ``` bash $ git clone https://github.com/molejar/pyEnum.git $ cd pyEnum $ pip install -U -e . ``` You may run into a permissions issues running these commands. Here are a few options how to fix it: 1. Run with `sudo` to install pyEnum and dependencies globally 2. Specify the `--user` option to install locally into your home directory (export "~/.local/bin" into PATH variable if haven't). 3. Run the command in a [virtualenv](https://virtualenv.pypa.io/en/latest/) local to a specific project working set. Usage ----- Following example is showing how easy you can use this Enum in your code: ``` Python from easy_enum import Enum class TestEnum(Enum): # attribute with no description, the name will be 'FIRST_ITEM' and empty string as description FIRST_ITEM = 1 # attribute with description SECOND_ITEM = (2, 'Description for second item') # attribute with description and custom string name THIRD_ITEM = (3, 'third', 'Description for third item') # attribute with custom string name (the description must be specified as empty string) FOURTH_ITEM = (4, 'fourth', '') # Read attributes value and name print(TestEnum.SECOND_ITEM) # 2 print(TestEnum['FIRST_ITEM']) # 1 print(TestEnum[1]) # 'FIRST_ITEM' print(TestEnum[3]) # 'third' print(TestEnum['third']) # 3 # Use get method with default value if want skip exception print(TestEnum.get(8)) # None print(TestEnum.get('eight')) # None print(TestEnum.get(8, 'eight')) # 'eight' # Check if exist attribute with specific value print(1 in TestEnum) # True print(8 in TestEnum) # False # Check if exist attribute with specific name print('first' in TestEnum) # False print('third' in TestEnum) # True # Get attribute description (as parameter use attribute name or value) print(TestEnum.desc(1)) # '' print(TestEnum.desc(2)) # 'Description for second item' print(TestEnum.desc('third')) # 'Description for third item' # Get count of all attributes print(len(TestEnum)) # 4 # Get list with all attributes name names = [item[0] for item in TestEnum] print(names) # ['FIRST_ITEM', 'SECOND_ITEM', 'third', 'fourth'] # Get list with all attributes value values = [item[1] for item in TestEnum] print(values) # [1, 2, 3, 4] # Read all items for name, value, desc in TestEnum: print('{} = {} ({})'.format(name, value, desc)) ``` easy_enum-0.3.0/easy_enum.egg-info/0000775000175000017500000000000013526445407020276 5ustar molejarmolejar00000000000000easy_enum-0.3.0/easy_enum.egg-info/SOURCES.txt0000664000175000017500000000024313526445407022161 0ustar molejarmolejar00000000000000README.md easy_enum.py setup.py easy_enum.egg-info/PKG-INFO easy_enum.egg-info/SOURCES.txt easy_enum.egg-info/dependency_links.txt easy_enum.egg-info/top_level.txteasy_enum-0.3.0/easy_enum.egg-info/top_level.txt0000664000175000017500000000001213526445407023021 0ustar molejarmolejar00000000000000easy_enum easy_enum-0.3.0/easy_enum.egg-info/PKG-INFO0000664000175000017500000001225513526445407021400 0ustar molejarmolejar00000000000000Metadata-Version: 1.1 Name: easy-enum Version: 0.3.0 Summary: User friendly implementation of Enum in Python Home-page: https://github.com/molejar/pyEnum Author: Martin Olejar Author-email: martin.olejar@gmail.com License: Apache 2.0 Description: pyEnum ====== |Build Status| |Coverage Status| |PyPI Status| |Python Version| User friendly implementation of documented ``Enum`` type for Python language. Installation ------------ .. code:: bash $ pip install easy_enum To install the latest version from master branch execute in shell following commands: .. code:: bash $ pip install -U https://github.com/molejar/pyEnum/archive/master.zip In case of development, install pyEnum from sources: .. code:: bash $ git clone https://github.com/molejar/pyEnum.git $ cd pyEnum $ pip install -U -e . You may run into a permissions issues running these commands. Here are a few options how to fix it: 1. Run with ``sudo`` to install pyEnum and dependencies globally 2. Specify the ``--user`` option to install locally into your home directory (export "~/.local/bin" into PATH variable if haven't). 3. Run the command in a `virtualenv `__ local to a specific project working set. Usage ----- Following example is showing how easy you can use this Enum in your code: .. code:: python from easy_enum import Enum class TestEnum(Enum): # attribute with no description, the name will be 'FIRST_ITEM' and empty string as description FIRST_ITEM = 1 # attribute with description SECOND_ITEM = (2, 'Description for second item') # attribute with description and custom string name THIRD_ITEM = (3, 'third', 'Description for third item') # attribute with custom string name (the description must be specified as empty string) FOURTH_ITEM = (4, 'fourth', '') # Read attributes value and name print(TestEnum.SECOND_ITEM) # 2 print(TestEnum['FIRST_ITEM']) # 1 print(TestEnum[1]) # 'FIRST_ITEM' print(TestEnum[3]) # 'third' print(TestEnum['third']) # 3 # Use get method with default value if want skip exception print(TestEnum.get(8)) # None print(TestEnum.get('eight')) # None print(TestEnum.get(8, 'eight')) # 'eight' # Check if exist attribute with specific value print(1 in TestEnum) # True print(8 in TestEnum) # False # Check if exist attribute with specific name print('first' in TestEnum) # False print('third' in TestEnum) # True # Get attribute description (as parameter use attribute name or value) print(TestEnum.desc(1)) # '' print(TestEnum.desc(2)) # 'Description for second item' print(TestEnum.desc('third')) # 'Description for third item' # Get count of all attributes print(len(TestEnum)) # 4 # Get list with all attributes name names = [item[0] for item in TestEnum] print(names) # ['FIRST_ITEM', 'SECOND_ITEM', 'third', 'fourth'] # Get list with all attributes value values = [item[1] for item in TestEnum] print(values) # [1, 2, 3, 4] # Read all items for name, value, desc in TestEnum: print('{} = {} ({})'.format(name, value, desc)) .. |Build Status| image:: https://travis-ci.org/molejar/pyEnum.svg?branch=master :target: https://travis-ci.org/molejar/pyEnum .. |Coverage Status| image:: https://coveralls.io/repos/github/molejar/pyEnum/badge.svg?branch=master :target: https://coveralls.io/github/molejar/pyEnum?branch=master .. |PyPI Status| image:: https://img.shields.io/pypi/v/easy-enum.svg :target: https://pypi.python.org/pypi/easy-enum .. |Python Version| image:: https://img.shields.io/pypi/pyversions/easy-enum.svg :target: https://www.python.org Platform: UNKNOWN Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python :: 3 Classifier: License :: OSI Approved :: Apache Software License Classifier: Topic :: Scientific/Engineering Classifier: Topic :: Software Development Classifier: Topic :: Utilities easy_enum-0.3.0/easy_enum.egg-info/dependency_links.txt0000664000175000017500000000000113526445407024344 0ustar molejarmolejar00000000000000