Skip to content

Instantly share code, notes, and snippets.

@travishen
Created May 18, 2022 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travishen/5273057cc920974f65fc996ae14359ca to your computer and use it in GitHub Desktop.
Save travishen/5273057cc920974f65fc996ae14359ca to your computer and use it in GitHub Desktop.
class EntityMeta(type):
"""Metaclass for business entities with validated fields"""
def __init__(cls, name, bases, attr_dict):
# handover to type.__init__
super().__init__(name, bases, attr_dict)
for key, attr in attr_dict.items():
if isinstance(attr, Validated):
type_name = type(attr).__name__
attr.storage_name = f'_{type_name}#{key}'
class Entity(metaclass=EntityMeta):
"""Shortcut class for direct inheritance."""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment