Simple(r) enums

Overview

basicenum

Simple(r) enums.

basicenum.compat

A (mostly) API-compatible re-implementation of enum.Enum from the stdlib (plus related code).

The goal for this module was to try and re-implement as much of the API of enum.Enum as possible while using modern Python features. While this does lead to some API breakage (e.g. type(enum.member) == type(enum) is no longer true), it mostly revolves around metaclass-level details. If you rely on the surface API for enum.Enum, then this module should be compatible.

API Compatibility

Using the example enum:

class Colour(Enum):
    RED = auto()
    GREEN = auto()
    BLUE = auto()

The compatibility with enum.Enum is:

Feature Supported?
repr(Colour.RED)
type(Colour.RED) (Member instead)
isinstance(Colour.RED, Colour)
iter(Colour)
hash(Colour.RED)
Colour(1)
Colour["RED"]
Colour.RED in Colour
Colour.RED.name
Colour.RED.value
auto()
_generate_next_value_()
Colour.__members__
Colour.RED == Colour.RED
Restricted subclassing
Pickling
Functional API (via create())

Type Checking

Unfortunately, type checkers hard-code their support for enum.Enum. That means they do not recognize members of basicenum.compat.Enum as being instances of Member or matching the API of members of enum.Enum. Auto-complete based on member names should work, though.

Benchmarking

If you install the [benchmark] extra, you can use richbench to see a performance comparison between enum and basicenum.compat.

Results

On a MacBook Pro (Retina, 13-inch, Early 2015):

richbench --repeat 5 --times 5 --benchmark compat benchmarks/
Benchmark Min Max Mean Min (+) Max (+) Mean (+)
import 0.374 0.503 0.422 0.117 (3.2x) 0.120 (4.2x) 0.119 (3.5x)
creation w/ auto() 0.422 0.429 0.425 0.075 (5.7x) 0.077 (5.5x) 0.076 (5.6x)
_generate_next_value_() 0.373 0.383 0.376 0.077 (4.8x) 0.078 (4.9x) 0.078 (4.8x)
creation w/ constants 0.333 0.351 0.339 0.060 (5.5x) 0.066 (5.4x) 0.063 (5.4x)
functional API 0.392 0.397 0.394 0.070 (5.6x) 0.074 (5.4x) 0.072 (5.5x)
isinstance(..., Enum) 0.189 0.191 0.190 0.279 (-1.5x) 0.282 (-1.5x) 0.280 (-1.5x)
iter(Enum) 0.912 0.922 0.917 0.197 (4.6x) 0.199 (4.6x) 0.198 (4.6x)
Enum(...) 0.297 0.297 0.297 0.078 (3.8x) 0.079 (3.8x) 0.078 (3.8x)
Enum[...] 0.121 0.122 0.121 0.075 (1.6x) 0.139 (-1.1x) 0.093 (1.3x)
... in Enum 0.267 0.375 0.308 0.162 (1.6x) 0.164 (2.3x) 0.163 (1.9x)
member access 0.333 0.337 0.334 0.118 (2.8x) 0.121 (2.8x) 0.119 (2.8x)
value access 0.735 0.761 0.744 0.141 (5.2x) 0.143 (5.3x) 0.142 (5.2x)
equality 0.343 0.345 0.344 0.103 (3.3x) 0.107 (3.2x) 0.105 (3.3x)
repr 0.379 0.386 0.382 0.236 (1.6x) 0.238 (1.6x) 0.237 (1.6x)
hashing 0.345 0.355 0.348 0.245 (1.4x) 0.250 (1.4x) 0.247 (1.4x)
pickling 0.270 0.272 0.271 0.270 (-1.0x) 0.274 (-1.0x) 0.272 (-1.0x)
unpickling 0.229 0.232 0.231 0.230 (-1.0x) 0.232 (-1.0x) 0.231 (-1.0x)
__members__ 0.373 0.381 0.376 0.046 (8.1x) 0.047 (8.1x) 0.046 (8.1x)
@unique 0.518 0.523 0.520 0.104 (5.0x) 0.109 (4.8x) 0.106 (4.9x)

Module Contents

Enum

The class to inherit from to create an enum.

Member

The class which all enum members are instances of.

auto()

Function for automatic, incrementing integer member values.

@unique

Guarantee that all enum members have unique values.

Raises ValueError if values are not all unique.

create()

A re-implementation of the functional API of enum.Enum.

def create(
    enum_name, member_names, /, *, module=None, qualname=None, type=None, start=1
): ...
You might also like...

Simple tool to help SOC and other blueteamers/cybersecurity professionals. Does simple commands like ping, nslookup, whois and reputation searches on IPs/URLs using AbuseIPDB and VirusTotal APIs

Simple tool to help SOC and other blueteamers/cybersecurity professionals.  Does simple commands like ping, nslookup, whois and reputation searches on IPs/URLs using AbuseIPDB and VirusTotal APIs

IPURL Tool Table of Contents About Requirements python3 PyQT6 Requests VirusTotal Free API AbuseIPDB Free API How to use Free API Limitations VirusTot

Jul 2, 2022

A simple web3py project deploying and interacting with a simple smart contract.

Basic Sample Web3py Project This project demonstrates a basic Web3py usecase. It comes with a sample contract, a test for that contract, a sample scri

Dec 3, 2022

A simple mac theme calculator build on python tkinter able to perform simple calculative tasks.

A simple mac theme calculator build on python tkinter able to perform simple calculative tasks.

Mac Calculator Python A simple python tkinter calulator having mac calculator theme, which is able to do most of the calculation works like addition,

Aug 14, 2022

Simple Json File Descriptor built using Python that can describe simple or nested JSON file.

Json File Descriptor in Python This is a simple json file descriptor. It can describe the the simple and nested json file. Basic Usage from dot_json i

Aug 29, 2022

Simple-SDW is simple and fast way to use the European Central Bank's Statistical Warehouse Database with python.

Simple-SDW is simple and fast way to use the European Central Bank's Statistical Warehouse Database with python.

simplesdw SimpleSDW is simple and fast way to use the European Central Bank's Statistical Warehouse Data with Python. Simple-SDW SimpleSDW is simple a

Jan 3, 2023

A very simple tool to swap connections between output and input variables in an ONNX graph. Simple Variable Switch for ONNX.

A very simple tool to swap connections between output and input variables in an ONNX graph. Simple Variable Switch for ONNX.

svs4onnx A very simple tool to swap connections between output and input variables in an ONNX graph. Simple Variable Switch for ONNX. https://github.c

Sep 17, 2022

Example of Simple Simple Q-Learning with Reinforcement Learning algorithm for ozone filter to decide on or off according to ozone level.

Filter-with-ReinforcementLearning Example of Simple Simple Q-Learning with Reinforcement Learning algorithm for ozone filter to decide on or off accor

Sep 29, 2022

A simple and extensible object detection evaluator in COCO style.

A simple and extensible object detection evaluator in COCO style.

A simple and extensible object detection evaluator in COCO style Features Extensible user interfaces to deal with different data formats. Support cust

Oct 29, 2022

ZS4IE: A Toolkit for Zero-Shot Information Extraction with Simple Verbalizations

ZS4IE: A Toolkit for Zero-Shot Information Extraction with Simple Verbalizations Please cite the following paper if you use ZS4IE in your research. Bi

Dec 18, 2022
Owner
Brett Cannon
Brett Cannon
A very simple tool that forces a change in the IR Version of an ONNX graph. Simple IR version Changer for ONNX.

sic4onnx A very simple tool that forces a change in the IR Version of an ONNX graph. Simple IR version Changer for ONNX. https://github.com/PINTO0309/

Katsuya Hyodo 2 Oct 11, 2022
A very simple tool that forces a change in the opset of an ONNX graph. Simple Opset Changer for ONNX.

soc4onnx A very simple tool that forces a change in the opset of an ONNX graph. Simple Opset Changer for ONNX. https://github.com/PINTO0309/simple-onn

Katsuya Hyodo 4 May 15, 2022
A very simple tool to rewrite parameters such as attributes and constants for OPs in ONNX models. Simple Attribute and Constant Modifier for ONNX.

sam4onnx A very simple tool to rewrite parameters such as attributes and constants for OPs in ONNX models. Simple Attribute and Constant Modifier for

Katsuya Hyodo 6 May 15, 2022
made a simple Ai assistant That can handle simple things with python(spyder)

Trophy-My-Ai made a simple Ai assistant That can handle simple things with python(spyder) (run those in command prompt/cmt/terminal) For the robot to

TATHAGATA SAU 1 Apr 20, 2022
This is a simple python script that automates a simple repetitive task.

MGS Spotify Automatizer This is a simple python script that automates a simple repetitive task. Task The script copies up to 10k two weeks old tracks

Soufiane Elkhamlichi 2 May 13, 2022
Tools for simple inference testing using TensorRT, CUDA and OpenVINO CPU/GPU and CPU providers. Simple Inference Test for ONNX.

sit4onnx Tools for simple inference testing using TensorRT, CUDA and OpenVINO CPU/GPU and CPU providers. Simple Inference Test for ONNX. https://githu

Katsuya Hyodo 14 Dec 24, 2022
A very simple script that only initializes the batch size of ONNX. Simple Batchsize Initialization for ONNX.

sbi4onnx A very simple script that only initializes the batch size of ONNX. Simple Batchsize Initialization for ONNX. https://github.com/PINTO0309/sim

Katsuya Hyodo 6 Oct 12, 2022
Simple example of a simple slackbot - Slackbotto!

sambotto-slackbotto Sambotto is a simple tutorial-styled slack bot that uses Slack's RTM to respond to in channel messages with some basic actions. Se

Samuele Mattiuzzo 2 Jun 23, 2022
Checker with simple ONNX model structure. Simple Structure Checker for ONNX.

ssc4onnx Checker with simple ONNX model structure. Simple Structure Checker for ONNX. https://github.com/PINTO0309/simple-onnx-processing-tools Key co

Katsuya Hyodo 1 May 27, 2022
Simple tool to combine(merge) onnx models. Simple Network Combine Tool for ONNX.

snc4onnx Simple tool to combine(merge) onnx models. Simple Network Combine Tool for ONNX. https://github.com/PINTO0309/simple-onnx-processing-tools 1.

Katsuya Hyodo 8 Oct 13, 2022