Skip to content

omegaconfutils

omegaconfutils

Module for all Omegaconf utils

Classes

StringSepResolver

StringSepResolver(seperator=',', cast_type=None)

OmegaConf resolver which extracts values from a string using the seperator

Parameters:

  • seperator (str, default: ',' ) –

    Used to split the input str

  • cast_type

    Optional; If given it is used to convert the value to a given type

Source code in niceml/utilities/omegaconfutils.py
def __init__(self, seperator: str = ",", cast_type=None):
    self.seperator = seperator
    self.cast_type = cast_type
Functions
__call__
__call__(incoming_str, index)

Extracts a value from 'incoming_str' by index Args: incoming_str: str with target information (e.g. '124,264') index: target index (e.g. 1 -> 264)

Source code in niceml/utilities/omegaconfutils.py
def __call__(self, incoming_str: str, index: int):
    """
    Extracts a value from 'incoming_str' by index
    Args:
        incoming_str: str with target information (e.g. '124,264')
        index: target index (e.g. 1 -> 264)
    """
    index = int(index)
    res = incoming_str.split(sep=self.seperator)[index]
    if self.cast_type is not None:
        res = self.cast_type(res)
    return res

TrueDivResolver

OmegaConf resolver which divides two values and returns an int

TypeCastResolver

TypeCastResolver(cast_type)

Converts a string to a given type

Source code in niceml/utilities/omegaconfutils.py
def __init__(self, cast_type):
    self.cast_type = cast_type

Functions

register_niceml_resolvers

register_niceml_resolvers()

register all niceml OmegaConf resolvers

Source code in niceml/utilities/omegaconfutils.py
def register_niceml_resolvers():
    """register all niceml OmegaConf resolvers"""
    # If this function is called twice, the Resolvers are already registered.
    # In this case, the upcoming ValueError is ignored.
    with contextlib.suppress(ValueError):
        OmegaConf.register_new_resolver(
            "niceml.extract_int", StringSepResolver(cast_type=int)
        )
        OmegaConf.register_new_resolver(
            "niceml.extract_float", StringSepResolver(cast_type=float)
        )
        OmegaConf.register_new_resolver("niceml.extract_raw", StringSepResolver())
        OmegaConf.register_new_resolver("niceml.to_int", TypeCastResolver(int))
        OmegaConf.register_new_resolver("niceml.to_float", TypeCastResolver(float))
        OmegaConf.register_new_resolver("niceml.to_bool", TypeCastResolver(str_to_bool))
        OmegaConf.register_new_resolver("niceml.true_div", TrueDivResolver())