Skip to main content

Enum Extension

Python Version Licence GitHub Workflow Status Code Formatting Code Styling Code Styling Code Styling GitHub last commit GitHub top language

enum-extension is a Python library that extends the functionality of the built-in enum module, offering an array of additional features and utilities to simplify enumeration handling and expand its capabilities.

Installation

You can install enum-extension using pip:

pip install enum-extension

Description

The enum-extension library enhances the native enum module in Python, providing additional functionality for enumerations. It introduces the StringEnum class, which inherits from the standard enum.Enum and extends its capabilities. With enum-extension, you can perform bitwise operations such as OR, AND, and NOT on string type enum values, combine multiple enums into a single variable, and easily check for membership within combined enums.

Usage

Here are some examples of how to use StringEnum:

from enum_extensions import StringEnum

# Create an enum by inheriting from StringEnum
class PaymentMethod(StringEnum):
    VISA = "VISA"
    MASTERCARD = "MASTERCARD"
    APPLE_PAY = "APPLE_PAY"
    GOOGLE_PAY = "GOOGLE_PAY"
    VENMO = "VENMO"
    PAYPAL = "PAYPAL"

# Use | to combine two enums into one
CARDS = PaymentMethod.MASTERCARD | PaymentMethod.VISA   # PaymentMethod.MASTERCARD|VISA

# Use ~ to get the opposite values of an enum
WALLETS = ~CARDS   # PaymentMethod.APPLE_PAY|GOOGLE_PAY|PAYPAL|VENMO

# Use & operator to check for membership of an enum in the combined enum
PaymentMethod.MASTERCARD & CARDS  # True
PaymentMethod.VISA & CARDS        # True
PaymentMethod.VENMO & CARDS       # False

# Check membership using a string value with & operator (case-insensitive)
CARDS & "Visa"   # True
CARDS & "visa"   # True

# Check string with enum value as a comparison
PaymentMethod.VISA & "visa"    # True
PaymentMethod.PAYPAL & "VISA"  # False

# Use comparison operator to check for equality
PaymentMethod.VISA == "visa"   # True
PaymentMethod.PAYPAL == "VISA"  # False

paypal1 = PaymentMethod.PAYPAL
paypal2 = PaymentMethod.PAYPAL
paypal1 == paypal2    # True

# Create multiple enum classes and merge their values
class CardProvider(StringEnum):
    VISA = "VISA"
    MASTERCARD = "MASTERCARD"

class WalletProvider(StringEnum):
    VENMO = "VENMO"
    PAYPAL = "PAYPAL"

CARD_PROVIDERS = CardProvider.MASTERCARD | CardProvider.VISA      # CardProvider.MASTERCARD|VISA
WALLET_PROVIDERS = WalletProvider.VENMO | WalletProvider.PAYPAL   # WalletProvider.PAYPAL|VENMO

# Merge two separate StringEnum class values
ALL_METHODS = CARD_PROVIDERS | WALLET_PROVIDERS    # CardProvider_WalletProvider.MASTERCARD|PAYPAL|VENMO|VISA

# Check for membership with the merged enum
WalletProvider.VENMO & WALLET_PROVIDERS    # True
CardProvider.MASTERCARD & ALL_METHODS      # True

# Please note that the merge and combine operations do not modify the original enum definitions but create new enum values.

If you have any suggestions or feature requests, please feel free to contribute or reach out.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Hope you find enum-extension helpful and convenient for working with enumerations in Python.

If you encounter any issues or have any questions, please don't hesitate to open an issue on GitHub.

This package is owned and maintained by m-ali-ubit.

Release files for enum-extension 1.0.1

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

Source distribution (sdist)

Source distribution for enum-extension 1.0.1
File Size Uploaded
enum-extension-1.0.1.tar.gz 9.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for enum-extension 1.0.1
File Interpreter ABI Platform
enum_extension-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.4 kB

Release files / enum-extension-1.0.1.tar.gz

Download URL enum-extension-1.0.1.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f7963a29e7d2c03f46805482579449ce066a6f2934e51a40ffb5ed4948f98cec
BLAKE2b-256 checksum
How to use checksums
f31dbf4da599ffdf58eca2a0e609e15df9f919d94f81ae1935c846df5957c514
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / enum_extension-1.0.1-py3-none-any.whl

Download URL enum_extension-1.0.1-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc39287013dfb321869bb3b666444856d32f802e7c768c9b1e6f76a757c926e3
BLAKE2b-256 checksum
How to use checksums
900eb8807a19550112bfcb173fbb520f2a6b73fe2e0c89031a1129f14dfcff5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

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