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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file enum-extension-1.0.1.tar.gz.

File metadata

  • Download URL: enum-extension-1.0.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for enum-extension-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f7963a29e7d2c03f46805482579449ce066a6f2934e51a40ffb5ed4948f98cec
MD5 b1992dc95516570c55a8a2bdb7f7e628
BLAKE2b-256 f31dbf4da599ffdf58eca2a0e609e15df9f919d94f81ae1935c846df5957c514

See more details on using hashes here.

File details

Details for the file enum_extension-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for enum_extension-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fc39287013dfb321869bb3b666444856d32f802e7c768c9b1e6f76a757c926e3
MD5 657377795fb5f2fdbd53e66705f88c95
BLAKE2b-256 900eb8807a19550112bfcb173fbb520f2a6b73fe2e0c89031a1129f14dfcff5e

See more details on using hashes here.

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