Utils¶
This module contains lots of helper functions.
-
class
acg.utils.AppBusyContext[source]¶ Bases:
contextlib.ContextDecoratorSet app.busy to
Trueand back to its previous value.
-
class
acg.utils.CD(new_path)[source]¶ Context manager for changing the current working directory to
new_path.
-
acg.utils.app_busy(func)[source]¶ Call function in new thread and set app.busy =
Truein the meantime.
-
acg.utils.async_get_results(functions: Callable[[Any], dict], *args)[source]¶ Get merged dict from gathered async-run.
-
acg.utils.close_and_callback(file_path, file_manager=None, callback=None, old_callback=None)[source]¶ Helper-function for
get_file_manager().
-
acg.utils.compress_img(path, width=512)[source]¶ Use the
Imageclass to reduce the resolution of an image atpathand overwrites it.If the image already has smaller width, nothing is done.
Parameters: - path – Path to image-file.
- width – New width of image (Default value = 512)
-
acg.utils.compress_img_bytes(bytes_image, width=512)[source]¶ Compress image given as bytes (e.g. as content of
requests.Response).
-
acg.utils.get_file_manager(ext=None, callback=None)[source]¶ Get file manager.
Context manager to temporarily set ext and callback of file_manager.
-
acg.utils.load_dicts_from_csv(path)[source]¶ Load csv. Has to have the keys as first line.
Returns: List of dictionaries.
-
acg.utils.merge_or_last(args)[source]¶ If all args are list, returns merged list. Else returns last arg.
-
acg.utils.not_implemented_toast(*_)[source]¶ Display toast notifying user that the function is not implemented.
-
acg.utils.pick_val(whitelist, dictionary)[source]¶ Return filtered dictionary, whose values are in
whitelist.
-
acg.utils.remove_whitespace(some_string)[source]¶ Replace each group of whitespaces by a simple space (or by a newline if at least one is in the match).
-
acg.utils.set_word_state(word, state)[source]¶ Set state in the data-base entry of the card of the current template.
-
acg.utils.smart_dict_merge(*args)[source]¶ If values are lists they are merged, else the last element is chosen as new value.
-
acg.utils.smart_loader(path)[source]¶ Use file ending of path to determine which function to use to load file.
- Supported file endings:
- .p (pickle)
- .csv
- .json
Parameters: path – Path of file to load. Returns: Loaded object.
-
acg.utils.smart_saver(obj, path)[source]¶ Use file ending of path to determine which function to use to save file.
- Supported file endings:
- .p (pickle)
- .csv
- .json
Parameters: - obj – Object to save.
- path – Path where object should be stored.
-
acg.utils.split_on_condition(seq, condition)[source]¶ Return two lazy generators for
TrueandFalserespectively.
-
acg.utils.start_thread(func, fn_arg, **kwargs)[source]¶ Wrapper-function to call
funcin new thread.Every kwarg that fits the signature of
funcis plugged intofunc, the others are used in the constructor ofthreading.Thread.Note
The
toolz.curry()decorator enables partial evaluation without extra use of partial.Examples
>>> def f(x,y="y",z="z"): ... print("Hello from",threading.current_thread().name) ... print(x,y,z) >>> callback = start_thread(f,z="non_default_z",name="print_thread") >>> callback(2) Hello from print_thread 2 y non_default_z
-
acg.utils.start_workers(worker_fn, num)[source]¶ Start a number of
numworkers in separate threads.Parameters: - worker_fn – Function to be started
- num – Number of threads/workers
-
acg.utils.widget_by_id(string)[source]¶ Get widget by string of ids, separated by “/”.
Parameters: string – Strings of ids, separated by “/”. The first one can be a screen name. Returns: widget - Examples
>>> print(widget_by_id("single_word/word_prop").__name__) MDApp.get_running_app().root.get_screen("single_word").children[ 0].ids.word_prop