Skip to main content

Faster drop-in replacement of Python's enum

Project description

A roughly 3x faster drop-in replacement of Python’s enum.

Why

Python’s enum type is a useful building block for creating semantic types and constants in your programs.

The problem is, if you’re doing millions and millions of enum lookups (even simple expressions like Color.BLUE) it’s a little bit slow.

fastenum is a stripped-down enum implementation that’s up to 3.5x faster than the standard library implementation. At Quantlane we use it in production code as a drop-in replacement. Read more, including benchmarks, on our blog: A 3x faster enum type for Python.

Installation

fastenum is available on PyPI and you can install it with:

pip install fastenum

or

poetry add fastenum

How to use it

Simply use fastenum.Enum instead of enum.Enum:

import fastenum

class Color(fastenum.Enum):
        RED = 0
        BLUE = 1
        GREEN = 2

assert isinstance(Color.RED, Color)
assert Color.RED is Color['RED']
assert Color.BLUE != 1
assert Color.GREEN.value == 2

def is_red(c: Color) -> bool:
        return c is Color.RED

There is also a mypy plugin that you’ll want to enable in mypy.ini to help mypy understand fastenum just like it understands enum:

[mypy]
plugins = fastenum.mypy_plugin:plugin

Note you may experience odd mypy crashes relating to the mypy cache when using this plugin. It’s an unfortunate bug we have not yet tracked down. If you run into it, it is unfortunately necessary to disable the mypy cache by setting cache_dir = /dev/null 🤦‍♂️

Tradeoffs and disadvantages

There is no support for automatic values, unique value checks, aliases, custom __init__ implementations on members, IntEnum, Flag, or the functional API. If you require any of these features it’s probably best to just use enum.

fastenum’s mypy plugin may cause issues with your mypy cache (see above).

Running tests & benchmarks

pip install -r dev-requirements.txt
PYTHONPATH=. cq && pytest

Contributing

Pull requests are welcome! Especially if you know how to fix that pesky mypy cache bug 🙈

We will accept pull requests adding missing functionality provided they do not impact base fastenum performance (it’s best to verify that with benchmarks).


quantlane.png

fastenum was made by Quantlane, a systematic trading firm. We design, build and run our own stock trading platform.

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

fastenum-1.0.4.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

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

fastenum-1.0.4-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file fastenum-1.0.4.tar.gz.

File metadata

  • Download URL: fastenum-1.0.4.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.8.2 requests/2.27.1 setuptools/58.1.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for fastenum-1.0.4.tar.gz
Algorithm Hash digest
SHA256 acf8a8b8a373586db854efc1c5d1d4c2e0fa47cb637ba65c3573611d2a4e4e49
MD5 da4a23182f67168e7d152146d4f209b1
BLAKE2b-256 e42faddf375d7633561117b0705756c1e019f52eb90e15c696bd7ebafc052f03

See more details on using hashes here.

File details

Details for the file fastenum-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: fastenum-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.8.2 requests/2.27.1 setuptools/58.1.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for fastenum-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6ee1a0006201532a2fc2b5ba8556098514edf5c017ebf131e0aec58feecfee74
MD5 4ab19e1e0080eff4bcedcd210097da80
BLAKE2b-256 0912a14bd8542553c27b631b458ecc725ac03a88d45f45fafdb04f250a78fb3b

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