Skip to content

skais_mapper.utils.helper

Generic helper functions and other stuff.

Functions:

Name Description
alias_kw

Decorator for aliasing a keyword argument in a function.

compress_encode

Compress and encode a string for shorter representations.

current_time

Get current time as string.

extract_decode

Decompress and decode a short representation string.

alias_kw

alias_kw(key: str, alias: str) -> Callable

Decorator for aliasing a keyword argument in a function.

Parameters:

Name Type Description Default
key str

Name of keyword argument in function to alias

required
alias str

Alias that can be used for this keyword argument

required
Source code in skais_mapper/utils/helper.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def alias_kw(key: str, alias: str) -> Callable:
    """Decorator for aliasing a keyword argument in a function.

    Args:
        key: Name of keyword argument in function to alias
        alias: Alias that can be used for this keyword argument
    """
    def decorator(func: Callable):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            alias_value = kwargs.get(alias)
            if alias_value:
                kwargs[key] = alias_value
            if alias in kwargs:
                del kwargs[alias]
            result = func(*args, **kwargs)
            return result
        return wrapper
    return decorator

compress_encode

compress_encode(string: str) -> str

Compress and encode a string for shorter representations.

Parameters:

Name Type Description Default
string str

String to be encoded.

required
Source code in skais_mapper/utils/helper.py
33
34
35
36
37
38
39
40
41
def compress_encode(string: str) -> str:
    """Compress and encode a string for shorter representations.

    Args:
        string: String to be encoded.
    """
    compressed_data = lzma.compress(string.encode("utf-8"))
    encoded_data = base64.b64encode(compressed_data)
    return encoded_data.decode("utf-8")

current_time

current_time(
    date_only: bool = True,
    as_str: bool = True,
    no_delim: bool = True,
) -> datetime.datetime | str

Get current time as string.

Parameters:

Name Type Description Default
date_only bool

If True, only the date is returned.

True
as_str bool

If True, a string is returned instead of a datetime object.

True
no_delim bool

If a string is returned, remove the standard delimiters.

True
Source code in skais_mapper/utils/helper.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def current_time(
    date_only: bool = True, as_str: bool = True, no_delim: bool = True
) -> datetime.datetime | str:
    """Get current time as string.

    Args:
        date_only: If True, only the date is returned.
        as_str: If True, a string is returned instead of a datetime object.
        no_delim: If a string is returned, remove the standard delimiters.
    """
    t = datetime.datetime.now()
    if date_only:
        t = t.date()
    if as_str:
        t = str(t)
        if no_delim:
            t = t.replace("-", "")
    return t

extract_decode

extract_decode(string: str) -> str

Decompress and decode a short representation string.

Parameters:

Name Type Description Default
string str

Compressed base64 string to be decoded.

required
Source code in skais_mapper/utils/helper.py
44
45
46
47
48
49
50
51
52
def extract_decode(string: str) -> str:
    """Decompress and decode a short representation string.

    Args:
        string: Compressed base64 string to be decoded.
    """
    compressed_data = base64.b64decode(string)
    original_data = lzma.decompress(compressed_data)
    return original_data.decode("utf-8")