# fastenum
A roughly 3x faster drop-in replacement of Python's ``enum``.
## Why
Python's [`enum`](https://docs.python.org/3/library/enum.html) 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](https://quantlane.com) 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](https://quantlane.com/blog/fastenum/).
## Installation
`fastenum` is [available on PyPI](https://pypi.org/project/fastenum/) and you can install it with:
```shell
pip install fastenum
```
or
```shell
poetry add fastenum
```
## How to use it
Simply use `fastenum.Enum` instead of `enum.Enum`:
```python
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`:
```ini
[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
```shell
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).
---
![](quantlane.png)
> `fastenum` was made by `Quantlane <https://quantlane.com>`_, a systematic trading firm.
> We design, build and run our own stock trading platform.
Raw data
{
"_id": null,
"home_page": "https://gitlab.com/quantlane/libs/fastenum",
"name": "fastenum",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "Quantlane",
"author_email": "code@quantlane.com",
"download_url": "https://files.pythonhosted.org/packages/4e/88/25ea6231fe54e3f13fc8c495b5d63bf05df812cff10fd1b25ac03d502d84/fastenum-1.1.1.tar.gz",
"platform": null,
"description": "# fastenum\n\nA roughly 3x faster drop-in replacement of Python's ``enum``.\n\n\n## Why\n\nPython's [`enum`](https://docs.python.org/3/library/enum.html) type is a useful building block for creating semantic types and constants in your programs.\n\nThe problem is, if you're doing millions and millions of enum lookups (even simple expressions like `Color.ORANGE`) it's a little bit slow.\n\n`fastenum` is a stripped-down enum implementation that's up to 3.5x faster than the standard library implementation. At [Quantlane](https://quantlane.com) 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](https://quantlane.com/blog/fastenum/).\n\n\n## Installation\n\n`fastenum` is [available on PyPI](https://pypi.org/project/fastenum/) and you can install it with:\n\n```shell\npip install fastenum\n```\n\nor\n\n```shell\npoetry add fastenum\n```\n\n## How to use it\n\nSimply use `fastenum.Enum` instead of `enum.Enum`:\n\n```python\nimport fastenum\n\nclass Color(fastenum.Enum):\n RED = 0\n BLUE = 1\n GREEN = 2\n\nassert isinstance(Color.RED, Color)\nassert Color.RED is Color['RED']\nassert Color.BLUE != 1\nassert Color.GREEN.value == 2\n\ndef is_red(c: Color) -> bool:\n return c is Color.RED\n```\n\nThere is also a mypy plugin that you'll want to enable in `mypy.ini` to help mypy understand `fastenum` just like it understands `enum`:\n\n```ini\n[mypy]\nplugins = fastenum.mypy_plugin:plugin\n```\n\n\n## Tradeoffs and disadvantages\n\nThere 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`.\n\n`fastenum`'s mypy plugin may cause issues with your mypy cache (see above).\n\n\n## Running tests & benchmarks\n\n```shell\npoetry install\npoetry run pytest\n```\n\n## Contributing\n\nPull requests are welcome!\n\nWe will accept pull requests adding missing functionality *provided* they do not impact base `fastenum` performance (it's best to verify that with benchmarks).\n\n---\n\n![](quantlane.png)\n\n> `fastenum` was made by `Quantlane <https://quantlane.com>`_, a systematic trading firm.\n> We design, build and run our own stock trading platform.\n\n",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "Faster drop-in replacement of Python's enum",
"version": "1.1.1",
"project_urls": {
"Homepage": "https://gitlab.com/quantlane/libs/fastenum",
"Repository": "https://gitlab.com/quantlane/libs/fastenum"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "7524746d273c959a6cc086ec3246829cd9804e7337bd6b98447dfc30e1a2db30",
"md5": "2e822f4ea9f7ed363b07408e6ae46f8b",
"sha256": "a7d0b5a50f64300bcf97acdb012c17d7d7f509bc096faa7e58422091fa593e3a"
},
"downloads": -1,
"filename": "fastenum-1.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2e822f4ea9f7ed363b07408e6ae46f8b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10,<4.0",
"size": 13682,
"upload_time": "2024-02-16T09:05:09",
"upload_time_iso_8601": "2024-02-16T09:05:09.622979Z",
"url": "https://files.pythonhosted.org/packages/75/24/746d273c959a6cc086ec3246829cd9804e7337bd6b98447dfc30e1a2db30/fastenum-1.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "4e8825ea6231fe54e3f13fc8c495b5d63bf05df812cff10fd1b25ac03d502d84",
"md5": "c9caac5f1d987f006698c5be7dffd9fb",
"sha256": "1386d445ec03d4c33b80d4300bd044f44d357ea8ad0d347c9e0ed01afbe11a49"
},
"downloads": -1,
"filename": "fastenum-1.1.1.tar.gz",
"has_sig": false,
"md5_digest": "c9caac5f1d987f006698c5be7dffd9fb",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10,<4.0",
"size": 12807,
"upload_time": "2024-02-16T09:05:15",
"upload_time_iso_8601": "2024-02-16T09:05:15.037294Z",
"url": "https://files.pythonhosted.org/packages/4e/88/25ea6231fe54e3f13fc8c495b5d63bf05df812cff10fd1b25ac03d502d84/fastenum-1.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-16 09:05:15",
"github": false,
"gitlab": true,
"bitbucket": false,
"codeberg": false,
"gitlab_user": "quantlane",
"gitlab_project": "libs",
"lcname": "fastenum"
}