Skip to content

Instantly share code, notes, and snippets.

@jpkotta
Created January 11, 2016 16:44
Show Gist options
  • Save jpkotta/c1df14ce4def4a0fe57c to your computer and use it in GitHub Desktop.
Save jpkotta/c1df14ce4def4a0fe57c to your computer and use it in GitHub Desktop.
def make_constants(mapping):
"""Create a namedtuple of constants
These are a poor man's enum.
Example:
c = make_constants({"ONE":1, "TWO":2, "THREE":3})
c.ONE == 1
:param mapping: A mapping with names as keys and values as values.
The names must be valid python identifier strings.
:returns: A namedtuple with the values given in mapping
:rtype: namedtuple
"""
nt = namedtuple("_Constants", mapping.viewkeys())
return nt(**mapping)
STATUS = make_constants(
OrderedDict([
("idle", 0),
("start", 1),
("success", 2),
("fail", 3),
("continuous", 4),
("stop", 5),
]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment