Skip to content

utils

Class info

Classes

Name Children Inherits
DataclassInstance
jinjarope.utils

    🛈 DocStrings

    fsspec_get cached

    fsspec_get(path: str | PathLike[str]) -> str
    

    Fetch a file via fsspec and return file content as a string.

    Parameters:

    Name Type Description Default
    path str | PathLike[str]

    The path to fetch the file from

    required
    Source code in src/jinjarope/utils.py
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    @functools.cache
    def fsspec_get(path: str | os.PathLike[str]) -> str:
        """Fetch a file via fsspec and return file content as a string.
    
        Args:
            path: The path to fetch the file from
        """
        import fsspec
    
        with fsspec.open(path) as file:
            return file.read().decode()
    

    get_dataclass_nondefault_values

    get_dataclass_nondefault_values(instance: DataclassInstance) -> dict[str, Any]
    

    Return dictionary with non-default key-value pairs of given dataclass.

    Parameters:

    Name Type Description Default
    instance DataclassInstance

    dataclass instance

    required
    Source code in src/jinjarope/utils.py
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    def get_dataclass_nondefault_values(
        instance: DataclassInstance,
    ) -> dict[str, Any]:
        """Return dictionary with non-default key-value pairs of given dataclass.
    
        Args:
            instance: dataclass instance
        """
        non_default_fields: dict[str, Any] = {}
    
        for field in dataclasses.fields(instance):
            value = getattr(instance, field.name)
    
            # Check if the field has a default value
            if field.default is not dataclasses.MISSING:
                default = field.default
            elif field.default_factory is not dataclasses.MISSING:
                default = field.default_factory()
            else:
                # If there's no default, we consider the current value as non-default
                non_default_fields[field.name] = value
                continue
    
            # Compare the current value with the default
            if value != default:
                non_default_fields[field.name] = value
        return non_default_fields
    

    get_hash

    get_hash(obj: Any, hash_length: int | None = 7) -> str
    

    Get a Md5 hash for given object.

    Parameters:

    Name Type Description Default
    obj Any

    The object to get a hash for ()

    required
    hash_length int | None

    Optional cut-off value to limit length

    7
    Source code in src/jinjarope/utils.py
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    def get_hash(obj: Any, hash_length: int | None = 7) -> str:
        """Get a Md5 hash for given object.
    
        Args:
            obj: The object to get a hash for ()
            hash_length: Optional cut-off value to limit length
        """
        import hashlib
    
        hash_md5 = hashlib.md5(str(obj).encode("utf-8"))
        return hash_md5.hexdigest()[:hash_length]
    

    get_repr

    get_repr(_obj: Any, *args: Any, **kwargs: Any) -> str
    

    Get a suitable repr string for an object.

    Parameters:

    Name Type Description Default
    _obj Any

    The object to get a repr for.

    required
    args Any

    Arguments for the repr

    ()
    kwargs Any

    Keyword arguments for the repr

    {}
    Source code in src/jinjarope/utils.py
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    def get_repr(_obj: Any, *args: Any, **kwargs: Any) -> str:
        """Get a suitable __repr__ string for an object.
    
        Args:
            _obj: The object to get a repr for.
            args: Arguments for the repr
            kwargs: Keyword arguments for the repr
        """
        classname = type(_obj).__name__
        parts = [repr(v) for v in args]
        kw_parts = [f"{k}={v!r}" for k, v in kwargs.items()]
        sig = ", ".join(parts + kw_parts)
        return f"{classname}({sig})"
    

    partial

    partial(fn: Callable, *args: Any, **kwargs: Any)
    

    Create new function with partial application of given arguments / keywords.

    Parameters:

    Name Type Description Default
    fn Callable

    The function to generate a partial from

    required
    args Any

    patially applied arguments

    ()
    kwargs Any

    partially applied keywords

    {}
    Source code in src/jinjarope/utils.py
    29
    30
    31
    32
    33
    34
    35
    36
    37
    def partial(fn: Callable, *args: Any, **kwargs: Any):
        """Create new function with partial application of given arguments / keywords.
    
        Args:
            fn: The function to generate a partial from
            args: patially applied arguments
            kwargs: partially applied keywords
        """
        return functools.partial(fn, *args, **kwargs)
    

    resolve cached

    resolve(name: str, module: str | None = None) -> ModuleType | Callable[..., Any]
    

    Resolve name to a Python object via imports / attribute lookups.

    If module is None, name must be "absolute" (no leading dots).

    If module is not None, and name is "relative" (has leading dots), the object will be found by navigating relative to module.

    Returns the object, if found. If not, propagates the error.

    Source code in src/jinjarope/utils.py
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    @functools.cache
    def resolve(
        name: str,
        module: str | None = None,
    ) -> types.ModuleType | Callable[..., Any]:
        """Resolve ``name`` to a Python object via imports / attribute lookups.
    
        If ``module`` is None, ``name`` must be "absolute" (no leading dots).
    
        If ``module`` is not None, and ``name`` is "relative" (has leading dots),
        the object will be found by navigating relative to ``module``.
    
        Returns the object, if found.  If not, propagates the error.
        """
        names = name.split(".")
        if not names[0]:
            if module is None:
                msg = "relative name without base module"
                raise ValueError(msg)
            modules = module.split(".")
            names.pop(0)
            while not name[0]:
                modules.pop()
                names.pop(0)
            names = modules + names
    
        used = names.pop(0)
        if envtests.is_python_builtin(used):
            import builtins
    
            return getattr(builtins, used)
        found = importlib.import_module(used)
        for n in names:
            used += "." + n
            try:
                found = getattr(found, n)
            except AttributeError:
                try:
                    importlib.import_module(used)
                    found = getattr(found, n)
                except ModuleNotFoundError:
                    mod = ".".join(used.split(".")[:-1])
                    importlib.import_module(mod)
                    found = getattr(found, n)
        return found