Utilities

Util functions for plexosdb.

plexosdb.utils.batched(iterable, n)

Implement batched iterator.

https://docs.python.org/3/library/itertools.html#itertools.batched

plexosdb.utils.validate_string(value)

Validate string and convert it to python object.

This function also tries to parse floats or ints.

Parameters:

value (Any) – String value to be converted to Python Object

Return type:

Any

Note

The ast is slow due to the multiple cases. Use it only on simple for loops as this could become a bottleneck.

plexosdb.utils.no_space(a, b)

Collate function for catching strings with spaces.

Parameters:
  • a (str)

  • b (str)

Return type:

int

plexosdb.utils.normalize_names(*args)

Normalize a name or list of names into a unique list of strings.

Parameters:

names (str or Iterable[str]) – A string or an iterable of strings to normalize

Returns:

A deduplicated list of the provided names

Return type:

list[str]

Raises:

ValueError – If the input is neither a string nor an iterable of strings

plexosdb.utils.get_sql_query(query_name)

Load SQL query from package.

Parameters:

query_name (str) – Name of the query file to load from plexosdb.queries

Returns:

Content of the SQL query file as a string

Return type:

str

plexosdb.utils.prepare_sql_data_params(records, memberships, property_mapping)

Create list of tuples for data ingestion.

Parameters:
  • records (list[dict[str, float]]) – List of records where each record is a dictionary containing ‘name’ and property values

  • memberships (list[dict[str, int]]) – List of membership dictionaries with ‘name’ and ‘membership_id’ keys

  • property_mapping (list[tuple[str, int]]) – List of tuples mapping property names to property IDs

Returns:

List of tuples containing (membership_id, property_id, value) for database insertion

Return type:

list[tuple[int, int, Any]]

plexosdb.utils.create_membership_record(object_ids, child_object_class_id, parent_object_id, parent_object_class_id, collection_id)

Create membership records for database insertion.

Parameters:
  • object_ids (Iterable[int]) – Iterable of child object IDs to create memberships for

  • child_object_class_id (int) – Class ID for the child objects

  • parent_object_id (int) – ID of the parent object

  • parent_object_class_id (int) – Class ID for the parent object

  • collection_id (int) – ID of the collection to which the membership belongs

Returns:

List of dictionaries representing membership records ready for database insertion

Return type:

list[dict[str, int]]