Skip to content

plaid.utils.base

plaid.utils.base

Base utilities.

plaid.utils.base.NotAllowedError

Exception for not allowed usage.

plaid.utils.base.ShapeError

Exception for badly shaped tensors.

plaid.utils.base.DeprecatedError

Exception for deprecated methods.

plaid.utils.base.generate_random_ASCII

generate_random_ASCII(size=16)

Generate a random ASCII string of the specified size.

Parameters:

  • size (int, default: 16 ) –

    The length of the generated string. Defaults to 16.

Returns:

  • str ( str ) –

    A random ASCII string of the specified size.

Source code in plaid/utils/base.py
def generate_random_ASCII(size: int = 16) -> str:
    """Generate a random ASCII string of the specified size.

    Args:
        size (int, optional): The length of the generated string. Defaults to 16.

    Returns:
        str: A random ASCII string of the specified size.
    """
    assert size >= 1
    rnd_ = chr(np.random.randint(65, 91))
    for _ in range(size - 1):
        val_ = np.random.randint(91 - 65 + 10)
        if val_ >= 10:
            rnd_ += chr(val_ - 10 + 65)
        else:
            rnd_ += str(val_)
    return rnd_

plaid.utils.base.safe_len

safe_len(obj)

Safely return the length of an object, or 0 if the object has no length.

Parameters

obj : Any The object whose length is to be computed.

Returns:

int The length of the object if it defines __len__, otherwise 0.

Source code in plaid/utils/base.py
def safe_len(obj):
    """Safely return the length of an object, or 0 if the object has no length.

    Parameters
    ----------
    obj : Any
        The object whose length is to be computed.

    Returns:
    -------
    int
        The length of the object if it defines `__len__`, otherwise 0.
    """
    return len(obj) if hasattr(obj, "__len__") else 0