Skip to main content

enum, meta, EnumException

Project description

lifehacks.metaclasses version

sponsor publish

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

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

lifehacks.metaclasses-1.2.4.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

lifehacks.metaclasses-1.2.4-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file lifehacks.metaclasses-1.2.4.tar.gz.

File metadata

  • Download URL: lifehacks.metaclasses-1.2.4.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for lifehacks.metaclasses-1.2.4.tar.gz
Algorithm Hash digest
SHA256 701041d18a3f4b2b28249e8ead70f7d61c6e8f66eb2049ef6afa4c876b0be87d
MD5 9a18c658e3056582529bfb5a0143980b
BLAKE2b-256 0afcbf48674af8a5ad44d617f4da54269b210cfcfc20a6e1d703cf0287a372bc

See more details on using hashes here.

File details

Details for the file lifehacks.metaclasses-1.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for lifehacks.metaclasses-1.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 db058ab78fe419cdeff0554050e9be6e5e10ec9760d2aefd04fcdaf5510c9189
MD5 bb411d5ef3d67ab170e9e4b4d5d623f0
BLAKE2b-256 0517335515c8502aa815ed0b96df9d8e54edfe8bc1874fdadbf56368960a0884

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