Skip to main content

Python library that extends the functionality of the built-in enum module,

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

enum-extension-1.0.1.tar.gz (9.1 kB view hashes)

Uploaded Source

Built Distribution

enum_extension-1.0.1-py3-none-any.whl (7.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page