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 details)

Uploaded Source

Built Distribution

simpleset-0.4.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file simpleset-0.4.1.tar.gz.

File metadata

  • Download URL: simpleset-0.4.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.4 Linux/6.5.0-35-generic

File hashes

Hashes for simpleset-0.4.1.tar.gz
Algorithm Hash digest
SHA256 74c24281de76756f32d91884ec91cb48db63bd9080d46eae4ec0c3e4bdd9f102
MD5 92415a413169cc152a11c5b96022278d
BLAKE2b-256 f4003d0e802d9cf23ea4d91d12cea5f914a2e0bf0e5806721583980a6145b86b

See more details on using hashes here.

File details

Details for the file simpleset-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: simpleset-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.4 Linux/6.5.0-35-generic

File hashes

Hashes for simpleset-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 021968963cb1c9760714b2d750b7a7646a6467ddc37a1e2d1449f4857b38542b
MD5 674988c23c5f037e7a4c045458979961
BLAKE2b-256 1a84a330b9bd3d1a88dca22471bc252b67a896477b81c91e481275fee499cfc3

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