Created
October 3, 2023 15:51
-
-
Save rokity/e617582e34f33f1041978245435c0717 to your computer and use it in GitHub Desktop.
DataClasses+Decorator+Metadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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