Skip to main content

Implement namespaced and inheritable metadata at the class level.

Project description

Description

Implement namespaced and inheritable metadata at the class level.

Inspired on Meta classes from Django models and Django Rest Framework model serializers.

Quick Start

Installation

pip install classoptions

Simple inheritance

from classoptions import ClassOptionsMetaclass
from typing import Any

class Pizza(metaclass=ClassOptionsMetaclass):
    _meta: Any
    
    class DefaultMeta:
        client_can_modify = True
        notes = None

    cooking_temp = 400
    cooking_time = 1    

class HawaiianPizza(Pizza):
    ingredients = ["cheese", "ham", "pineapple"]
    
    class Meta:
        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._meta.client_can_modify)
print("Notes:")
print(HawaiianPizza._meta.notes)
print("Private Note:")
print(HawaiianPizza._meta.private_note)

Outputs:

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 ClassOptionsMetaclass
from typing import Any

class A(metaclass=ClassOptionsMetaclass):
    _meta: Any
    class DefaultMeta:
        color = "red"
        size = 2
        hello = "world"
        i_like_pizza = True

class B(A):
    class DefaultMeta:
        color = "blue"
        size = 3

class C(B):
    class Meta:
        size = 4  # Specific to C only

class D(A):
    class DefaultMeta:
        color = "black"
        hello = "country"

class E(D, C):
    class Meta:
        i_like_hawaiian_pizza = "maybe"

print("E custom meta")
print("i_like_hawaiian_pizza:", E._meta.i_like_hawaiian_pizza)

print("\nInherited from B")
print("size:", E._meta.size)

print("\nInherited from D")
print("color:", E._meta.color)
print("hello:", E._meta.hello)

print("\nInherited from A")
print("i_like_pizza:", E._meta.i_like_pizza)

Outputs:

E custom meta
i_like_hawaiian_pizza: maybe

Inherited from B
size: 3

Inherited from D
color: black
hello: country

Inherited from A
i_like_pizza: True

Using other class/attribute names

With ClassOptionsMetaclass.factory you can overwrite how you define default metadata, class specific metadata, and how you access the result.

from classoptions import ClassOptionsMetaclass
from typing import Any

class A(metaclass=ClassOptionsMetaclass.factory("Options", "DefaultOptions", "_options")):
    _options: Any
    
    class DefaultOptions:
        color = "red"
        size = 2

    cooking_temp = 400
    cooking_time = 1    

class B(A):
    class Options:
        color = "blue"
        
print("B color:", B._options.color)
print("B size:", B._options.size)

Outputs:

B color: blue
B size: 2

License

MIT License.

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

classoptions-0.2.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

classoptions-0.2.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file classoptions-0.2.0.tar.gz.

File metadata

  • Download URL: classoptions-0.2.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.13

File hashes

Hashes for classoptions-0.2.0.tar.gz
Algorithm Hash digest
SHA256 aa9b6af1bd92409507e7ae664ab2b6048193e7b452beea51b54ab5ebb27ebc00
MD5 e98e73d7e118e6eba7b60684a9c7c9e1
BLAKE2b-256 09a312f662721a62fe12a0802fcdb5b2c472cab259ce86e67f92d37e3ff24a19

See more details on using hashes here.

File details

Details for the file classoptions-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: classoptions-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.13

File hashes

Hashes for classoptions-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f79e7f86a63978ab9aa3f118cc1e43270120a8aafe5ba84732fb3858374360bb
MD5 6145d0f4f94042e671e3ce45f7f0a085
BLAKE2b-256 fc2b01c90dfe141df167f7504a3e356f01ca7a50bc9047e3694a3f5c8de1ce86

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page