Conversion#
register_object_objectinfo_rule#
- tkclasswiz.convert.register_object_objectinfo_rule(cls: type, mapping: dict | None = {}, **kwargs)#
Used for adding new conversion rules when converting from Python objects into abstract ObjectInfo objects (GUI objects).
These rules will be used when calling the
convert_to_object_infofunction.If rules for
clsdo not exist, the conversion function will assume parameters are located under the same attribute name.A neat way to avoid registering custom conversion rules, is to store the attributes either under the same name as the parameter, or store them under a different name and create a
@propertygetter which has the same name as the parameter.- Parameters:
mapping (Optional[Dict[str, Union[Callable[[T], Any]], str]]) – Mapping mapping the parameter name to attribute from which to obtain the value. Values of mapping can also be a getter function, that accepts the object as parameter.
kwargs (Optional[Unpack[str, Union[Callable[[T], Any]], str]]) – Keyword arguments mapping parameter name to attribute names from which to obtain the value. Values of mapping can also be a getter function, that accepts the object as parameter.
Example
class FILE: def __init__(self, filename: str): self.fullpath = filename register_object_objectinfo_rule( FILE, filename="fullpath" ) # Timezone import datetime register_object_objectinfo_rule( datetime.timezone, offset=lambda timezone: timezone.utcoffset(None) )
get_object_objectinfo_rule_map#
convert_objects_to_script#
convert_to_object_info#
convert_to_objects#
- tkclasswiz.convert.convert_to_objects(d: ObjectInfo | dict | list) object | dict | List#
Converts
ObjectInfoinstances into actual objects, specified by the ObjectInfo.class_ attribute.- Parameters:
d (ObjectInfo | list[ObjectInfo] | dict) – The object(s) to convert. Can be an ObjectInfo object, a list of ObjectInfo objects or a dictionary that is a mapping of ObjectInfo parameters.
cached (bool) – If True items will be returned from cache. ONLY USE FOR IMMUTABLE USE.
convert_to_dict#
- tkclasswiz.convert.convert_to_dict(d: ObjectInfo | List[ObjectInfo] | Any)#
Converts ObjectInfo into dictionary representation.
convert_from_dict#
ObjectInfo#
- class tkclasswiz.convert.ObjectInfo(class_, data: Mapping, nickname: str | None = None)#
A GUI object that represents real objects. The GUI only knows how to work with ObjectInfo objects, iterables and primitive-like types.
NOTE: If the class has any password like fields, they can be specified by the class
__passwords__attribute, which is an iterable of strings.- Parameters:
- classmethod register_repr(class_: type, repr: Callable[[ObjectInfo], str], inherited: bool = False)#
Registers a custom __repr__ (string representation of object) function. The function must as a single parameter accept the
ObjectInfoinstance being represented as a string.- Parameters:
class (type) – The class for which this custom
repris being register.repr (Callable[[ObjectInfo], str]) – The function that will provide custom
__repr__. As a parameter it accepts theObjectInfoobject. It returns astr(string).inherited (bool) – Boolean flag. Setting it to True will register repr for inherited members as well. Defaults to False.