enum, meta, EnumException
Project description
lifehacks.metaclasses ![version](https://pypi-camo.freetls.fastly.net/5a6c44e0933a84addd768d37cc8f3c90b2c311f1/68747470733a2f2f696d672e736869656c64732e696f2f707970692f762f6c6966656861636b732e6d657461636c61737365733f6c6162656c3d)
Structure
📦 lifehacks.metaclasses
├── meta
├── enum
└── EnumException
Installation
pip install lifehacks.metaclasses
Usage
meta
metaclass
There are a few ways to make an meta
class.
from lifehacks.metaclasses import meta
class myenum0(metaclass=meta): ...
@meta # preferred method
class myenum1: ...
@meta() # do not use this method
class myenum2: ...
enum
metaclass
Same with meta
metaclass from above,
there are a few ways to make an enum
class.
from lifehacks.metaclasses import enum
# with typing
class MyPalette0(metaclass=enum[Colour]): ...
@enum # preferred method
class MyPalette1: ...
@enum[Colour] # syntax only allowed python>=3.9
class MyPalette2: ...
@enum() # do not use this method
class MyPalette3: ...
usage example:
from lifehacks.metaclasses import enum
@enum
class BasePalette:
BLACK = ...
WHITE = ...
# extending base palette enum
class SubPalette(BasePalette):
RED = ...
GREEN = ...
# can be used in type hinting
def print_colours(palette:enum) -> None:
for name, value in palette:
print(name, value)
print_colours(BasePalette) # BLACK, WHITE
print_colours(SubPalette) # BLACK, WHITE, RED, GREEN
print(BasePalette.BLACK in SubPalette) # True
enum
classes are not instantiable, more on that below.
EnumException
enum
classes are not instantiable.
If you try to, you get an EnumException
@enum
class Palette: ...
p = Palette() # illegal, raises EnumException
Contributors
- Simon - mightbesimon
- you?
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for lifehacks.metaclasses-1.2.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 336977d9966e5e479761b2790bb5230a5c20ab37492a3af2dbd7c7d79e31b882 |
|
MD5 | 9187bacc5fdb9db9bc88d44dba3ac504 |
|
BLAKE2b-256 | 2bf586fcdc6c7dc85202b86f9b0deea46885975e300ef9b851d717f2b7512588 |
Close
Hashes for lifehacks.metaclasses-1.2.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f2cfa0ef87d05a0924defd006a42f1080b17e8adc630c03c17df0097772e719 |
|
MD5 | 3c783caa1e6b667d0d4cef655e5e1f7f |
|
BLAKE2b-256 | c9f55a8bcb36497e5804bb1574ccd906a6d2291488a0826a9135232a934c5433 |