gws.core.util

Core utilities

Most common function which are needed everywhere.

This module is available as gws.u everywhere.

Source code: gws.core.util

Module Contents

gws.core.util.action_url_path(name: str, **kwargs) str
gws.core.util.app_lock(name='')
class gws.core.util.cached_property(fn)

Decorator for a cached property.

gws.core.util.collect(pairs)
gws.core.util.compact(x)

Remove all None values from a collection.

gws.core.util.deep_merge(x, y, concat_lists=True)

Deeply merge dicts/Datas into a nested dict/Data. Latter vales overwrite former ones unless None.

Parameters:
  • x – dict or Data.

  • y – dict or Data.

  • concat_lists – if true, list will be concatenated, otherwise merged

Returns:

A new object (dict or Data).

gws.core.util.delete_app_global(name)
gws.core.util.dirname(path)
gws.core.util.ensure_dir(dir_path: str, base_dir: str = None, mode: int = 493, user: int = None, group: int = None) str

Check if a (possibly nested) directory exists and create if it does not.

Parameters:
  • dir_path – Path to a directory.

  • base_dir – Base directory.

  • mode – Directory creation mode.

  • user – Directory user (defaults to gws.c.UID)

  • group – Directory group (defaults to gws.c.GID)

Returns:

The absolute path to the directory.

gws.core.util.ensure_system_dirs()
gws.core.util.exit(code: int = 255)

Exit the application.

Parameters:

code – Exit code.

gws.core.util.first(it)
gws.core.util.first_not_none(*args)
gws.core.util.format_map(fmt: str, x: dict | Data, default: str = '') str
gws.core.util.get(x, key, default=None)

Get a nested value/attribute from a structure.

Parameters:
  • x – A dict, list or Data.

  • key – A list or a dot separated string of nested keys.

  • default – The default value.

Returns:

The value if it exists and the default otherwise.

gws.core.util.get_app_global(name, init_fn)
gws.core.util.get_cached_object(name: str, life_time: int, init_fn)
gws.core.util.get_server_global(name: str, init_fn)
gws.core.util.has(x, key) bool

True if a nested value/attribute exists in a structure.

Parameters:
  • x – A dict, list or Data.

  • key – A list or a dot separated string of nested keys.

Returns:

True if a key exists

gws.core.util.is_atom(x)
gws.core.util.is_bytes(x)
gws.core.util.is_data_object(x) bool
gws.core.util.is_dict(x)
gws.core.util.is_dir(path)
gws.core.util.is_empty(x) bool

Check if the value is empty (None, empty list/dict/object).

gws.core.util.is_file(path)
gws.core.util.is_list(x)
gws.core.util.join_uid(parent_uid, object_uid)
gws.core.util.merge(*args, **kwargs) dict | Data

Create a new dict/Data object by merging values from dicts/Datas or kwargs. Latter vales overwrite former ones unless None.

Parameters:
  • *args – dicts or Datas.

  • **kwargs – Keyword args.

Returns:

A new object (dict or Data).

gws.core.util.mstime() int

Unix time as an integer number of milliseconds.

gws.core.util.omit(x, *keys)
gws.core.util.parse_acl(acl)

Parse an ACL config into an ACL.

Parameters:

acl – an ACL config. Can be given as a string allow X, allow Y, deny Z, or as a list of dicts { role X type allow }, { role Y type deny }, or it can already be an ACL [1 X], [0 Y], or it can be None.

Returns:

Access list.

gws.core.util.pick(x, *keys)
gws.core.util.pop(x, key, default=None)
gws.core.util.printtemp(name: str) str

Return a transient path name in the print directory.

gws.core.util.random_string(size: int) str

Generate a random string of length size.

gws.core.util.read_file(path: str) str
gws.core.util.read_file_b(path: str) bytes
gws.core.util.serialize_to_path(obj, path)
gws.core.util.server_lock(uid)
gws.core.util.set_app_global(name, value)
gws.core.util.sha256(x)
gws.core.util.sleep(n: float)

Sleep for n seconds.

gws.core.util.split_uid(joined_uid: str) tuple[str, str]
gws.core.util.stime() int

Unix time as an integer number of seconds.

gws.core.util.strip(x)

Strip all strings and remove empty values from a collection.

gws.core.util.to_bytes(x, encoding='utf8') bytes

Convert a value to bytes by converting it to string and encoding.

gws.core.util.to_data_object(x) bool
gws.core.util.to_dict(x) dict

Convert a value to a dict. If the argument is an object, return its dict.

gws.core.util.to_float(x) float

Convert a value to a float or 0.0 if this fails.

gws.core.util.to_int(x) int

Convert a value to an int or 0 if this fails.

gws.core.util.to_lines(txt: str, comment: str = None) list[str]

Convert a multiline string into a list of strings.

Strip each line, skip empty lines, if comment is given, also remove lines starting with it.

gws.core.util.to_list(x, delimiter: str = ',') list

Convert a value to a list.

Parameters:
  • x – A value. Is it’s a string, split it by the delimiter

  • delimiter

Returns:

A list.

gws.core.util.to_lower_dict(x) dict
gws.core.util.to_rounded_int(x) int

Round and convert a value to an int or 0 if this fails.

gws.core.util.to_str(x, encodings: list[str] = None) str

Convert a value to a string.

Parameters:
  • x – Value.

  • encodings – A list of acceptable encodings. If the value is bytes, try each encoding, and return the first one which passes without errors.

Returns:

A string.

gws.core.util.to_uid(x) str

Convert a value to an uid (alphanumeric string).

gws.core.util.to_upper_dict(x) dict
gws.core.util.UID_DELIMITER = '::'
gws.core.util.uniq(x)

Remove duplicate elements from a collection.

gws.core.util.unserialize_from_path(path)
gws.core.util.utime() float

Unix time as a float number.

gws.core.util.write_file(path: str, s: str, user: int = None, group: int = None)
gws.core.util.write_file_b(path: str, s: bytes, user: int = None, group: int = None)