gws.core.util

Core utilities

Most common function which are needed everywhere. These function are exported in gws and can be used as gws.function().

gws.core.util.exit(code: int = 255)[source]

Exit the application.

Parameters

code – Exit code.

gws.core.util.is_data_object(x)[source]
gws.core.util.get(x, key, default=None)[source]

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.has(x, key) → bool[source]

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.merge(x, *args, **kwargs)[source]

Create a dict/Data object with the values from dicts/Datas or kwargs, overwriting keys.

Parameters
  • x – A dict or a Data.

  • *args – Dicts or Datas.

  • **kwargs – Keyword args.

Returns

A new object (dict or Data).

gws.core.util.extend(x, *args, **kwargs)[source]

Create a dict/Data object with the values from dicts/Datas or kwargs, do not overwrite keys unless they’re None.

Parameters
  • x – A dict or a Data.

  • *args – Dicts or Datas.

  • **kwargs – Keyword args.

Returns

A new object (dict or Data).

gws.core.util.filter(x, fn=None)[source]

Apply a filter to a collection.

Parameters
  • x – A dict/Data or an iterable.

  • fn – Filtering function, if omitted, blank strings and empty values are removed.

Returns

A filtered object.

gws.core.util.compact(x)[source]

Remove all None values from a collection.

gws.core.util.deep_merge(x, *args, **kwargs)[source]

Deeply merge dicts/Datas into a Data object.

Parameters
  • x – A dict or a Data.

  • *args – Dicts or Datas.

  • **kwargs – Keyword args.

Returns

A new object (dict or Data).

gws.core.util.map(x, fn)[source]

Apply a function to a collection.

Parameters
  • x – A dict/Data or an iterable.

  • fn – A function.

Returns

A mapped object.

gws.core.util.strip(x)[source]

Strip all str values in a collection and remove empty values.

Parameters

x – A dict/Data or an iterable.

Returns

The stripped object.

gws.core.util.is_empty(x) → bool[source]

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

gws.core.util.as_int(x) → int[source]

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

gws.core.util.as_float(x) → float[source]

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

gws.core.util.as_str(x, encodings: List[str] = None) → str[source]

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.as_bytes(x) → bytes[source]

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

gws.core.util.as_list(x, delimiter: str = ', ') → list[source]

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.as_dict(x) → dict[source]

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

gws.core.util.as_uid(x) → str[source]

Convert a value to an uid (alphanumeric string).

gws.core.util.as_query_string(x) → str[source]

Convert a dict/list to a query string.

For each item in x, if it’s a list, join it with a comma, stringify and in utf8.

Parameters

x – Value, which can be a dict’able or a list of key,value pairs.

Returns

The query string.

gws.core.util.lines(txt: str, comment: str = None) → List[str][source]

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.read_file(path: str) → str[source]
gws.core.util.read_file_b(path: str) → bytes[source]
gws.core.util.write_file(path: str, s: str, user: int = None, group: int = None)[source]
gws.core.util.write_file_b(path: str, s: bytes, user: int = None, group: int = None)[source]
gws.core.util.ensure_dir(dir_path: str, base_dir: str = None, mode: int = 493, user: int = None, group: int = None) → str[source]

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.UID)

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

Retruns:

The absolute path to the directory.

gws.core.util.random_string(size: int) → str[source]

Generate a random string of length size.

gws.core.util.sha256(s)[source]
class gws.core.util.cached_property(fn)[source]

Decorator for a cached property.

gws.core.util.global_lock()[source]
gws.core.util.get_global(name, init_fn)[source]

Get a global variable in a thread-safe way.

Parameters
  • name – Variable name

  • init_fn – Function that returns the value if the name doesn’t exist.

Returns

The variable value

gws.core.util.set_global(name, value)[source]

Set a global variable in a thread-safe way.

Parameters
  • name – Variable name.

  • value – Variable value.

Returns

The value

gws.core.util.get_cached_object(name, init_fn, max_age: int)[source]

Return a cached object pickled in gws.OBJECT_CACHE_DIR.

Parameters
  • name – Object name

  • init_fn – Function that returns the value if the cache doesn’t exist or is too old

  • max_age – Cache max age in seconds.

Returns

The value.