Skip to main content

PyConst one simple way to organize the constants

Project description

Build Status Coverage Status License: GPL v3 Latest Version

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',
    'Update User'
)
Enable Auto Complate

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(label='My Label Name', attr='my_attribute_name')

or

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

or

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

or

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

or

>>> c = Cont()
>>> c.add('First Item', 1)
>>> c._1
'1'

or

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyconst-1.1.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyconst-1.1.2-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file pyconst-1.1.2.tar.gz.

File metadata

  • Download URL: pyconst-1.1.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for pyconst-1.1.2.tar.gz
Algorithm Hash digest
SHA256 59866d963eb6b6a94db623f287fa5baf42d62d84bce294584a2515ae21d7d1bb
MD5 636b75089cef6850dd2c678e06230ab0
BLAKE2b-256 fc8e434634ff0624927ae300043ac21918a986f48431cc60393621465754560f

See more details on using hashes here.

File details

Details for the file pyconst-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: pyconst-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 16.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for pyconst-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f1ba93594cba00fbd6e2bfd1646bfeb29d07613bab80b6f2d8c37bd579d63d88
MD5 1438557afce85fdc8e264854fdb911e8
BLAKE2b-256 77a74ea1c0ee370769079c3b4022e6810d0dcec2ddf592fda0fc9d6b3ebbe52c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page