enum, meta, EnumException
Project description
lifehacks.metaclasses
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.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc24465d4a801ce225cac604299c6ea6cf89d990f82329deced93b654c68f1cb |
|
MD5 | 659a3df4613207f463e5bfe0d3c2f095 |
|
BLAKE2b-256 | be7bb82051bb8412db0398ce9d0b13d616271fabfb45e5db3c4cd0e0de87c826 |
Close
Hashes for lifehacks.metaclasses-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb90216ddb1a531030b2c5290bb703aa210865f7434737cc8a4db9a94b6b62d5 |
|
MD5 | bf8029e8bcaacdd2a8c63be02bbf5e4d |
|
BLAKE2b-256 | 6f3405e53d757c1a9e786654fa4a80bc18cde0c0a2c2b4d6f5400e76964ff8ed |