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/setters.py
7762 views
1
# SPDX-License-Identifier: MIT
2
3
"""
4
Commonly used hooks for on_setattr.
5
"""
6
7
from __future__ import absolute_import, division, print_function
8
9
from . import _config
10
from .exceptions import FrozenAttributeError
11
12
13
def pipe(*setters):
14
"""
15
Run all *setters* and return the return value of the last one.
16
17
.. versionadded:: 20.1.0
18
"""
19
20
def wrapped_pipe(instance, attrib, new_value):
21
rv = new_value
22
23
for setter in setters:
24
rv = setter(instance, attrib, rv)
25
26
return rv
27
28
return wrapped_pipe
29
30
31
def frozen(_, __, ___):
32
"""
33
Prevent an attribute to be modified.
34
35
.. versionadded:: 20.1.0
36
"""
37
raise FrozenAttributeError()
38
39
40
def validate(instance, attrib, new_value):
41
"""
42
Run *attrib*'s validator on *new_value* if it has one.
43
44
.. versionadded:: 20.1.0
45
"""
46
if _config._run_validators is False:
47
return new_value
48
49
v = attrib.validator
50
if not v:
51
return new_value
52
53
v(instance, attrib, new_value)
54
55
return new_value
56
57
58
def convert(instance, attrib, new_value):
59
"""
60
Run *attrib*'s converter -- if it has one -- on *new_value* and return the
61
result.
62
63
.. versionadded:: 20.1.0
64
"""
65
c = attrib.converter
66
if c:
67
return c(new_value)
68
69
return new_value
70
71
72
NO_OP = object()
73
"""
74
Sentinel for disabling class-wide *on_setattr* hooks for certain attributes.
75
76
Does not work in `pipe` or within lists.
77
78
.. versionadded:: 20.1.0
79
"""
80
81