Create self-named enums beautifully
Project description
Enumify
A quick way to write documented Enums, or Enums with member values
Install
pip install enumify
What is Enumify?
Enumify was built on an idea of how could I be the absolute laziest at declaring Enums, honestly!
I didn't enjoy the idea of how I would need to re-type out member names if I just wanted them to be a 1:1, such as the following:
import enum
class DaysOfWeek(enum.Enum):
MONDAY = "monday"
TUESDAY = "tuesday"
WEDNESDAY = "wednesday"
There is of course, an alternative that lives in fastcore
, specifically the mk_class
, which annotates like the following:
from fastcore.basics import mk_class
mk_class("DaysOfWeek", {o:o.lower() for o in ["MONDAY", "TUESDAY", "WEDNESDAY"]})
But I didn't like that the DaysOfWeek
class was permeable as it was not a true Enum
, and I had a side goal of also making the enum members be documented.
As a result, this is our finished conversion of that previous enum:
from enumify import enumify, Member
@enumify
class DaysOfWeek:
MONDAY:Member
TUESDAY:Member
WEDNESDAY:Member
And we can see its set value below:
print(DaysOfWeek.MONDAY)
monday
Thus we come to the two use-cases of enumify
:
- Make Enum's whose member's values are the names of the members
- Make Enum's whose members can be documented
How do I use it?
There are two supported "modes" for enumify
, an interactive mode and a static mode. The static mode should be used when you're in environments such as VSCode or PyLint, where they can make use of documentation underneath a particular variable or member.
Interactive mode should be if you are developing or working out of Jupyter Notebook for your enums.
What do I mean by this? Let's compare two examples.
Interactive Mode
With interactive mode, we can not only use the Member
or Mem
notation to specify our self-named member, but we can also document that member with an assign:
@enumify(interactive=True)
class DaysOfWeek:
MONDAY:Member = "The first day of the week"
TUESDAY:Member = "The second day of the week"
WEDNESDAY:Member = "The third day of the week"
And now these members will have affiliated documentation:
DaysOfWeek.MONDAY.__doc__
'The first day of the week'
We can even document values that aren't Member
type by specifying it's documentation last in the assign
@enumify(interactive=True)
class DaysOfWeek:
MONDAY:Member = "The first day of the week"
TUESDAY:int = 0, "Some number"
DaysOfWeek.TUESDAY.__doc__
'Some number'
BUT, they will not have this documentation show when using IDE's such as VSCode. This is where static mode comes in.
Static Mode
With static mode, we can still use the Member
annotation, but we assume that the user will be working out of an IDE and instead rely on how IDE's can annotate variables.
{% include note.html content='If so, it will be impossible for Jupyter/Notebook users to pick up your documentation for the member' %}
Below is our complete DaysOfWeek
example with this:
@enumify(interactive=False)
class DaysOfWeek:
MONDAY:Member
"The first day of the week"
TUESDAY:Member
"The second day of the week"
WEDNESDAY:Member
"The third day of the week"
DaysOfWeek.MONDAY
<DaysOfWeek.MONDAY: 'monday'>
assert DaysOfWeek.MONDAY.__doc__ is None
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file enumify-0.0.1.tar.gz
.
File metadata
- Download URL: enumify-0.0.1.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ddbcbbc6db9f89f7cee5434393a48031df2968e70256802d25529ffe33851c0 |
|
MD5 | 47bae149d4e3836cc17150c90b6373b6 |
|
BLAKE2b-256 | 2971519aafa65022fd1b604bd0b0cdd077a46e4e0f2096491640c15a3afd0fc6 |
File details
Details for the file enumify-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: enumify-0.0.1-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c599b51e3b4ac99d57226eb3ad8cf61294590f795da32a685964664081bec4c |
|
MD5 | 148ff2c40d10b2ba579c6e010211abb7 |
|
BLAKE2b-256 | a0f94bfe44ccd0ba72041ba5b18c25d50138e8ea22fd17952b71629e25c78f07 |