Skip to main content

opinionated 'enum' implementation.

Project description

vistenum

The 'vistenum' package provides an opinionated 'enum' implementation.

Usage

Installation

Available from PyPI:

pip install vistenum

This package provides an opinionated 'enum' implementation through the base enum class VistEnum and the auto function. Below is a brief example implementing a weekday enumeration:

"""Weekday provides an enumeration of the weekdays."""
#  AGPL-3.0 license
#  Copyright (c) 2024 Asger Jon Vistisen
from vistenum import VistEnum, auto


class Weekday(VistEnum):
  """Weekday provides an enumeration of the weekdays."""
  MONDAY = auto()
  TUESDAY = auto()
  WEDNESDAY = auto()
  THURSDAY = auto()
  FRIDAY = auto()
  SATURDAY = auto()
  SUNDAY = auto()

Enum Attributes

The VistEnum class provides the following attributes:

  • name: The name of the enum member. Automatically set to the value as it appears in the class body.
  • value: (Optional) The public value of the enum member. If not provided, the name of the enum member is used as the public value.

An internally managed, integer valued private attribute is used for logic implementation. The name of the enum member is case-insensitive and is set to the name as it appears in the class body.

Human Readable Representation

VistEnum implements a custom __str__ method such that str(Weekday.MONDAY) actually returns 'Weekday.MONDAY'. Of course, this implementation omits the public value. Inclusion of the public value depends on the includeValue attribute on the enum class. This class attribute is implemented on the metaclass level, allowing it to be changed dynamically at runtime.

(Please note that this behaviour must be implemented on the metaclass level for the value to be shared between all instances of the class. In general, runtime changes to class attributes should be avoided.)

Below is an example of runtime toggling of public value inclusion:

"""Weekday provides an enumeration of the weekdays."""
#  AGPL-3.0 license
#  Copyright (c) 2024 Asger Jon Vistisen
from vistenum.example import Weekday

if __name__ == '__main__':
  print(Weekday.includeValue, Weekday.MONDAY)
  Weekday.includeValue = True
  print(Weekday.includeValue, Weekday.MONDAY)

The above code outputs:

False, Weekday.MONDAY
True, Weekday.MONDAY(MONDAY)

Please note that the default behaviour is to omit inclusion of the public value. For the Weekday class, the public value is the name of the enum member making inclusion redundant. In other situations, the public value could belong to a class without a custom __str__ implementation, making inclusion of the public value cluttered and unreadable.

Iteration Protocol

VistEnum implements the iteration protocol, allowing for iteration over the enum members. The iteration order is the order of the enum members as they appear in the class body. The iteration protocol is demonstrated in the example below:

"""Weekday provides an enumeration of the weekdays."""
#  AGPL-3.0 license
#  Copyright (c) 2024 Asger Jon Vistisen
from vistenum.example import Weekday

if __name__ == '__main__':
  for weekday in Weekday:
    print(weekday)  # VistEnum provides a readable __str__ reimplementation

The above code outputs:

Weekday.MONDAY
Weekday.TUESDAY
Weekday.WEDNESDAY
Weekday.THURSDAY
Weekday.FRIDAY
Weekday.SATURDAY
Weekday.SUNDAY

Hashing

The VistEnum class provides hashing allowing enum members to be used as dictionary keys. For example:

"""Ugedag provides a translation of the weekdays to Danish."""
#  AGPL-3.0 license
#  Copyright (c) 2024 Asger Jon Vistisen
from __future__ import annotations
from vistenum.example import Weekday

if __name__ == '__main__':
  weekly = {Weekday.Monday : 'Family Home Evening',
            Weekday.Tuesday: 'None', Weekday.Wednesday: 'None', }
  print(weekly[Weekday.Monday])

The above code outputs:

Family Home Evening

Public Value

The enum members of Weekday used the default public value which is the name of the member. However, the public value can be set explicitly to any value of any type as it is entirely semantic with respect to the vistenum package. In the example below, the Weekday class is again implemented, but this time with explicit public values:

"""Ugedag provides a translation of the weekdays to Danish."""
#  AGPL-3.0 license
#  Copyright (c) 2024 Asger Jon Vistisen
from __future__ import annotations

from vistenum import VistEnum, auto


class Ugedag(VistEnum):
  """Ugedag provides a translation of the weekdays to Danish."""
  includeValue = True
  MONDAY = auto('Mandag')
  TUESDAY = auto('Tirsdag')
  WEDNESDAY = auto('Onsdag')
  THURSDAY = auto('Torsdag')
  FRIDAY = auto('Fredag')
  SATURDAY = auto('Lørdag')
  SUNDAY = auto('Søndag')

Please note that the Ugedag class sets includeValue to True, meaning that the public value is included in the string representation of the enum members. The public value is set to the Danish translation of the weekdays:

"""Ugedag provides a translation of the weekdays to Danish."""
#  AGPL-3.0 license
#  Copyright (c) 2024 Asger Jon Vistisen
from __future__ import annotations

from vistenum.example import Ugedag

if __name__ == '__main__':
  for ugedag in Ugedag:
    print(ugedag)
  print("""Toggle public value inclusion off""")
  Ugedag.includeValue = False  # Toggle public value inclusion
  for ugedag in Ugedag:
    print(ugedag)

The above code outputs:

Ugedag.MONDAY(Mandag)
Ugedag.TUESDAY(Tirsdag)
Ugedag.WEDNESDAY(Onsdag)
Ugedag.THURSDAY(Torsdag)
Ugedag.FRIDAY(Fredag)
Ugedag.SATURDAY(Lørdag)
Ugedag.SUNDAY(Søndag)
Toggle public value inclusion off
Ugedag.MONDAY
Ugedag.TUESDAY
Ugedag.WEDNESDAY
Ugedag.THURSDAY
Ugedag.FRIDAY
Ugedag.SATURDAY
Ugedag.SUNDAY

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

vistenum-0.1.5.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

vistenum-0.1.5-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file vistenum-0.1.5.tar.gz.

File metadata

  • Download URL: vistenum-0.1.5.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for vistenum-0.1.5.tar.gz
Algorithm Hash digest
SHA256 22d784c78d0edd63000e744434b8f5169872ad93cba39585bb49d9b38caa922a
MD5 669c263e1dd5353e5698109d08a56b78
BLAKE2b-256 e36aadc2cbe6f787942ad1897dfbc129c512f223a47b7bfdd259482ada0f5041

See more details on using hashes here.

File details

Details for the file vistenum-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: vistenum-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for vistenum-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 dd7b8d27bd2e964b41cf472760a8208d917f011a7e74db5ac3ff24a572ac3d0c
MD5 1a0ccafa2e9638703ef8b450ac7aedc8
BLAKE2b-256 7e07f054a213d9b695fb0fe9c7421bd79acc79662463b99e7d9f1d4053cc4a82

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