PyConst one simple way to organize the constants


Keywords
constants, django, data, control
License
GPL-3.0
Install
pip install pyconst==1.1.2

Documentation

PyConst

Build Status Coverage Status License: GPL v3 Latest Version Pypi Download

The problem

Everbody in Python need create a little constants in your project. I had been working with Django Project and is a good pratices create constants for permission or any others values that could be global in your project. And could be good for translation, your IDE can help you with autocomplete and other things

Ok, everybody that use constants in your project is a good pratices :D

Why I need this ?

Think that you need create one const.py with a lot constants and aggroup the values

ADD_USER = 'add_user'
UPDATE_USER = 'add_user'

USER_PERMISSIONS = (
    (ADD_USER, 'Add User'),
    (UPDATE_USER, 'Update User'),
)

Ok, this is simple, but think in a case that you have more than 100 values in your file. Or think that you need create one constant with a similar name.

ADD_USER_PRIVATE = 'add_user_private'

You must check the value if is not duplicated, because you can overrind other values.

The problem

In my module, now I'll need only one value from my constant

from company.const import (
    ADD_PERSON,
    EXCLUDE_PERSON,
    PERSON_STATE_START,
    PERSON_STATE_END,
    PERSON_STATE_WAITING,
    ...
    )
from .const import (
    USER_PERMISSIONS,
    ADD_USER
    )

@has_perm(ADD_USER)
def my_example():
    choices = USER_PERMISSIONS
    ....

And think that you USER_PERMISSIONS could be bigger, with 30 permissions or more ... ADD_USER no have any connection with USER_PERMISSION, I could have ADD_USER in a different constants without connection with USER_PERMISSION, something like ADMIN_DEFAULT_ACTIONS or MANAGER_CONSTANT.

The Solution (Morning Sun)

Yes, we working with Python. And now we can set the contants as one Const

from pyconst import Const

USER_PERMISSIONS = Const(
    ('add_user', 'AddUser'),
    ('update_user', 'UpdateUser'),
)

Enable Auto Complate

And USER_PERMISSION will have one new attribute by permission

In [5]: USER_PERMISSIONS.add_user
Out[5]: 'ADD_USER'

In [6]: USER_PERMISSIONS.add_user.label
Out[6]: u'Add User'

Check that is more easy undestand the constants and organize the values, and no need use multiple imports to get values. Because the values are in constants

from company.const import COMPANY_PERMISSIONS
from company.const import COMPANY_WORKFLOW
from user.const import USER_PERMISSIONS

Example in django model

from pyconst import Const

USER_PERMISSIONS = Const(
    'ADD_USER',
    'UPDATE_USER'
)

class CustomUser(AbstractUser):

    class Meta:
        permissions = USER_PERMISSIONS

Add value in constants

>>> from pyconst import Const
>>> const = Const()
>>> cont.add(attr='my_attribute_name', label='My Label Name')
>>> cont.my_attribute_name
'my_attribute_name'
>>> cont.my_attribute_name.name
'My Label Name'
>>> cont.my_attribute_name.label
'My Label Name'

or

>>> from pyconst import Const
>>> const = Const('my_attribute_name')
>>> const.my_attribute_name
'my_attribute_name'

or

>>> from pyconst import Const
>>> const = Const(('MY_ATTRIBUTE_NAME', 'my_attribute_value'))
>>> const.MY_ATTRIBUTE_NAME
'my_attribute_value'

or

>>> from pyconst import Const
>>> const = Const(('my_attribute_name', 'my_attribute_value', 'My Label Name'))

or

>>> c = Const()
>>> c.add('first item', 1)
>>> c.first_item
'1'

or

>>> c = Const()
>>> c.add(attr="my_attr", value=1, label='First Item')
>>> c.my_attr
'1'