better decorator
This commit is contained in:
parent
1688463e96
commit
c669b15fc9
1 changed files with 14 additions and 7 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue