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.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8235c620777328be8d6398f13ae801cb78cb7e7aa0a55b462cebdaa5dd4bd34f |
|
MD5 | 4ff094cbe955e849d0e6f9694f5aaf2b |
|
BLAKE2b-256 | c15832acc85704f2d84087411a104b799b1f0ea4e082e6b0b44a6b0fe0f404af |
Close
Hashes for lifehacks.metaclasses-1.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | add887718175695e57ac4017144c7c2e07e6201e1fbf9977408ede560e9e4929 |
|
MD5 | 8c90061ff98187334a0883e120a8bafa |
|
BLAKE2b-256 | 84b4c34c6322ec21a84987aea5197f679289d1b391906f0a48ab6783b0f808a7 |