Skip to main content

Enums which are ordered, by int value, and can be compared with both integers and strings.

Project description

A simple extension of IntEnum used to create ordered StrEnums.

What is an OrderedStrEnum?

Simply, OrderdStrEnums are like StrEnum but have an integer value and their string "value" is equal to their name. This means that OrderedStrEnum are ordered (by their integer values) and can be compared with both integers and strings.

For example, if we have an OrderedStrEnum Foo and we want it to contain the strings 'One', 'Two', and 'Three' in order, we can achieve this as follows:

from ordered_str_enum import OrderedStrEnum

class Foo(OrderedStrEnum):
  One = 1
  Two = 2
  Three = 3

This now gives us the power to compare however we wish, even without case sensitivity

>>> Foo.One == 1
True
>>> Foo.One < 'three'
True
>>> Foo.One <= 'Two'
True
>>> Foo.One > 2
False

We can also get the Enum using the int value or string name

>>> Foo(1)
<Foo.One: 1>
>>> Foo('one')
<Foo.One: 1>
>>> Foo('ONE')
<Foo.One: 1>

It's that simple.

Installation

pip install ordered-str-enum

Then import

from ordered_str_enum import OrderedStrEnum

Check out the source code.

Intersecting OrderedStrEnum

What happens when two different inheritors of OrderedStrEnum intersect (share common entries)?

OrderedStrEnum inherits from IntEnum so they would be compared by their integer values. Though, considering this class intends to add string comparisons to the mix it could lead to confusion. For example if Foo.A is 1 but Bar.A is -1 the result may not be inherently obvious when comparing Foo.A with Bar.A. It would not be unlikely to expect Foo.A == Bar.A to return True since they both can be considered equivalent with the strings 'A' and 'a', but his is not the case as 1 != -1 and so you'd actually get False. This is the result when comparing the Enums as children of IntEnum.

To prevent this confusion, comparison between different children of OrderedStrEnum is disabled by default. Each inheriting class has an internal flag marking them as "strict". Comparison between different inheritors will raise a TypeError unless both have this flag set to False:

class Foo(OrderedStrEnum):
  A = 1

class Bar(OrderedStrEnum):
  A = 1

Foo.strict()
# True
Bar.strict()
# True

Foo.A == Bar.A
# raises TypeError

Both need to set strict to False

class Foo(OrderedStrEnum, strict=False):
  A = 1

class Bar(OrderedStrEnum, strict=False):
  A = 1

Foo.strict()
# False
Bar.strict()
# False
Foo.A == Bar.A
# True

Why does OrderedStrEnum exist?

I decided to make this reusable when creating Enums for the days of a week in another project. See the days of the week implementation: coming soon...

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

ordered_str_enum-0.1.3.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ordered_str_enum-0.1.3-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file ordered_str_enum-0.1.3.tar.gz.

File metadata

  • Download URL: ordered_str_enum-0.1.3.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ordered_str_enum-0.1.3.tar.gz
Algorithm Hash digest
SHA256 db7c5b19c57e83cbac3274a989761233cbe830d63e3a6abb049f4b72cd734249
MD5 5871f8155e70354bbddf79cc2dc7397f
BLAKE2b-256 90a63f5f94b04ea2ab90a2e1e793a0dd8de930ddac3551e5b4a975947fd6eb86

See more details on using hashes here.

File details

Details for the file ordered_str_enum-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for ordered_str_enum-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 708c6084af56840caf7d5a46dfe3c53b88c01e73d82d604ae9ec53092bc3dd9c
MD5 059f535a4b876ebbdffc3579f62b14f0
BLAKE2b-256 eaef5ef61a0de07c271289dd678f012c0c69797ed1524c9122812d914105e8b3

See more details on using hashes here.

Supported by

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