Implement namespaced and inheritable metadata at the class level.
Project description
classoptions
Description
Implement namespaced and inheritable metadata at the class level.
Inspired on Meta classes from Django and Django Rest Framework.
Quick Start
Installation
pip install classoptions
Simple inheritance
In the example, DefaultInfo
holds metadata that is inherited by subclasses, while Info
holds
class specific metadata.
from classoptions import get_options_metaclass
EmployeesInfoMetaclass = get_options_metaclass("EmployeesInfoMetaclass", "Info", "DefaultInfo")
class Pizza(metaclass=EmployeesInfoMetaclass):
class DefaultInfo:
client_can_modify = True
notes = None
cooking_temp = 400
cooking_time = 1
class HawaiianPizza(Pizza):
ingredients = ["cheese", "ham", "pineapple"]
class Info:
notes = "Do not judge the costumer."
private_note = "If planning a party, ask first if people like it."
print("Hawaiian Pizza:")
print("Ingredients:", ", ".join(HawaiianPizza.ingredients))
print("Cooking Temperature (F):", HawaiianPizza.cooking_temp)
print("Cooking Time (hours):", HawaiianPizza.cooking_time)
print("Client can modify:", HawaiianPizza.Info.client_can_modify)
print("Notes:")
print(HawaiianPizza.Info.notes)
print("Private Note:")
print(HawaiianPizza.Info.private_note)
Output:
Hawaiian Pizza:
Ingredients: cheese, ham, pineapple
Cooking Temperature (F): 400
Cooking Time (hours): 1
Client can modify: True
Notes:
Do not judge the costumer.
Private Note:
If planning a party, ask first if people like it.
Multiple Inheritance
Works similar to python inheritance, except we don't need to explicitly inherit from the parent class.
from classoptions import get_options_metaclass
OptionsMetaclass = get_options_metaclass("OptionsMetaclass", "Options", "DefaultOptions")
class A(metaclass=OptionsMetaclass):
class DefaultOptions:
color = "red"
size = 2
hello = "world"
i_like_pizza = True
class B(A):
class DefaultOptions:
color = "blue"
size = 3
class C(B):
class Options:
size = 4 # Specific to C only
class D(A):
class DefaultOptions:
color = "black"
hello = "country"
class E(D, C):
class Options:
i_like_hawaiian_pizza = "maybe"
print("E custom options")
print("i_like_hawaiian_pizza:", E.Options.i_like_hawaiian_pizza)
print("\nInherited from B")
print("size:", E.Options.size)
print("\nInherited from D")
print("color:", E.Options.color)
print("hello:", E.Options.hello)
print("\nInherited from A")
print("i_like_pizza:", E.Options.i_like_pizza)
Output:
E custom options
i_like_hawaiian_pizza: maybe
Inherited from B
size: 3
Inherited from D
color: black
hello: country
Inherited from A
i_like_pizza: True
License
MIT License.
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
classoptions-0.1.tar.gz
(3.7 kB
view hashes)
Built Distribution
Close
Hashes for classoptions-0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7363254afe357cd664d68836e60c6dd4337d2a27e3a35c5a16c08437335ff51 |
|
MD5 | b802c67a5659070de64d70f40074511a |
|
BLAKE2b-256 | 0e4d1d794dd6b2bcea4affa0618512a71f5ed4260f89319e6b8cffda317b8126 |