Skip to main content

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

Metadata

Release files for enumify 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for enumify 0.0.1
File Size Uploaded
enumify-0.0.1.tar.gz 12.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for enumify 0.0.1
File Interpreter ABI Platform
enumify-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 22.3 kB

Release files / enumify-0.0.1.tar.gz

Download URL enumify-0.0.1.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1ddbcbbc6db9f89f7cee5434393a48031df2968e70256802d25529ffe33851c0
BLAKE2b-256 checksum
How to use checksums
2971519aafa65022fd1b604bd0b0cdd077a46e4e0f2096491640c15a3afd0fc6
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / enumify-0.0.1-py3-none-any.whl

Download URL enumify-0.0.1-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c599b51e3b4ac99d57226eb3ad8cf61294590f795da32a685964664081bec4c
BLAKE2b-256 checksum
How to use checksums
a0f94bfe44ccd0ba72041ba5b18c25d50138e8ea22fd17952b71629e25c78f07
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page