From c669b15fc927a385c2c33ffe2dbfbde277984f7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sun, 6 Feb 2022 00:02:59 +0100 Subject: [PATCH] better decorator --- PyTeX/format/formatting_config.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/PyTeX/format/formatting_config.py b/PyTeX/format/formatting_config.py index b5840e2..2f4e619 100644 --- a/PyTeX/format/formatting_config.py +++ b/PyTeX/format/formatting_config.py @@ -1,22 +1,29 @@ -from typing import List, Optional, Union +from typing import List, Optional, Union, Any, Tuple from .enums import NamingScheme, License from .generic_text import GenericText -def generate_properties(attr_names): +def generate_properties(attributes: List[Union[str, Tuple[str, Any]]]): + attributes = [ + x if isinstance(x, Tuple) else (x, None) for x in attributes + ] + def decorator(cls): - for attr_name in attr_names: - def get_attr(self, attr_name=attr_name): - return getattr(self, "_" + attr_name) + for [attribute, default_value] in attributes: + def get_attr(self, attribute=attribute, default_value=default_value): + if getattr(self, "_" + attribute) is not None: + return getattr(self, "_" + attribute) + else: + return default_value prop = property(get_attr) - setattr(cls, attr_name, prop) + setattr(cls, attribute, prop) return cls return decorator -@generate_properties(['naming_scheme']) +@generate_properties([('naming_scheme', False), 'license']) class FormattingConfig: def __init__(self): self._naming_scheme: Optional[Union[NamingScheme, str]] = None