Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/attr/converters.py
7770 views
# SPDX-License-Identifier: MIT12"""3Commonly useful converters.4"""56from __future__ import absolute_import, division, print_function78from ._compat import PY29from ._make import NOTHING, Factory, pipe101112if not PY2:13import inspect14import typing151617__all__ = [18"default_if_none",19"optional",20"pipe",21"to_bool",22]232425def optional(converter):26"""27A converter that allows an attribute to be optional. An optional attribute28is one which can be set to ``None``.2930Type annotations will be inferred from the wrapped converter's, if it31has any.3233:param callable converter: the converter that is used for non-``None``34values.3536.. versionadded:: 17.1.037"""3839def optional_converter(val):40if val is None:41return None42return converter(val)4344if not PY2:45sig = None46try:47sig = inspect.signature(converter)48except (ValueError, TypeError): # inspect failed49pass50if sig:51params = list(sig.parameters.values())52if params and params[0].annotation is not inspect.Parameter.empty:53optional_converter.__annotations__["val"] = typing.Optional[54params[0].annotation55]56if sig.return_annotation is not inspect.Signature.empty:57optional_converter.__annotations__["return"] = typing.Optional[58sig.return_annotation59]6061return optional_converter626364def default_if_none(default=NOTHING, factory=None):65"""66A converter that allows to replace ``None`` values by *default* or the67result of *factory*.6869:param default: Value to be used if ``None`` is passed. Passing an instance70of `attrs.Factory` is supported, however the ``takes_self`` option71is *not*.72:param callable factory: A callable that takes no parameters whose result73is used if ``None`` is passed.7475:raises TypeError: If **neither** *default* or *factory* is passed.76:raises TypeError: If **both** *default* and *factory* are passed.77:raises ValueError: If an instance of `attrs.Factory` is passed with78``takes_self=True``.7980.. versionadded:: 18.2.081"""82if default is NOTHING and factory is None:83raise TypeError("Must pass either `default` or `factory`.")8485if default is not NOTHING and factory is not None:86raise TypeError(87"Must pass either `default` or `factory` but not both."88)8990if factory is not None:91default = Factory(factory)9293if isinstance(default, Factory):94if default.takes_self:95raise ValueError(96"`takes_self` is not supported by default_if_none."97)9899def default_if_none_converter(val):100if val is not None:101return val102103return default.factory()104105else:106107def default_if_none_converter(val):108if val is not None:109return val110111return default112113return default_if_none_converter114115116def to_bool(val):117"""118Convert "boolean" strings (e.g., from env. vars.) to real booleans.119120Values mapping to :code:`True`:121122- :code:`True`123- :code:`"true"` / :code:`"t"`124- :code:`"yes"` / :code:`"y"`125- :code:`"on"`126- :code:`"1"`127- :code:`1`128129Values mapping to :code:`False`:130131- :code:`False`132- :code:`"false"` / :code:`"f"`133- :code:`"no"` / :code:`"n"`134- :code:`"off"`135- :code:`"0"`136- :code:`0`137138:raises ValueError: for any other value.139140.. versionadded:: 21.3.0141"""142if isinstance(val, str):143val = val.lower()144truthy = {True, "true", "t", "yes", "y", "on", "1", 1}145falsy = {False, "false", "f", "no", "n", "off", "0", 0}146try:147if val in truthy:148return True149if val in falsy:150return False151except TypeError:152# Raised when "val" is not hashable (e.g., lists)153pass154raise ValueError("Cannot convert value to bool: {}".format(val))155156157