o3bvv / candv Goto Github PK
View Code? Open in Web Editor NEWCreate complex enum-like constants in Python
Home Page: http://candv.rtfd.io
License: MIT License
Create complex enum-like constants in Python
Home Page: http://candv.rtfd.io
License: MIT License
Make constant's reference to own container public instead of protected.
Assume you have the following package structure:
.
└── x
├── __init__.py
└── y
├── __init__.py
└── constants.py
where x.y.constants.py
contains the following:
# -*- coding: utf-8 -*-
from candv import Constants, SimpleConstant
class FOO_TYPES(Constants):
PRIMARY = SimpleConstant()
SECONDARY = SimpleConstant()
If x
is in Python's path, and you import constants from x.y
and simply from y
, then their module will differ and comparison of constants will fail:
>>> import sys
>>> sys.path.insert(0, './x')
>>> from x.y.constants import FOO_TYPES as X_TYPES
>>> from y.constants import FOO_TYPES as Y_TYPES
>>> X_TYPES.__module__
'x.y.constants'
>>> Y_TYPES.__module__
'y.constants'
>>> X_TYPES.PRIMARY
<constant 'FOO_TYPES.PRIMARY'>
>>> Y_TYPES.PRIMARY
<constant 'FOO_TYPES.PRIMARY'>
>>> X_TYPES.PRIMARY == Y_TYPES.PRIMARY
False
Solution: constants base must implement own __hash__
and __eq__
magic methods, whose result will be based on full name of constant.
Add ability to get:
__name__
)__repr__
of a container__repr__
of a groupProvide ability to set dynamic values for constants using some storage (e.g. integrate with Redis, django-constance or whatsoever).
Add support of default values for such methods as get_by_name
, get_by_value
, etc.
Add ability to specify class of constants via dynamic mixin, e.g. this:
class FOO(Constants):
constant_class = FOO_CONSTANT
ONE = FOO_CONSTANT()
TWO = FOO_CONSTANT()
with decorator will look like:
class FOO(with_constant_class(FOO_CONSTANT), Constants):
ONE = FOO_CONSTANT()
TWO = FOO_CONSTANT()
Constants which are not instances of container's constant_class
are not initialized. E.g.
>>> from candv import Values, SimpleConstant
>>> class FOO(Values):
... BAR = SimpleConstant()
...
...
>>> FOO.BAR
>>> FOO.BAR.name
>>> repr(FOO.BAR)
''
Despite FOO.BAR
is invalid, it still needs to be initialized.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.