Skip to content

graphcast.util

Utility functions for graph operations.

This package provides utility functions for data transformation, standardization, and manipulation in the context of graph database operations.

Key Components
  • Transform: Data transformation and standardization
  • Date: Date parsing and formatting utilities
  • String: String manipulation and standardization
  • Dict: Dictionary operations and cleaning
Example

from graphcast.util import standardize, parse_date_standard name = standardize("John. Doe, Smith") date = parse_date_standard("2023-01-01")

parse_date_standard(input_str)

Parse a date string in YYYY-MM-DD format.

Parameters:

Name Type Description Default
input_str str

Date string in YYYY-MM-DD format.

required

Returns:

Name Type Description
tuple

(year, month, day) as integers.

Example

parse_date_standard("2023-01-01") (2023, 1, 1)

Source code in graphcast/util/transform.py
def parse_date_standard(input_str):
    """Parse a date string in YYYY-MM-DD format.

    Args:
        input_str (str): Date string in YYYY-MM-DD format.

    Returns:
        tuple: (year, month, day) as integers.

    Example:
        >>> parse_date_standard("2023-01-01")
        (2023, 1, 1)
    """
    dt = datetime.strptime(input_str, "%Y-%m-%d")
    return dt.year, dt.month, dt.day

standardize(k)

Standardizes a string key by removing periods and splitting.

Handles comma and space-separated strings, normalizing their format.

Parameters:

Name Type Description Default
k str

Input string to be standardized.

required

Returns:

Name Type Description
str

Cleaned and standardized string.

Example

standardize("John. Doe, Smith") 'John,Doe,Smith' standardize("John Doe Smith") 'John,Doe,Smith'

Source code in graphcast/util/transform.py
def standardize(k):
    """Standardizes a string key by removing periods and splitting.

    Handles comma and space-separated strings, normalizing their format.

    Args:
        k (str): Input string to be standardized.

    Returns:
        str: Cleaned and standardized string.

    Example:
        >>> standardize("John. Doe, Smith")
        'John,Doe,Smith'
        >>> standardize("John Doe Smith")
        'John,Doe,Smith'
    """
    k = k.translate(str.maketrans({".": ""}))
    # try to split by ", "
    k = k.split(", ")
    if len(k) < 2:
        k = k[0].split(" ")
    else:
        k[1] = k[1].translate(str.maketrans({" ": ""}))
    return ",".join(k)