https://github.com/kaiyou/pyconsts
Simple python constant management
https://github.com/kaiyou/pyconsts
Last synced: 3 months ago
JSON representation
Simple python constant management
- Host: GitHub
- URL: https://github.com/kaiyou/pyconsts
- Owner: kaiyou
- License: gpl-3.0
- Created: 2015-05-03T10:29:29.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-05-03T10:32:35.000Z (about 10 years ago)
- Last Synced: 2025-01-20T07:13:58.443Z (5 months ago)
- Language: Python
- Size: 121 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
pyconsts
========Except for configuration-based constants that should be managed in
configuration files, one often has to declare module-specific constants when
writing code. For instance: one might plan on coding multiple choice values,
naming monkey-patched attributes or dynamic database fields.Dictionaries are the most straightforward implementation. Object-like syntax is
however best suited for code readability and one should make sure these
constants are in fact constant (not modified during the course of the program).Enum are also quite useful at achieving this goal, yet they lack the
immutability and their syntax is more suited for enumerating attributes than
storing constant values.pyconsts aims at providing simple functions for constant management. Constants
are enforced during a single runtime. Therefore, one should not rely on
pyconsts for constants that must remain unchanged over the course of multiple
runtimes (protocol constants for instance).Use cases
=========Coding for enum-like indices
----------------------------``` python
Colors = pyconsts.index(
"red", "blue", "green"
)color = Colors.red
if color == Colors.red:
print "This is red!"elif color == Colors.blue:
print "This is blue instead!"
```Naming attributes when monkey patching
--------------------------------------``` python
ATTR = pyconsts.text(pattern="__monkey_%s__",
"COUNT", "VALUE"
)setattr(mypatchedtype, ATTR.COUNT, 2)
print(mypatchedtype.__monkey_COUNT__)
```