Skip to content

Miscellaneous Utility Functions

Ensures that value is a list.

Source code in biocypher/_misc.py
def to_list(value: Any) -> list:
    """
    Ensures that ``value`` is a list.
    """

    if isinstance(value, LIST_LIKE):
        value = list(value)

    else:
        value = [value]

    return value

Returns iterables, except strings, wraps simple types into tuple.

Source code in biocypher/_misc.py
def ensure_iterable(value: Any) -> Iterable:
    """
    Returns iterables, except strings, wraps simple types into tuple.
    """

    return value if isinstance(value, LIST_LIKE) else (value,)

Creates a visualisation of the inheritance tree using treelib.

Source code in biocypher/_misc.py
def create_tree_visualisation(inheritance_graph: Union[dict, nx.Graph]) -> Tree:
    """
    Creates a visualisation of the inheritance tree using treelib.
    """
    inheritance_tree = _get_inheritance_tree(inheritance_graph)
    classes, root = _find_root_node(inheritance_tree)

    tree = Tree()
    tree.create_node(root, root)
    while classes:
        for child in classes:
            parent = inheritance_tree[child]
            if parent in tree.nodes.keys() or parent == root:
                tree.create_node(child, child, parent=parent)

        for node in tree.nodes.keys():
            if node in classes:
                classes.remove(node)

    return tree
Source code in biocypher/_misc.py
def from_pascal(s: str, sep: str = " ") -> str:
    underscored = underscore_pattern.sub(sep, s)
    lowercased = lowercase_pattern.sub(
        lambda match: match.group(0).lower(),
        underscored,
    )
    return lowercased

Convert PascalCase to sentence case.

Parameters:

Name Type Description Default
s str

Input string in PascalCase

required

Returns:

Type Description
str

string in sentence case form

Source code in biocypher/_misc.py
def pascalcase_to_sentencecase(s: str) -> str:
    """
    Convert PascalCase to sentence case.

    Args:
        s: Input string in PascalCase

    Returns:
        string in sentence case form
    """
    return from_pascal(s, sep=" ")

Convert snake_case to sentence case.

Parameters:

Name Type Description Default
s str

Input string in snake_case

required

Returns:

Type Description
str

string in sentence case form

Source code in biocypher/_misc.py
def snakecase_to_sentencecase(s: str) -> str:
    """
    Convert snake_case to sentence case.

    Args:
        s: Input string in snake_case

    Returns:
        string in sentence case form
    """
    return stringcase.sentencecase(s).lower()

Convert sentence case to snake_case.

Parameters:

Name Type Description Default
s str

Input string in sentence case

required

Returns:

Type Description
str

string in snake_case form

Source code in biocypher/_misc.py
def sentencecase_to_snakecase(s: str) -> str:
    """
    Convert sentence case to snake_case.

    Args:
        s: Input string in sentence case

    Returns:
        string in snake_case form
    """
    return stringcase.snakecase(s).lower()

Convert sentence case to PascalCase.

Parameters:

Name Type Description Default
s str

Input string in sentence case

required

Returns:

Type Description
str

string in PascalCase form

Source code in biocypher/_misc.py
def sentencecase_to_pascalcase(s: str, sep: str = r"\s") -> str:
    """
    Convert sentence case to PascalCase.

    Args:
        s: Input string in sentence case

    Returns:
        string in PascalCase form
    """
    return re.sub(
        r"(?:^|[" + sep + "])([a-zA-Z])",
        lambda match: match.group(1).upper(),
        s,
    )