easy_enum-0.3.0/ 0000775 0001750 0001750 00000000000 13526445407 014617 5 ustar molejar molejar 0000000 0000000 easy_enum-0.3.0/setup.py 0000664 0001750 0001750 00000003211 13526445277 016333 0 ustar molejar molejar 0000000 0000000 #!/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-INFO 0000664 0001750 0001750 00000012255 13526445407 015721 0 ustar molejar molejar 0000000 0000000 Metadata-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.py 0000664 0001750 0001750 00000006354 13526445277 017173 0 ustar molejar molejar 0000000 0000000 # 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.cfg 0000664 0001750 0001750 00000000046 13526445407 016440 0 ustar molejar molejar 0000000 0000000 [egg_info]
tag_build =
tag_date = 0
easy_enum-0.3.0/README.md 0000664 0001750 0001750 00000006645 13526445277 016116 0 ustar molejar molejar 0000000 0000000 pyEnum
======
[](https://travis-ci.org/molejar/pyEnum)
[](https://coveralls.io/github/molejar/pyEnum?branch=master)
[](https://pypi.python.org/pypi/easy-enum)
[](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/ 0000775 0001750 0001750 00000000000 13526445407 020276 5 ustar molejar molejar 0000000 0000000 easy_enum-0.3.0/easy_enum.egg-info/SOURCES.txt 0000664 0001750 0001750 00000000243 13526445407 022161 0 ustar molejar molejar 0000000 0000000 README.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.txt easy_enum-0.3.0/easy_enum.egg-info/top_level.txt 0000664 0001750 0001750 00000000012 13526445407 023021 0 ustar molejar molejar 0000000 0000000 easy_enum
easy_enum-0.3.0/easy_enum.egg-info/PKG-INFO 0000664 0001750 0001750 00000012255 13526445407 021400 0 ustar molejar molejar 0000000 0000000 Metadata-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.txt 0000664 0001750 0001750 00000000001 13526445407 024344 0 ustar molejar molejar 0000000 0000000