hpcflow.sdk.core.utils#

Miscellaneous utilities.

Functions

bisect_slice

Given two sequences (the first of which of known length), get the two slices that are equivalent to a given slice if the two sequences were combined.

capitalise_first_letter

Convert the first character of a string to upper case (if that makes sense).

check_in_object_list

Decorator factory for the various from_spec class methods that have attributes that should be replaced by an object from an object list.

check_valid_py_identifier

Check a string is (roughly) a valid Python variable identifier and return it.

dict_values_process_flat

Return a copy of a dict, where the values are processed by a callable that is to be called only once, and where the values may be single items or lists of items.

ensure_in

Get the index of an item in a list and append the item if it is not in the list.

flatten

Flatten an arbitrarily (but of uniform depth) nested list and return shape information to enable un-flattening.

get_duplicate_items

Get a list of all items in an iterable that appear more than once, assuming items are hashable.

get_enum_by_name_or_val

Retrieve an enum by name or value, assuming uppercase names and integer values.

get_in_container

Follow a path (sequence of indices of appropriate type) into a container to obtain a "leaf" value.

get_item_repeat_index

Get the repeat index for each item in a list.

get_md5_hash

Compute the MD5 hash of an object.

get_nested_indices

Generate the set of nested indices of length n that correspond to a global idx.

get_process_stamp

Return a globally unique string identifying this process.

get_relative_path

Get relative path components between two paths.

get_time_stamp

Get the current time in standard string form.

group_by_dict_key_values

Group a list of dicts according to specified equivalent key-values.

is_fsspec_url

Test if a URL appears to be one that can be understood by fsspec.

linspace_rect

Generate a linear space around a rectangle.

list_to_dict

Convert a list of dicts to a dict of lists.

load_config

API function decorator to ensure the configuration has been loaded, and load if not.

make_workflow_id

Generate a random ID for a workflow.

nth_key

Given a dict in some order, get the n'th key of that dict.

nth_value

Given a dict in some order, get the n'th value of that dict.

open_file

Open a file or directory using the default system application.

process_string_nodes

Walk through a nested data structure and process string nodes using a provided callable.

read_JSON_file

Load a JSON file.

read_JSON_string

Load a JSON string.

read_YAML_file

Load a YAML file.

read_YAML_str

Load a YAML string.

remove_ansi_escape_sequences

Strip ANSI terminal escape codes from a string.

replace_items

Replaced a range of items in a list with items in another list.

reshape

Reverse the destructuring of the flatten() function.

search_dir_files_by_regex

Search recursively for files in a directory by a regex pattern and return matching file paths, relative to the given directory.

set_in_container

Follow a path (sequence of indices of appropriate type) into a container to update a "leaf" value.

split_param_label

Split a parameter path into the path and the label, if present.

substitute_string_vars

Scan string and substitute sequences like <<var:ABC>> with the value looked up in the supplied dictionary (with ABC as the key).

swap_nested_dict_keys

Return a copy where top-level keys have been swapped with a second-level inner key.

write_JSON_file

Write a basic object to a JSON file.

write_YAML_file

Write a basic object to a YAML file.

Classes

JSONLikeDirSnapShot

Overridden DirectorySnapshot from watchdog to allow saving and loading from JSON.

PrettyPrinter

A class that produces a nice readable version of itself with str().

Singleton

Metaclass that enforces that only one instance can exist of the classes to which it is applied.

classproperty

Simple class property decorator.