-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstants.py
More file actions
47 lines (33 loc) · 1.5 KB
/
constants.py
File metadata and controls
47 lines (33 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from types import MappingProxyType
import keyword
class _ConstantsMeta(type):
def __setattr__(cls, key, value):
if key in cls.__dict__:
raise AttributeError(f"Cannot overwrite existing attribute '{key}'")
if key in cls._constants or key in cls._mutable_constants:
raise AttributeError(f"Cannot modify constant '{key}'.")
if key in {"_constants", "_mutable_constants"}:
raise AttributeError("Cannot overwrite internal constants!")
super().__setattr__(key, value)
class Constants(metaclass=_ConstantsMeta):
_mutable_constants = {}
_constants = MappingProxyType(_mutable_constants)
def __init__(self):
raise ValueError("Hey! No trying to access private data through an instance.")
@classmethod
def new(cls, name, value):
if not isinstance(name, str):
raise ValueError("Key must be a string")
if not name.isidentifier() or keyword.iskeyword(name):
raise NameError(f"'{name}' is not a valid Python variable name")
if name in cls._mutable_constants:
raise ValueError(f"The Constant '{name}' is already defined and immutable")
cls._mutable_constants[name] = value
@classmethod
def get(cls, name):
if name not in cls._constants:
raise KeyError(f"The Constant '{name}' is not defined")
return cls._constants[name]
@classmethod
def all(cls):
return cls._constants