Skip to main content

Better Than Enums

Project description

[ Documentation | Cheatsheet | Changelog ]

Disclaimer!

tl;dr — Expect breaking changes in the future, and pin the version to protect yourself.

This package is very new and has only been used by me as far as I know.

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 Constant which facilitates the creation and usage of immutable enumerated value sets. 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 exceptions are discussed in the docs.

Preview

from simpleset import Constant

# simplest form

Color = Constant.define_set( "Color", "RED", "GREEN", "BLUE" )

Color.RED == "RED"          # -> True  (equality to strings)
Color.RED.cname             # -> "RED"
Color.all                   # -> [ Color.RED, Color.GREEN, Color.BLUE ]
Color.max_length            # -> 5  (useful for a VARCHAR db column)

# more complex

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

Color.RED.hex               # -> "ff0000"
Color.select( like=True  )  # -> [ Color.RED, Color.GREEN ]
Color.get(    like=False )  # -> Color.BLUE

Learn more.


Historical Note: This package was originally named py-objects.

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

simpleset-0.4.1.tar.gz (5.2 kB view hashes)

Uploaded Source

Built Distribution

simpleset-0.4.1-py3-none-any.whl (6.3 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