Package: constants
The problem?Most applications use constants. Many constants take different values based
on the environment the application is executed in.Think database credentials over development, testing, staging, production or
stock market execution over development, testing, paper, production …A solutionShamelessly inspired by theapp_constantsgem,constantsaims to solve that
problem (and that problem only)..ini fileconstantsuses the .ini file format to specify the application constants
values in each environment. DEFAULT values are available in every environment
unless specifically overridden in a section.[DEFAULT]
something = a_default_value
all =  1
a_string = 0350

[a_section]
something = a_section_value
just_for_me = 5.0
flag = False
minutes = 365 * 24 * 60To find out more about ini files and sections, check the Python standard
libraryconfigparserdocumention.The default file isconstants.iniin the current working directory. but
you can use any filename you want cf.Instantiation.EnvironmentDefine the environment the application will run in. The default environment
variable to store that value is __CONSTANTS__, but you can use any variable
name you want cf.Instantiation.Most platform have a way to do that, in bash:export __CONSTANTS__=a_sectionInstantiation>>> import constants
>>> consts = constants.Constants()On instantiation, constants looks for an environement variable named
__CONSTANTS__ whose value is used to find out which section of the
constants.ini file should be used.Constants’ constructor takes two (2) optional parameters.variablelet’s you specify the name of the environment variable andfilenamethe absolute path to the .ini file containing the constants definitions.>>> consts = Constants(variable='AN_ENVIRONMENT_VARIABLE',
...                    filename='constants.cfg') # doctest: +SKIPValuesTo access the values, the instance can be used like a dictionary (getitem).>>> consts['something']
'a_section_value'Values are cast into integer, float or boolean when pertinent.>>> consts['all']
1
>>> consts.a_string
'0350'
>>> consts.flag
FalseExpressions are evaluated.>>> consts.minutes
525600Values can also be accessed using the . operator (getattr)>>> consts.all
1Warning“We are responsible adults” yet, inspired by Matthew Wilson’ssuggestionto
raise an exception when an attempt is made tochange a constant,constantsissueswarnings…>>> import warnings>>> with warnings.catch_warnings(record=True) as warning:
...     # reassigning the constant all
...     consts.all = 2>>> warning[0].message
UserWarning('all changed to 2',)… andchanges the constantanyway.>>> consts.all
2It does so with the dict like assignment as well.>>> with warnings.catch_warnings(record=True) as warning:
...     consts['something'] = 'a_new_value'>>> warning[0].message
UserWarning('something changed to a_new_value',)>>> consts['something']
'a_new_value'Loggingconstantsaims to be a goodloggingcitizen, grafting a logger to the
logging tree.All calls to the logger methods expose an extra logRecord key calledmethod.With the logging level set to INFO, it logs one and only one useful message.>>> import sys
>>> import logging
>>> logging.basicConfig(level=logging.INFO,
...                     stream=sys.stdout,
...                     format='%(levelname)s %(name)s.%(method)s %(message)s')
>>> consts = constants.Constants() # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
INFO constants.load
variable: __CONSTANTS__,
filename: constants.ini,
environment: a_section,
constants: {...}At DEBUG level it becomes a tadnoisier.>>> logging.root.setLevel(logging.DEBUG)
>>> debug_me  = consts.just_for_me # doctest: +ELLIPSIS
DEBUG constants.__getattr__ begin (..., 'just_for_me') {}
DEBUG constants.__getitem__ begin (..., 'just_for_me') {}
DEBUG constants.cast begin ('5.0',) {}
DEBUG constants.cast end 5.0
DEBUG constants.__getitem__ end 5.0
DEBUG constants.__getattr__ end 5.0Installationconstantsis available onPyPI…pip install constants… and can be forked onGitHub.