Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
setters.py80 linesDownload Raw Back to attr
1# SPDX-License-Identifier: MIT2 3"""4Commonly used hooks for on_setattr.5"""6 7from . import _config8from .exceptions import FrozenAttributeError9 10 11def pipe(*setters):12    """13    Run all *setters* and return the return value of the last one.14 15    .. versionadded:: 20.1.016    """17 18    def wrapped_pipe(instance, attrib, new_value):19        rv = new_value20 21        for setter in setters:22            rv = setter(instance, attrib, rv)23 24        return rv25 26    return wrapped_pipe27 28 29def frozen(_, __, ___):30    """31    Prevent an attribute to be modified.32 33    .. versionadded:: 20.1.034    """35    raise FrozenAttributeError36 37 38def validate(instance, attrib, new_value):39    """40    Run *attrib*'s validator on *new_value* if it has one.41 42    .. versionadded:: 20.1.043    """44    if _config._run_validators is False:45        return new_value46 47    v = attrib.validator48    if not v:49        return new_value50 51    v(instance, attrib, new_value)52 53    return new_value54 55 56def convert(instance, attrib, new_value):57    """58    Run *attrib*'s converter -- if it has one -- on *new_value* and return the59    result.60 61    .. versionadded:: 20.1.062    """63    c = attrib.converter64    if c:65        # This can be removed once we drop 3.8 and use attrs.Converter instead.66        from ._make import Converter67 68        if not isinstance(c, Converter):69            return c(new_value)70 71        return c(new_value, instance, attrib)72 73    return new_value74 75 76# Sentinel for disabling class-wide *on_setattr* hooks for certain attributes.77# Sphinx's autodata stopped working, so the docstring is inlined in the API78# docs.79NO_OP = object()80 
codekingpro/portable-devtools · Team Ai