Skip to main content

A utility package for extending, subclassing and reusing Enum classes.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

extendable-enum-1.0.1.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

extendable_enum-1.0.1-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file extendable-enum-1.0.1.tar.gz.

File metadata

  • Download URL: extendable-enum-1.0.1.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for extendable-enum-1.0.1.tar.gz
Algorithm Hash digest
SHA256 401558c43873d07d0ed42702c4772e435c92b1c07dfdc9e6a9da4a3daab7b833
MD5 f775ab0353ebb98544d77b760b903fbb
BLAKE2b-256 72c0db529ddfa7d312e292af5fd9a0bf0f2cc2fcfd4d200b11211b156e031517

See more details on using hashes here.

File details

Details for the file extendable_enum-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for extendable_enum-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 56d1d56e798f021d702dd0e19cb5fb31643636c2d0c06d4f8e1bfe7435c35ec7
MD5 800c762b92ccd0b9717f2ed69dd093f3
BLAKE2b-256 e520889c162e1f5c52d99b43c508fd34e0ec6454c8cd48f1e4ad71c9d9cfb4df

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page