Skip to main content

Xenum offers a simple alternative to Python 3 enums that’s especially useful for (de)serialization. When you would like to model your enum values so that they survive jumps to and from JSON, databases, and other sources cleanly as strings, Xenum will allow you to do so extremely easily.

Installation

Installation is simple. With python3-pip, do the following:

$ sudo pip install -e .

Or, to install the latest version available on PyPI:

$ sudo pip install xenum

Usage

Just create a basic class with attributes and annotate it with the @xenum attribute. This will convert all of the class attributes into Xenum instances and allow you to easily convert them to strings via str(MyEnum.A) and from strings via MyEnum.by_name("MyEnum.A").

Example

from xenum import xenum

@xenum
class Colors:
  RED = 1
  GREEN = 2
  BLUE = 3

# convert to a string
str_value = str(Colors.RED)
# convert from a string
enum_value = Colors.by_name(str_value)

if Colors.RED == enum_value:
  print('We did it!')

Checkout test.py in the git repo for more usage examples.

Change Log

Version 1.2: September 4th, 2016

  • Add ‘values()’ method to @xenum annotated classes for fetching an ordered sequence of the Xenum entities created.

Version 1.1: August 31st, 2016

  • Made Xenum instances hashable, removed value() as a function.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xenum-1.2.tar.gz (3.9 kB view details)

Uploaded Source

File details

Details for the file xenum-1.2.tar.gz.

File metadata

  • Download URL: xenum-1.2.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for xenum-1.2.tar.gz
Algorithm Hash digest
SHA256 f0308a6ec4e8348a9cfc6974945fd91126837bb420bbb12bd19f92f91f4aa511
MD5 bc31552bf1f6197cb43f850728662ca4
BLAKE2b-256 a17997f4d5fb54055f66470c17d5379b6d04dcf53301274b2929aa2aedc55c4e

See more details on using hashes here.

Release history Release notifications | RSS feed

1.4

1 file

1.3

1 file

This release

1.2 This release

1 file

1.1

1 file

1.0

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page