Skip to content

Utils various

utils_various

Module contains a collection of miscellaneous utilities.

LOCALE_LOCK = threading.Lock() module-attribute

loggers: dict[str, logging.Logger] = {} module-attribute

parserinfo_localized(localeID: str, *args, **kwargs)

parserinfo_localized - create localized date parser object

Source code in src/utils_mystuff/utils_various.py
def __init__(self, localeID: str, *args, **kwargs):
    """ initialize parserinfo localized """

    with setlocale(localeID):
        self.WEEKDAYS = zip(calendar.day_abbr, calendar.day_name)  # type: ignore[assignment]
        self.MONTHS = list(zip(calendar.month_abbr, calendar.month_name))[1:]  # type: ignore[assignment]
    super().__init__(*args, **kwargs)

MONTHS = list(zip(calendar.month_abbr, calendar.month_name))[1:] instance-attribute

WEEKDAYS = zip(calendar.day_abbr, calendar.day_name) instance-attribute

copy_dictfields(source: dict, target: Any) -> None

copy_dict_fields - copy fields from dict to target structure

can be used to copy same named fields to a data class from TypedArgParser via .as_dict() or one dataclass to another via .asdict()

Parameters:

Name Type Description Default
source dict

source dictionary

required
target Any

target dictionary

required
Source code in src/utils_mystuff/utils_various.py
def copy_dictfields(source: dict, target: Any) -> None:
    """
    copy_dict_fields - copy fields from dict to target structure

    can be used to copy same named fields to a data class from TypedArgParser via <TypedArgParser object>.as_dict() or
    one dataclass to another via <dataclass>.asdict()

    Args:
        source (dict): source dictionary
        target (Any): target dictionary
    """
    copy_dictfields(source, target)

copydictfields(source: dict, target: Any) -> None

copydictfields - copy fields from dict to target structure

can be used to copy same named fields to a data class from TypedArgParser via .as_dict() or one dataclass to another via .asdict()

Parameters:

Name Type Description Default
source dict

source dictionary

required
target Any

target dictionary

required
Source code in src/utils_mystuff/utils_various.py
def copydictfields(source: dict, target: Any) -> None:
    """
    copydictfields - copy fields from dict to target structure

    can be used to copy same named fields to a data class from TypedArgParser via <TypedArgParser object>.as_dict() or
    one dataclass to another via <dataclass>.asdict()

    Args:
        source (dict): source dictionary
        target (Any): target dictionary
    """

    for key, value in source.items():
        if hasattr(target, key):
            target.key = value
        elif isinstance(target, dict):
            if key in target:
                target[key] = value

get_real_apppath() -> tuple[str, str]

get_real_apppath - find true application directory for frozen/bundled execution

Returns:

Name Type Description
str tuple[str, str]

application path

Source code in src/utils_mystuff/utils_various.py
def get_real_apppath() -> tuple[str, str]:
    """
    get_real_apppath - find true application directory for frozen/bundled execution

    Returns:
        str: application path
    """

    if getattr(sys, 'frozen', False):
        application_path = os.path.dirname(sys.executable)
        running_mode = 'Frozen/executable'
    else:
        try:
            app_full_path = os.path.realpath(__file__)
            application_path = os.path.dirname(app_full_path)
            running_mode = "Non-interactive (e.g. 'python myapp.py')"
        except NameError:
            application_path = os.getcwd()
            running_mode = 'Interactive'

    return application_path, running_mode

ignore_exceptions(func: Callable[..., Any]) -> Callable[..., Any]

ignore_exceptions - decorator for ignoring exceptions using parameter in decorated function

Parameters:

Name Type Description Default
func Callable

function to be decorated

required

Returns:

Name Type Description
Callable Callable[..., Any]

wrapped function

Source code in src/utils_mystuff/utils_various.py
def ignore_exceptions(func: Callable[..., Any]) -> Callable[..., Any]:
    """
    ignore_exceptions - decorator for ignoring exceptions using parameter in decorated function

    Args:
        func (Callable): function to be decorated

    Returns:
        Callable: wrapped function
    """

    @wraps(func)
    def wrapper(*args, **kwargs):
        result = None
        ignored_exceptions = kwargs.get('ignored_exceptions')
        # if ignored_exceptions:
        if isinstance(ignored_exceptions, tuple) and all(issubclass(e, BaseException) for e in ignored_exceptions):
            try:
                result = func(*args, **kwargs)
            except ignored_exceptions:
                pass
        else:
            result = func(*args, **kwargs)
        return result

    return wrapper

ignore_exceptions_parameterized(ignored_exceptions: tuple[type[BaseException]]) -> Callable[[Callable[_P, _R]], Callable[_P, Optional[_R]]]

ignore_exceptions_parameterized - parameterized decorator for ignoring exceptions using closure

Parameters:

Name Type Description Default
ignored_exceptions tuple[BaseException]

exceptions to be ignored

required

Returns:

Name Type Description
Callable Callable[[Callable[_P, _R]], Callable[_P, Optional[_R]]]

wrapped function

Source code in src/utils_mystuff/utils_various.py
def ignore_exceptions_parameterized(ignored_exceptions: tuple[type[BaseException]]) -> Callable[[Callable[_P, _R]], Callable[_P, Optional[_R]]]:  # type: ignore
    """
    ignore_exceptions_parameterized - parameterized decorator for ignoring exceptions using closure

    Args:
        ignored_exceptions (tuple[BaseException]): exceptions to be ignored

    Returns:
        Callable: wrapped function
    """
    def ignore_exceptions_helper(func):

        @wraps(func)
        def wrapper(*args, **kwargs):
            result = None
            if ignored_exceptions:
                try:
                    result = func(*args, **kwargs)
                except ignored_exceptions:
                    pass
            else:
                result = func(*args, **kwargs)
            return result

        return wrapper

    return ignore_exceptions_helper

initLogger(loggername: str, formatstr: str = '%(asctime)s\t%(levelname)s\t%(message)s', datefmtstr: str = '%d.%m.%Y %H:%M:%S', filename: str = '') -> logging.Logger

initLogger - initialize standard logger object

Parameters:

Name Type Description Default
loggername str

name of logger

required
formatstr str

format string for log entries. Defaults to "%(asctime)s %(levelname)s %(message)s".

'%(asctime)s\t%(levelname)s\t%(message)s'
datefmtstr str

date format for log entries. Defaults to "%d.%m.%Y %H:%M:%S".

'%d.%m.%Y %H:%M:%S'
filename str

name of log file. Defaults to "".

''

Returns:

Type Description
Logger

logging.Logger: logger object

Source code in src/utils_mystuff/utils_various.py
def initLogger(
    loggername: str,
    formatstr: str = "%(asctime)s\t%(levelname)s\t%(message)s",
    datefmtstr: str = "%d.%m.%Y %H:%M:%S",
    filename: str = ""
) -> logging.Logger:
    """
    initLogger - initialize standard logger object

    Args:
        loggername (str): name of logger
        formatstr (str, optional): format string for log entries. Defaults to "%(asctime)s\t%(levelname)s\t%(message)s".
        datefmtstr (str, optional): date format for log entries. Defaults to "%d.%m.%Y %H:%M:%S".
        filename (str, optional): name of log file. Defaults to "".

    Returns:
        logging.Logger: logger object
    """

    global loggers

    if loggers.get(loggername):
        logger = loggers.get(loggername)
    else:
        logger = logging.getLogger(loggername)
        logformatter = logging.Formatter(formatstr, datefmt=datefmtstr)
        if filename == "":
            loghandler = logging.FileHandler(os.path.join(tempfile.gettempdir(), loggername + "_Py.txt"))
        else:
            loghandler = logging.FileHandler(filename)
        loghandler.setFormatter(logformatter)
        loghandler.setLevel(logging.INFO)
        logger.addHandler(loghandler)
        logger.setLevel(logging.INFO)
        loggers[loggername] = logger

    assert logger is not None
    return logger

init_logger(loggername: str, formatstr: str = '%(asctime)s\t%(levelname)s\t%(message)s', datefmtstr: str = '%d.%m.%Y %H:%M:%S', filename: str = '') -> logging.Logger

init_logger - initialize standard logger object

Parameters:

Name Type Description Default
loggername str

name of logger

required
formatstr str

format string for log entries. Defaults to "%(asctime)s %(levelname)s %(message)s".

'%(asctime)s\t%(levelname)s\t%(message)s'
datefmtstr str

date format for log entries. Defaults to "%d.%m.%Y %H:%M:%S".

'%d.%m.%Y %H:%M:%S'
filename str

name of log file. Defaults to "".

''

Returns:

Type Description
Logger

logging.Logger: logger object

Source code in src/utils_mystuff/utils_various.py
def init_logger(
    loggername: str,
    formatstr: str = "%(asctime)s\t%(levelname)s\t%(message)s",
    datefmtstr: str = "%d.%m.%Y %H:%M:%S",
    filename: str = ""
) -> logging.Logger:
    """
    init_logger - initialize standard logger object

    Args:
        loggername (str): name of logger
        formatstr (str, optional): format string for log entries. Defaults to "%(asctime)s\t%(levelname)s\t%(message)s".
        datefmtstr (str, optional): date format for log entries. Defaults to "%d.%m.%Y %H:%M:%S".
        filename (str, optional): name of log file. Defaults to "".

    Returns:
        logging.Logger: logger object
    """
    return initLogger(loggername, formatstr, datefmtstr, filename)

is_date(checkvalue: Any, checkformat='%d.%m.%Y') -> bool

is_date - check if checkvalue is a date value

Parameters:

Name Type Description Default
checkvalue Any

value to be checked

required
checkformat str

dateformat for check of checkvalue of type str. Defaults to "%d.%m.%Y".

'%d.%m.%Y'

Returns:

Name Type Description
bool bool

checkvalue is date or not

Source code in src/utils_mystuff/utils_various.py
def is_date(checkvalue: Any, checkformat="%d.%m.%Y") -> bool:
    """
    is_date - check if checkvalue is a date value

    Args:
        checkvalue (Any): value to be checked
        checkformat (str, optional): dateformat for check of checkvalue of type str. Defaults to "%d.%m.%Y".

    Returns:
        bool: checkvalue is date or not
    """
    return isdate(checkvalue, checkformat)

isdate(checkvalue: Any, checkformat='%d.%m.%Y') -> bool

isdate - check if checkvalue is a date value

Parameters:

Name Type Description Default
checkvalue Any

value to be checked

required
checkformat str

dateformat for check of checkvalue of type str. Defaults to "%d.%m.%Y".

'%d.%m.%Y'

Returns:

Name Type Description
bool bool

checkvalue is date or not

Source code in src/utils_mystuff/utils_various.py
def isdate(checkvalue: Any, checkformat="%d.%m.%Y") -> bool:
    """
    isdate - check if checkvalue is a date value

    Args:
        checkvalue (Any): value to be checked
        checkformat (str, optional): dateformat for check of checkvalue of type str. Defaults to "%d.%m.%Y".

    Returns:
        bool: checkvalue is date or not
    """

    if checkvalue is None or checkvalue == "":
        return False
    # elif type(checkvalue).__name__ == "datetime" or type(checkvalue).__name__ == "date":
    elif isinstance(checkvalue, (datetime.datetime, datetime.date)):
        return True
    elif isinstance(checkvalue, str):
        try:
            check = bool(datetime.datetime.strptime(checkvalue, checkformat))
        except ValueError:
            check = False
        return check
    else:
        return False

read_configfile(configfile: str, optionxform: Optional[Callable[[str], str]] = None, encoding: str = 'utf-8') -> configparser.ConfigParser

read_configfile - read config file with standardized boolean states into ConfigParser object

Parameters:

Name Type Description Default
configfile str

config file

required
optionxform callable[[str], str]]

callable to pass on to ConfigParser.optionxform. Defaults to None.

None
encoding str

file encoding. Defaults to "utf-8".

'utf-8'

Raises:

Type Description
Exception

file 'configfile' does not exist.

Returns:

Type Description
ConfigParser

configparser.ConfigParser: config parser object

Source code in src/utils_mystuff/utils_various.py
def read_configfile(
    configfile: str,
    optionxform: Optional[Callable[[str], str]] = None,
    encoding: str = "utf-8"
) -> configparser.ConfigParser:
    """
    read_configfile - read config file with standardized boolean states into ConfigParser object

    Args:
        configfile (str): config file
        optionxform (callable[[str], str]], optional): callable to pass on to ConfigParser.optionxform. Defaults to None.
        encoding (str, optional): file encoding. Defaults to "utf-8".

    Raises:
        Exception: file 'configfile' does not exist.

    Returns:
        configparser.ConfigParser: config parser object
    """
    return readconfigfile(configfile, optionxform, encoding)

readconfigfile(configfile: str, optionxform: Optional[Callable[[str], str]] = None, encoding: str = 'utf-8') -> configparser.ConfigParser

readconfigfile - read config file with standardized boolean states into ConfigParser object

Parameters:

Name Type Description Default
configfile str

config file

required
optionxform callable[[str], str]]

callable to pass on to ConfigParser.optionxform. Defaults to None.

None
encoding str

file encoding. Defaults to "utf-8".

'utf-8'

Raises:

Type Description
Exception

file 'configfile' does not exist.

Returns:

Type Description
ConfigParser

configparser.ConfigParser: config parser object

Source code in src/utils_mystuff/utils_various.py
def readconfigfile(
    configfile: str,
    optionxform: Optional[Callable[[str], str]] = None,
    encoding: str = "utf-8"
) -> configparser.ConfigParser:
    """
    readconfigfile - read config file with standardized boolean states into ConfigParser object

    Args:
        configfile (str): config file
        optionxform (callable[[str], str]], optional): callable to pass on to ConfigParser.optionxform. Defaults to None.
        encoding (str, optional): file encoding. Defaults to "utf-8".

    Raises:
        Exception: file 'configfile' does not exist.

    Returns:
        configparser.ConfigParser: config parser object
    """

    # no path provided as part of name of configfile, only filename -> use execution directory as default
    if configfile == os.path.basename(configfile):
        configfile = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), configfile)

    if not os.path.isfile(configfile):
        err_msg = f"Error reading ini file '{configfile}'. File does not exist"
        raise Exception(err_msg)

    config = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
    if optionxform is not None:
        config.optionxform = optionxform  # type: ignore
    config.BOOLEAN_STATES = {  # type: ignore
        "1": True, "True": True, "true": True, "Yes": True, "yes": True,
        "0": False, "False": False, "false": False, "No": False, "no": False
    }

    config.read(configfile, encoding=encoding)

    return config

setLogLevel(logger: logging.Logger, config: configparser.ConfigParser, section: str, optionLogLevel: str) -> None

setLogLevel - set log level from configparser object

Parameters:

Name Type Description Default
logger Logger

name of logger

required
config configparser

configparser object

required
section str

section in 'config'

required
optionLogLevel str

option in 'config'

required
Source code in src/utils_mystuff/utils_various.py
def setLogLevel(logger: logging.Logger, config: configparser.ConfigParser, section: str, optionLogLevel: str) -> None:
    """
    setLogLevel - set log level from configparser object

    Args:
        logger (logging.Logger): name of logger
        config (configparser): configparser object
        section (str): section in 'config'
        optionLogLevel (str): option in 'config'
    """

    if (logger is not None) and (config is not None):
        loglevelnum = getattr(logging, config[section][optionLogLevel].upper(), None)
        if isinstance(loglevelnum, int):
            logger.setLevel(loglevelnum)
        else:
            logger.setLevel(logging.INFO)

set_loglevel(logger: logging.Logger, config: configparser.ConfigParser, section: str, optionLogLevel: str) -> None

set_logLevel - set log level from cCallableonfigparser object

Parameters:

Name Type Description Default
logger Logger

name of logger

required
config configparser

configparser object

required
section str

section in 'config'

required
optionLogLevel str

option in 'config'

required
Source code in src/utils_mystuff/utils_various.py
def set_loglevel(logger: logging.Logger, config: configparser.ConfigParser, section: str, optionLogLevel: str) -> None:
    """
    set_logLevel - set log level from cCallableonfigparser object

    Args:
        logger (logging.Logger): name of logger
        config (configparser): configparser object
        section (str): section in 'config'
        optionLogLevel (str): option in 'config'
    """
    setLogLevel(logger, config, section, optionLogLevel)

set_loglevel_from_config(logger: logging.Logger, config: configparser.ConfigParser, section: str, optionLogLevel: str) -> None

set_loglevel_from_config - set log level from configparser object

Parameters:

Name Type Description Default
logger Logger

name of logger

required
config configparser

configparser object

required
section str

section in 'config'

required
optionLogLevel str

option in 'config'

required
Source code in src/utils_mystuff/utils_various.py
def set_loglevel_from_config(logger: logging.Logger, config: configparser.ConfigParser, section: str, optionLogLevel: str) -> None:
    """
    set_loglevel_from_config - set log level from configparser object

    Args:
        logger (logging.Logger): name of logger
        config (configparser): configparser object
        section (str): section in 'config'
        optionLogLevel (str): option in 'config'
    """
    setLogLevel(logger, config, section, optionLogLevel)

setlocale(locale_value: str)

setlocale - switch locale threadsafe

Parameters:

Name Type Description Default
locale_value str

target locale

required
Source code in src/utils_mystuff/utils_various.py
@contextmanager
def setlocale(locale_value: str):
    """
    setlocale - switch locale threadsafe

    Args:
        locale_value (str): target locale
    """

    with LOCALE_LOCK:
        saved_locale_value = locale.setlocale(locale.LC_ALL)
        try:
            yield locale.setlocale(locale.LC_ALL, locale_value)
        finally:
            locale.setlocale(locale.LC_ALL, saved_locale_value)

to_bool(value: Union[str, int, float, bool], truevalues: list[str] = ['true', 'yes', 'x', '1', '-1']) -> bool

to_bool - convert basic scalar types to bool

Parameters:

Name Type Description Default
value Union[str, int, float, bool]

value to be checked

required
truevalues list[str]

truthy str values. Defaults to ["true", "yes", "x", "1", "-1"].

['true', 'yes', 'x', '1', '-1']

Returns:

Name Type Description
bool bool

truthiness of value

Source code in src/utils_mystuff/utils_various.py
def to_bool(value: Union[str, int, float, bool], truevalues: list[str] = ["true", "yes", "x", "1", "-1"]) -> bool:
    """
    to_bool - convert basic scalar types to  bool

    Args:
        value (Union[str, int, float, bool]): value to be checked
        truevalues (list[str], optional): truthy str values. Defaults to ["true", "yes", "x", "1", "-1"].


    Returns:
        bool: truthiness of value
    """

    if type(value) is bool:
        return value
    elif type(value) in {int, float}:
        return value != 0
    elif type(value) is str:
        return value.lower() in truevalues
    else:
        return False

to_boolean(value: Union[str, int, float, bool], truevalues: list[str] = ['true', 'yes', 'x', '1', '-1']) -> bool

to_boolean - convert basic scalar types to bool

Parameters:

Name Type Description Default
value Union[str, int, float, bool]

value to be checked

required
truevalues list[str]

truthy str values. Defaults to ["true", "yes", "x", "1", "-1"].

['true', 'yes', 'x', '1', '-1']

Returns:

Name Type Description
bool bool

truthiness of value

Source code in src/utils_mystuff/utils_various.py
def to_boolean(value: Union[str, int, float, bool], truevalues: list[str] = ["true", "yes", "x", "1", "-1"]) -> bool:
    """
    to_boolean - convert basic scalar types to  bool

    Args:
        value (Union[str, int, float, bool]): value to be checked
        truevalues (list[str], optional): truthy str values. Defaults to ["true", "yes", "x", "1", "-1"].

    Returns:
        bool: truthiness of value
    """

    return to_bool(value, truevalues)