pymasep.utils

Miscellaneous utility functions

Attributes

native_xml_types

Corresponding string to python native types. {str->type}

from_xml_types

Pymas objects containing from_xml() method. Use to create object from an xml. {str->type}

Classes

LazyXMLString

This class allows an XML object to be lazily serialized

Functions

import_from_dotted_path(dotted_names)

Import a class from a string

classname(obj)

Get the full name (with module) of an object's class

setup_logger(name, path, log_filename[, level])

To set up as many loggers as you want

close_logger(logger)

Close a logger instance by removing all handlers and close them.

remove_recursive_file(path, pattern)

Remove files with a certain pattern inside path and subdirectories

native_type_to_xml(native)

Transform a python native type (simple (int, float, ...) or complex (list, set, tuple, dict)) to xml.

native_type_to_xml_none(value_type)

transform a python type (simple (int, float, ...) or complex (list, set, tuple, dict)) to an empty XML

native_type_from_xml(xml_node)

Transform a xml to a python native type (simple (int, float, ...) or complex (list, set, tuple, dict)).

method_exists(instance, method)

Test if a method exists for an object instance

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

rm_spc_acc(s)

remove space and accent from a string.

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.

Module Contents

pymasep.utils.native_xml_types

Corresponding string to python native types. {str->type}

pymasep.utils.from_xml_types

Pymas objects containing from_xml() method. Use to create object from an xml. {str->type}

pymasep.utils.import_from_dotted_path(dotted_names)

Import a class from a string import_from_dotted_path('foo.bar') <=> from foo import bar; return bar

Parameters:

dotted_names (str) – String representing a class inside a module

Returns:

the class represented by dotted_names

Return type:

Any

pymasep.utils.classname(obj)

Get the full name (with module) of an object’s class

License: ?

Thanks clbarnes https://gist.github.com/clbarnes/edd28ea32010eb159b34b075687bb49e

Parameters:

obj (Any) – The object from which to obtain the name

Returns:

a string containing the full class name

Return type:

str

pymasep.utils.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

Parameters:
  • name (str) – Name of the logger

  • path (str) – path to save the log_file

  • log_filename (str) – filename of the log file

  • level (Union[int, str]) – log level messages to log as str or int. Default is logging.WARNING.

Returns:

An instance of a Logger

Return type:

logging.Logger

pymasep.utils.close_logger(logger)

Close a logger instance by removing all handlers and close them.

Parameters:

logger – Logger to close

Return type:

None

pymasep.utils.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/

Parameters:
  • path (str) – Path to recursively search in.

  • pattern (str) – The file patterns to delete.

Raises:
  • PermissionError – In windows (file still open ?) TO REFACTOR

  • OSError – if error occurs during deletion

Return type:

None

pymasep.utils.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 <complex_type><item [key="KEY1"]>...</item></complex_type>. 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 ‘<str content=”empty” />

Parameters:

native (Union[List, Set, Tuple, Dict, Any]) – the instance of a native type

Returns:

the instance as xml

Return type:

xml.etree.ElementTree.Element

pymasep.utils.native_type_to_xml_none(value_type)

transform a python type (simple (int, float, …) or complex (list, set, tuple, dict)) to an empty XML

Parameters:

value_type (type) – a python type.

Returns:

an XML instance with the format <'value_type' />

Return type:

xml.etree.ElementTree.Element

pymasep.utils.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 <complex_type><item [key="KEY1"]>...</item></complex_type>. Note : an empty string is represented by ‘<str content=”empty” />

Parameters:

xml_node (xml.etree.ElementTree.Element) – the xml instance with expected format.

Returns:

an instance of the python native type

Return type:

Tuple[Union[None, Any], type]

pymasep.utils.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

Parameters:
  • instance (object) – The instance to search

  • method (str) – the method (as string) to search

Returns:

True if the methode exists in the instance

Return type:

bool

pymasep.utils.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

Parameters:
  • key (Any) – The key to search

  • var (dict) – the dict in which to search

Returns:

a generator returning all values associated to the key

Return type:

Iterator[Any]

pymasep.utils.rm_spc_acc(s)

remove space and accent from a string.

Parameters:

s (str) – the string where the space and accent will be removed

Returns:

the string without space and accent

Return type:

str

pymasep.utils.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

Parameters:
  • text (str) – The text to be chunked

  • font_size (int) – the font size of the rendered text

  • max_render_width (int) – the maximum width of the rendered text

Returns:

the list of chunk text

Return type:

List[str]

class pymasep.utils.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

Parameters:
  • element (xml.etree.ElementTree.Element) – the xml element to be lazy serialized

  • encoding (str) – the encoding of serialization result

  • method (str) – the method used to serialize the element

element

xml element

encoding

encoding

method

method used to serialize the element. see xml.etree.ElementTree.tostring()

__str__()
lazy_tostring()

serialize using iterator

Returns:

an iterator of strings

Return type:

Iterator[str]