comparison env/lib/python3.9/site-packages/attr/setters.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """
2 Commonly used hooks for on_setattr.
3 """
4
5 from __future__ import absolute_import, division, print_function
6
7 from . import _config
8 from .exceptions import FrozenAttributeError
9
10
11 def pipe(*setters):
12 """
13 Run all *setters* and return the return value of the last one.
14
15 .. versionadded:: 20.1.0
16 """
17
18 def wrapped_pipe(instance, attrib, new_value):
19 rv = new_value
20
21 for setter in setters:
22 rv = setter(instance, attrib, rv)
23
24 return rv
25
26 return wrapped_pipe
27
28
29 def frozen(_, __, ___):
30 """
31 Prevent an attribute to be modified.
32
33 .. versionadded:: 20.1.0
34 """
35 raise FrozenAttributeError()
36
37
38 def validate(instance, attrib, new_value):
39 """
40 Run *attrib*'s validator on *new_value* if it has one.
41
42 .. versionadded:: 20.1.0
43 """
44 if _config._run_validators is False:
45 return new_value
46
47 v = attrib.validator
48 if not v:
49 return new_value
50
51 v(instance, attrib, new_value)
52
53 return new_value
54
55
56 def convert(instance, attrib, new_value):
57 """
58 Run *attrib*'s converter -- if it has one -- on *new_value* and return the
59 result.
60
61 .. versionadded:: 20.1.0
62 """
63 c = attrib.converter
64 if c:
65 return c(new_value)
66
67 return new_value
68
69
70 NO_OP = object()
71 """
72 Sentinel for disabling class-wide *on_setattr* hooks for certain attributes.
73
74 Does not work in `pipe` or within lists.
75
76 .. versionadded:: 20.1.0
77 """