pymasep.utils ============= .. py:module:: pymasep.utils .. autoapi-nested-parse:: Miscellaneous utility functions Attributes ---------- .. autoapisummary:: pymasep.utils.native_xml_types pymasep.utils.from_xml_types Classes ------- .. autoapisummary:: pymasep.utils.LazyXMLString Functions --------- .. autoapisummary:: pymasep.utils.import_from_dotted_path pymasep.utils.classname pymasep.utils.setup_logger pymasep.utils.close_logger pymasep.utils.remove_recursive_file pymasep.utils.native_type_to_xml pymasep.utils.native_type_to_xml_none pymasep.utils.native_type_from_xml pymasep.utils.method_exists pymasep.utils.gen_dict_extract pymasep.utils.rm_spc_acc pymasep.utils.cut_text_for_render Module Contents --------------- .. py:data:: native_xml_types Corresponding string to python native types. {str->type} .. py:data:: from_xml_types Pymas objects containing from_xml() method. Use to create object from an xml. {str->type} .. py:function:: import_from_dotted_path(dotted_names) Import a class from a string ``import_from_dotted_path('foo.bar')`` <=> ``from foo import bar; return bar`` :param dotted_names: String representing a class inside a module :return: the class represented by `dotted_names` .. py:function:: classname(obj) Get the full name (with module) of an object's class License: ? Thanks clbarnes https://gist.github.com/clbarnes/edd28ea32010eb159b34b075687bb49e :param obj: The object from which to obtain the name :return: a string containing the full class name .. py:function:: setup_logger(name, path, log_filename, level = logging.WARNING) To set up as many loggers as you want Licence: CC BY-SA 4.0 Thanks eos87 https://stackoverflow.com/questions/11232230/logging-to-two-files-with-different-settings :param name: Name of the logger :param path: path to save the log_file :param log_filename: filename of the log file :param level: log level messages to log as str or int. Default is logging.WARNING. :return: An instance of a Logger .. py:function:: close_logger(logger) Close a logger instance by removing all handlers and close them. :param logger: Logger to close .. py:function:: remove_recursive_file(path, pattern) Remove files with a certain pattern inside path and subdirectories Copyright © 2022 thisPointer Thanks https://thispointer.com/python-how-to-remove-files-by-matching-pattern-wildcards-certain-extensions-only/ :param path: Path to recursively search in. :param pattern: The file patterns to delete. :raise PermissionError: In windows (file still open ?) TO REFACTOR :raise OSError: if error occurs during deletion .. py:function:: native_type_to_xml(native) Transform a python native type (simple (`int`, `float`, ...) or complex (`list`, `set`, `tuple`, `dict`)) to xml. Complex types are serialized with ``...``. If the instance has a ``to_xml()`` method, this method is called . `dict` object are transformed to xml contains the attribute `key`. an empty string is represented by ' :param native: the instance of a native type :return: the instance as xml .. py:function:: native_type_to_xml_none(value_type) transform a python type (simple (`int`, `float`, ...) or complex (`list`, `set`, `tuple`, `dict`)) to an empty XML :param value_type: a python type. :return: an XML instance with the format ``<'value_type' />`` .. py:function:: native_type_from_xml(xml_node) Transform a xml to a python native type (simple (`int`, `float`, ...) or complex (`list`, `set`, `tuple`, `dict`)). The format for complex type is ``...``. Note : an empty string is represented by ' :param xml_node: the xml instance with expected format. :return: an instance of the python native type .. py:function:: method_exists(instance, method) Test if a method exists for an object instance Licence: CC BY-SA 3.0. Thanks https://itqna.net/questions/15635/how-check-if-method-exists-class-python :param instance: The instance to search :param method: the method (as string) to search :return: True if the methode exists in the instance .. py:function:: gen_dict_extract(key, var) Traversing a dictionary to extract the values of every `key` key (at every level) and return values as a generator Licence: CC BY-SA 3.0. Thanks https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-dictionaries-and-lists :param key: The key to search :param var: the dict in which to search :return: a generator returning all values associated to the key .. py:function:: rm_spc_acc(s) remove space and accent from a string. :param s: the string where the space and accent will be removed :return: the string without space and accent .. py:function:: cut_text_for_render(text, font_size, max_render_width) This function breaks a string into chunk which size depends on the rendered font size. Development in progress, the chunk should not break words in half. Not sure if this function is useful if we use UITextBox :param text: The text to be chunked :param font_size: the font size of the rendered text :param max_render_width: the maximum width of the rendered text :return: the list of chunk text .. py:class:: LazyXMLString(element, encoding = 'unicode', method = 'xml') This class allows an XML object to be lazily serialized Thanks, Claude.AI even if I am not sure about the license of this code :param element: the xml element to be lazy serialized :param encoding: the encoding of serialization result :param method: the method used to serialize the element .. py:attribute:: element xml element .. py:attribute:: encoding encoding .. py:attribute:: method method used to serialize the element. see xml.etree.ElementTree.tostring() .. py:method:: __str__() .. py:method:: lazy_tostring() serialize using iterator :return: an iterator of strings