Utils

This module contains lots of helper functions.

class acg.utils.AppBusyContext[source]

Bases: contextlib.ContextDecorator

Set app.busy to True and 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 = True in the meantime.

acg.utils.async_get_results(functions: Callable[[Any], dict], *args)[source]

Get merged dict from gathered async-run.

acg.utils.async_wrapper(functions, *args)[source]

Gather async functions.

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 Image class to reduce the resolution of an image at path and 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.contains_newline(match)[source]

Return ‘n’ or ” “.

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.get_username()[source]

Return name of currently active user.

acg.utils.is_duplicate(word)[source]

Check if word is already present in the app.word_state_dict.

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.now_string()[source]

Return current time in the format YYYY-MM-DD_HH:MM:SS.

acg.utils.pick_val(whitelist, dictionary)[source]

Return filtered dictionary, whose values are in whitelist.

acg.utils.pop_unchanged(dictionary)[source]

Pop all values from dict where key=val.

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.run_in_thread(func)[source]

Call func in new thread.

acg.utils.save_dict_to_csv(some_dict, out_path)[source]

Save dictionary as row to csv.

acg.utils.set_screen(screen_name)[source]

Set current screen to the one with name screen_name.

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 True and False respectively.

acg.utils.start_thread(func, fn_arg, **kwargs)[source]

Wrapper-function to call func in new thread.

Every kwarg that fits the signature of func is plugged into func, the others are used in the constructor of threading.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 num workers in separate threads.

Parameters:
  • worker_fn – Function to be started
  • num – Number of threads/workers
acg.utils.timer()[source]

Print execution time.

acg.utils.update_word_state_dict(word, state)[source]

Set state in app.word_state_dict.

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