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_info function.

If rules for cls do 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 @property getter 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#

tkclasswiz.convert.get_object_objectinfo_rule_map(cls: type) dict#

Returns the conversion mapping for cls

Returns:

Mapping of parameter name to the attribute name or getter function.

Return type:

Dict[str, Union[Callable[[T], Any]], str]

convert_objects_to_script#

tkclasswiz.convert.convert_objects_to_script(object: ObjectInfo | list | tuple | set | str)#

Converts ObjectInfo objects into equivalent Python code.

convert_to_object_info#

tkclasswiz.convert.convert_to_object_info(object_: object, **kwargs)#

Converts an object into ObjectInfo.

Parameters:

object (object) – The object to convert.

convert_to_objects#

tkclasswiz.convert.convert_to_objects(d: ObjectInfo | dict | list) object | dict | List#

Converts ObjectInfo instances 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#

tkclasswiz.convert.convert_from_dict(d: dict | List[dict] | Any) ObjectInfo#

Converts previously converted dict back to ObjectInfo.

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:
  • class (type) – Real object’s type.

  • data (dict) – Dictionary mapping to real object’s parameters

  • nickname (Optional[str]) – Add a nickname to the defined object for easier recognition.

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 ObjectInfo instance being represented as a string.

Parameters:
  • class (type) – The class for which this custom repr is being register.

  • repr (Callable[[ObjectInfo], str]) – The function that will provide custom __repr__. As a parameter it accepts the ObjectInfo object. It returns a str (string).

  • inherited (bool) – Boolean flag. Setting it to True will register repr for inherited members as well. Defaults to False.