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.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9516a44d3f2551d3bccb61fa5a865cf625b1ce61c25a321dd0f092b1d5bc3eaa |
|
MD5 | 4935163a6a1a99b20ba567b1a824b510 |
|
BLAKE2b-256 | c1fc2a103125b4659fa7d52dfccb8b7234bd1032ac6c6fa4c9a9149d3b262c63 |
Close
Hashes for lifehacks.metaclasses-1.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89bbc4ec761c407e5f13b25cd16a6311f424712b0ffdfd5b777dc37c02a02aef |
|
MD5 | 768e19578de39162f46fc378f55f3144 |
|
BLAKE2b-256 | c5a9b1baa7474a3f4f8731b47e38981c29ac720a41d16a30bf819a6b11cf8c8a |