GithubHelp home page GithubHelp logo

fastenum's Introduction

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


image

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

fastenum's People

Contributors

pbabics avatar vitawasalreadytaken avatar zagor avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.