Skip to content

Instantly share code, notes, and snippets.

@rokity
Created October 3, 2023 15:51
Show Gist options
  • Save rokity/e617582e34f33f1041978245435c0717 to your computer and use it in GitHub Desktop.
Save rokity/e617582e34f33f1041978245435c0717 to your computer and use it in GitHub Desktop.
DataClasses+Decorator+Metadata
from dataclasses import dataclass, field
@dataclass
class SalesData:
sales_figures: list = field(metadata={"description": "List of sales figures"})
date: str = field(metadata={"description": "Date of the sales data"})
region: str = field(metadata={"description": "Sales region"})
def log_metadata(func):
def wrapper(data):
for field_name, field_value in data.__dataclass_fields__.items():
print(f"Field: {field_name}")
print(f"Description: {field_value.metadata.get('description', 'N/A')}")
return func(data)
return wrapper
@log_metadata
def process_sales(data):
# Your data processing logic goes here
pass
sales_data = SalesData(
sales_figures=[1000, 1500, 1200],
date="2023-10-03",
region="North"
)
process_sales(sales_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment