Skip to main content

fastenum

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.ORANGE) 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

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

poetry install
poetry run pytest

Contributing

Pull requests are welcome!

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


fastenum was made by Quantlane <https://quantlane.com>_, a systematic trading firm. We design, build and run our own stock trading platform.

Metadata

Release files for fastenum 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastenum 1.1.2
File Size Uploaded
fastenum-1.1.2.tar.gz 12.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastenum 1.1.2
File Interpreter ABI Platform
fastenum-1.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 26.8 kB

Release files / fastenum-1.1.2.tar.gz

Download URL fastenum-1.1.2.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
bd915b797469aa8cf0d96668298e943c3a2bf79df6e6fc8f73e318d43b258dcf
BLAKE2b-256 checksum
How to use checksums
2030ed068bbe6861195bc7795dd9c483506b5bc3e1767c72d1225ef239d80019
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.12 Linux/5.15.0-72-generic

Release files / fastenum-1.1.2-py3-none-any.whl

Download URL fastenum-1.1.2-py3-none-any.whl
Size 13.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca8eb9e282d7d53d7c097a571945661e9e9a6b3772759f7c0bb865699b1671ef
BLAKE2b-256 checksum
How to use checksums
2756757d864bb64fa38cfc0e47a249534df85b42e99624c03c0316ddb83d1d72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.12 Linux/5.15.0-72-generic

Release history Release notifications | RSS feed

This release

1.1.2 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.4

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.1

1 release 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