Skip to main content

Better Than Enums

Project description

[ Documentation | Changelog ]

Disclaimer!

This package is very new and has only be used by me on a handful of projects.

When others start using it in different contexts, providing useful feedback on the quality of the API, and guiding its evolution to a mature 1.0 release, I will then offer API stability guarantees and adhere to SemVer.

In the meantime, I see no harm in using it in production given how simple it is. However, please make sure to pin your package dependency to an exact version so your code doesn't break if and when I decide to change something.

Synopsis

I dislike Python enums. They behave strangely and are difficult to build on top of or extend. This package provides a Python class named Object which facilitates the creation and usage of enumerated values. This plain old Python class is both simpler to understand and more powerful/flexible then native enums.

Even a Python beginner should be able to understand most of the source code. The only notable exception is the Object.define() class method which uses type() to construct a subclass of itself.

Preview

from objects import Object

# simplest form

Color = Object.define( "Color", "RED", "GREEN", "BLUE" )

assert Color.RED == "RED"
Color.RED.canonical_name    # "RED"
Color.all                   # [ Color.RED, Color.GREEN, Color.BLUE ]

# more complex

Color = Object.define(
    "Color",
    RED   = dict( hex="ff0000", like=True  ),
    GREEN = dict( hex="00ff00", like=True  ),
    BLUE  = dict( hex="0000ff", like=False ),
)

assert Color.max_length == 5        # useful for a VARCHAR db column
assert Color.select( like=True )    # [ Color.RED, Color.GREEN ]
assert Color.get( like=False )      # Color.BLUE

Learn more.

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

py_objects-0.1.0.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distribution

py_objects-0.1.0-py3-none-any.whl (4.6 kB view hashes)

Uploaded Python 3

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