Skip to main content

Extendable Enum

extendableenum is a simple package for extending, subclassing and reusing python Enum classes. It contains four class decorators and a single class:

  1. inheritable_enum - Enum class decorator. Makes the class and all its members inheritable. Subclassing enums can define new members while still having access to the base class members.
  2. auto_null_member - Enum class decorator. Automatically adds the null member to an enum class. If the class doesn't define any other enum members, also becomes an inheritable enum. The auto null member name and value can be set with the set_auto_null function. Default name/value is NULL/None.
  3. post_mixin_enum - Enum class decorator. Adds the decorated class as a base class to an existing enum. Allows extending an enum class with methods after creation.
  4. copy_enum_members - Enum class decorator. Creates a new and distinct enum class with the same members as an existing enum. New enum can optionally define additional members.
  5. AutoNullEnum - A base class for enum classes using the auto null feature. Contains convenience functions for dealing with the auto null member.

Documentation

Full documentation for the package can be found on read the docs

Installation

Install with pip:

pip install extendable-enum

Requirements

  • Python 3.6+
    • This version was auto-detected by the vermin package. This is yet to be verified.

Examples

Basic usage and examples can be found here. For in depth behaviour and advanced usage, see the docs.

Inheritable Enums

from enum import Enum
from extendableenum import inheritable_enum

@inheritable_enum
class Base(Enum):
  A = 1
  B = 2

# Subclass Base, define new members and an aliases.
class Derived(Base):
  ALIAS_B = 2
  C = 3
  D = 4

Base.restore() # Base now behaves as if it were never decorated.

Auto Null Member

Create An Enum with the Null Member

from enum import Enum
from extendableenum import auto_null_member, set_auto_null

# optional: specify null member name and value
set_auto_null('NULL', None) # this is the default name/value

@auto_null_member
class Fruit(Enum):
  APPLE = 1
  BANANA = 2
  PEAR = 3

print(f'{Fruit.NULL.name}, {Fruit.NULL.value}')
>>> NULL, None

Create a Custom Base Class for Auto Null Enums

from enum import Enum
from extendableenum import auto_null_member

@auto_null_member
class MyAutoNullEnum(Enum):
  # No enum members defined, MyAutoNullEnum is inheritable.
  def some_method(self):
    print(f'My name is {self.name} and my value is {self.value}')

class Fruit(MyAutoNullEnum):
  APPLE = 1
  BANANA = 2
  PEAR = 3

Fruit.APPLE.some_method()
>>> My name is APPLE and my value is 1
Fruit.NULL.some_method()
>>> My name is NULL and my value is None

*** move the below to the docs. ***
# With decorator
@auto_null_enum
class Decorated(MyAutoNullEnum):
  A = 1
  B = 2
  
  # override some_method
  def some_method(self):
    print(f'{self.name}:{self.value}')

assert(Decorated.NULL is not MyAutoNullEnum.NULL)
Decorated.A.some_method()
>>> A:1
Decorated.NULL.some_method()
>>> NULL:None
# access MyAutoNULLEnum.NULL using super()
super(Decorated, Decorated).NULL.some_method()
>>> My name is NULL and my value is None

Post Mixin Enum

Extending an Externally Defined Enum

from enum import Enum
from some_library import SomeEnum
from extendableenum import post_mixin_enum

@post_mixin_enum(SomeEnum)
class MyMixin(Enum):
  def my_new_mixin(self):
    print('This is my new mixin function!')

# SomeEnum is now a subclass of MyMixin!
assert(isinstance(SomeEnum.MEMBER, MyMixin))
SomeEnum.MEMBER.my_new_mixin()
>>> This is my new mixin function!

Copy Enum Members

Extend an Existing Enum with Additional Name/Values

from enum import Enum
from extendableenum import copy_enum_members

class AmpVolume(Enum):
   MUTE = 0
   QUIET = 1
   NORMAL = 5
   LOUD = 7
   TOOLOUD = 10
   
@copy_enum_members(AmpVolume)
class BetterAmpVolume(Enum):
  METAL = 10      # Alias for TOOLOUD
  SPINALTAP = 11
  
print(repr(BetterAmpVolume.METAL))
>>> <BetterAmpVolume.TOOLOUD: 10>

Contributing

Pull requests are always welcome! For any additional features or major changes, please open an issue for discussion.

This is my first public project, so any comments, suggestions, and feedback are also welcome!

Acknowledgements

Thanks to StackOverflow users Ethan Furman, jsbueno and l4mpi for inspiration, help and some code examples (see this question if you are curious).

License

GNU GPL v 3

Metadata

Release files for extendable-enum 1.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 extendable-enum 1.0.1
File Size Uploaded Provenance Actions
extendable-enum-1.0.1.tar.gz 19.3 kB Without provenance Details for extendable-enum-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for extendable-enum 1.0.1
File Interpreter ABI Platform Actions
extendable_enum-1.0.1-py3-none-any.whl Python 3 none any Details for extendable_enum-1.0.1-py3-none-any.whl

Total release size: 39.1 kB

Release files / extendable-enum-1.0.1.tar.gz

Download URL extendable-enum-1.0.1.tar.gz
Size 19.3 kB
Tags Source
SHA-256 checksum
How to use checksums
401558c43873d07d0ed42702c4772e435c92b1c07dfdc9e6a9da4a3daab7b833
BLAKE2b-256 checksum
How to use checksums
72c0db529ddfa7d312e292af5fd9a0bf0f2cc2fcfd4d200b11211b156e031517
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Release files / extendable_enum-1.0.1-py3-none-any.whl

Download URL extendable_enum-1.0.1-py3-none-any.whl
Size 19.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
56d1d56e798f021d702dd0e19cb5fb31643636c2d0c06d4f8e1bfe7435c35ec7
BLAKE2b-256 checksum
How to use checksums
e520889c162e1f5c52d99b43c508fd34e0ec6454c8cd48f1e4ad71c9d9cfb4df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Security for extendable-enum 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

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