Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/attr/converters.py
7770 views
1
# SPDX-License-Identifier: MIT
2
3
"""
4
Commonly useful converters.
5
"""
6
7
from __future__ import absolute_import, division, print_function
8
9
from ._compat import PY2
10
from ._make import NOTHING, Factory, pipe
11
12
13
if not PY2:
14
import inspect
15
import typing
16
17
18
__all__ = [
19
"default_if_none",
20
"optional",
21
"pipe",
22
"to_bool",
23
]
24
25
26
def optional(converter):
27
"""
28
A converter that allows an attribute to be optional. An optional attribute
29
is one which can be set to ``None``.
30
31
Type annotations will be inferred from the wrapped converter's, if it
32
has any.
33
34
:param callable converter: the converter that is used for non-``None``
35
values.
36
37
.. versionadded:: 17.1.0
38
"""
39
40
def optional_converter(val):
41
if val is None:
42
return None
43
return converter(val)
44
45
if not PY2:
46
sig = None
47
try:
48
sig = inspect.signature(converter)
49
except (ValueError, TypeError): # inspect failed
50
pass
51
if sig:
52
params = list(sig.parameters.values())
53
if params and params[0].annotation is not inspect.Parameter.empty:
54
optional_converter.__annotations__["val"] = typing.Optional[
55
params[0].annotation
56
]
57
if sig.return_annotation is not inspect.Signature.empty:
58
optional_converter.__annotations__["return"] = typing.Optional[
59
sig.return_annotation
60
]
61
62
return optional_converter
63
64
65
def default_if_none(default=NOTHING, factory=None):
66
"""
67
A converter that allows to replace ``None`` values by *default* or the
68
result of *factory*.
69
70
:param default: Value to be used if ``None`` is passed. Passing an instance
71
of `attrs.Factory` is supported, however the ``takes_self`` option
72
is *not*.
73
:param callable factory: A callable that takes no parameters whose result
74
is used if ``None`` is passed.
75
76
:raises TypeError: If **neither** *default* or *factory* is passed.
77
:raises TypeError: If **both** *default* and *factory* are passed.
78
:raises ValueError: If an instance of `attrs.Factory` is passed with
79
``takes_self=True``.
80
81
.. versionadded:: 18.2.0
82
"""
83
if default is NOTHING and factory is None:
84
raise TypeError("Must pass either `default` or `factory`.")
85
86
if default is not NOTHING and factory is not None:
87
raise TypeError(
88
"Must pass either `default` or `factory` but not both."
89
)
90
91
if factory is not None:
92
default = Factory(factory)
93
94
if isinstance(default, Factory):
95
if default.takes_self:
96
raise ValueError(
97
"`takes_self` is not supported by default_if_none."
98
)
99
100
def default_if_none_converter(val):
101
if val is not None:
102
return val
103
104
return default.factory()
105
106
else:
107
108
def default_if_none_converter(val):
109
if val is not None:
110
return val
111
112
return default
113
114
return default_if_none_converter
115
116
117
def to_bool(val):
118
"""
119
Convert "boolean" strings (e.g., from env. vars.) to real booleans.
120
121
Values mapping to :code:`True`:
122
123
- :code:`True`
124
- :code:`"true"` / :code:`"t"`
125
- :code:`"yes"` / :code:`"y"`
126
- :code:`"on"`
127
- :code:`"1"`
128
- :code:`1`
129
130
Values mapping to :code:`False`:
131
132
- :code:`False`
133
- :code:`"false"` / :code:`"f"`
134
- :code:`"no"` / :code:`"n"`
135
- :code:`"off"`
136
- :code:`"0"`
137
- :code:`0`
138
139
:raises ValueError: for any other value.
140
141
.. versionadded:: 21.3.0
142
"""
143
if isinstance(val, str):
144
val = val.lower()
145
truthy = {True, "true", "t", "yes", "y", "on", "1", 1}
146
falsy = {False, "false", "f", "no", "n", "off", "0", 0}
147
try:
148
if val in truthy:
149
return True
150
if val in falsy:
151
return False
152
except TypeError:
153
# Raised when "val" is not hashable (e.g., lists)
154
pass
155
raise ValueError("Cannot convert value to bool: {}".format(val))
156
157