|
1 | | -# Common arguments to all properties |
| 1 | +# Common arguments to all properties |
2 | 2 |
|
3 | 3 | [API Reference](../../api-reference/property) |
4 | 4 |
|
| 5 | +### `allow_None`, `constant` & `readonly` |
5 | 6 |
|
6 | | -### `allow_None`, `constant` & `readonly` |
7 | | - |
8 | | -* if `allow_None` is `True`, property supports `None` apart from its own type |
9 | | -* `readonly` (being `True`) makes the property read-only or execute the getter method |
10 | | -* `constant` (being `True`), again makes the property read-only but can be set once if `allow_None` is `True`. |
| 7 | +- if `allow_None` is `True`, property supports `None` apart from its own type |
| 8 | +- `readonly` (being `True`) makes the property read-only or execute the getter method |
| 9 | +- `constant` (being `True`), again makes the property read-only but can be set once if `allow_None` is `True`. |
11 | 10 | This is useful to set the property once at `__init__()` but remain constant after that. |
12 | 11 |
|
13 | 12 | ```py title="allow None, constant and readonly" linenums="1" hl_lines="7 15 19" |
14 | | ---8<-- "docs/howto/code/properties/common_args_1.py:1:24" |
| 13 | +--8<-- "docs/beginners-guide/code/properties/common_args_1.py:1:24" |
15 | 14 | ``` |
16 | 15 |
|
17 | 16 | === "`allow_None=True`" |
18 | 17 |
|
19 | 18 | ```py title="allow None, constant and readonly" linenums="1" |
20 | | - --8<-- "docs/howto/code/properties/common_args_1.py:26:28" |
| 19 | + --8<-- "docs/beginners-guide/code/properties/common_args_1.py:26:28" |
21 | 20 | ``` |
22 | 21 |
|
23 | 22 | === "`allow_None=False`" |
24 | 23 |
|
25 | 24 | ```py title="allow None, constant and readonly" linenums="1" |
26 | | - --8<-- "docs/howto/code/properties/common_args_1.py:31:32" |
| 25 | + --8<-- "docs/beginners-guide/code/properties/common_args_1.py:31:32" |
27 | 26 | ``` |
28 | 27 |
|
29 | 28 | === "`readonly=True`" |
30 | 29 |
|
31 | 30 | ```py title="allow None, constant and readonly" linenums="1" |
32 | | - --8<-- "docs/howto/code/properties/common_args_1.py:35:35" |
| 31 | + --8<-- "docs/beginners-guide/code/properties/common_args_1.py:35:35" |
33 | 32 | ``` |
34 | 33 |
|
35 | 34 | === "`constant=True`" |
36 | 35 |
|
37 | 36 | ```py title="allow None, constant and readonly" linenums="1" |
38 | | - --8<-- "docs/howto/code/properties/common_args_1.py:38:40" |
| 37 | + --8<-- "docs/beginners-guide/code/properties/common_args_1.py:38:40" |
39 | 38 | ``` |
40 | 39 |
|
41 | | - |
42 | 40 | ### `doc` and `label` |
43 | 41 |
|
44 | | -`doc` allows clients to fetch a docstring for the property. `label` can be used to show the property |
45 | | -in a GUI for example. |
| 42 | +`doc` allows clients to fetch a docstring for the property. `label` can be used to show the property |
| 43 | +in a GUI for example. |
46 | 44 |
|
47 | 45 | ```py title="allow None, constant and readonly" linenums="1" hl_lines="8-9" |
48 | | ---8<-- "docs/howto/code/properties/common_args_1.py:1:6" |
49 | | ---8<-- "docs/howto/code/properties/common_args_1.py:11:14" |
| 46 | +--8<-- "docs/beginners-guide/code/properties/common_args_1.py:1:6" |
| 47 | +--8<-- "docs/beginners-guide/code/properties/common_args_1.py:11:14" |
50 | 48 | ``` |
51 | 49 |
|
52 | | - |
53 | 50 | ### `default`, `fget`, `fset` & `fdel` |
54 | 51 |
|
55 | | -To provide a getter-setter (& deleter) method is optional. If none given, when the property is set/written, the value |
56 | | -is stored inside the instance's `__dict__` under the name `<given property name >_param_value` |
57 | | -(for example, `serial_number_param_value` for `serial_number`). In layman's terms, |
58 | | -`__dict__` is the internal map where the attributes of the object are stored by python. |
| 52 | +To provide a getter-setter (& deleter) method is optional. If none given, when the property is set/written, the value |
| 53 | +is stored inside the instance's `__dict__` under the name `<given property name >_param_value` |
| 54 | +(for example, `serial_number_param_value` for `serial_number`). In layman's terms, |
| 55 | +`__dict__` is the internal map where the attributes of the object are stored by python. |
59 | 56 |
|
60 | | -When a value assignment was never called on the property, `default` is returned when reading the value. This is the purpose of the `default` argument. If a setter/deleter is given, getter is mandatory. In this case, `default` is also ignored & the getter is always executed. |
| 57 | +When a value assignment was never called on the property, `default` is returned when reading the value. This is the purpose of the `default` argument. If a setter/deleter is given, getter is mandatory. In this case, `default` is also ignored & the getter is always executed. |
61 | 58 |
|
62 | 59 | === "with decorator" |
63 | 60 |
|
64 | | - ```py linenums="1" |
65 | | - --8<-- "docs/howto/code/properties/common_args_2.py:22:42" |
66 | | - --8<-- "docs/howto/code/properties/common_args_2.py:46:48" |
67 | | - --8<-- "docs/howto/code/properties/common_args_2.py:98:102" |
| 61 | + ```py linenums="1" |
| 62 | + --8<-- "docs/beginners-guide/code/properties/common_args_2.py:22:42" |
| 63 | + --8<-- "docs/beginners-guide/code/properties/common_args_2.py:46:48" |
| 64 | + --8<-- "docs/beginners-guide/code/properties/common_args_2.py:98:102" |
68 | 65 | ``` |
| 66 | + |
69 | 67 | === "with fget-fset-fdel arguments" |
70 | 68 |
|
71 | | - ```py linenums="1" |
72 | | - --8<-- "docs/howto/code/properties/common_args_2.py:22:24" |
73 | | - --8<-- "docs/howto/code/properties/common_args_2.py:29:34" |
74 | | - --8<-- "docs/howto/code/properties/common_args_2.py:36:48" |
75 | | - --8<-- "docs/howto/code/properties/common_args_2.py:98:102" |
| 69 | + ```py linenums="1" |
| 70 | + --8<-- "docs/beginners-guide/code/properties/common_args_2.py:22:24" |
| 71 | + --8<-- "docs/beginners-guide/code/properties/common_args_2.py:29:34" |
| 72 | + --8<-- "docs/beginners-guide/code/properties/common_args_2.py:36:48" |
| 73 | + --8<-- "docs/beginners-guide/code/properties/common_args_2.py:98:102" |
76 | 74 | ``` |
77 | 75 |
|
78 | | -If default is desirable, one has to return it manually in the getter method by accessing the property [descriptor object directly](../#__codelineno-2-15). |
79 | | - |
| 76 | +If default is desirable, one has to return it manually in the getter method by accessing the property [descriptor object directly](../#__codelineno-2-15). |
80 | 77 |
|
81 | 78 | ### `class_member` |
82 | 79 |
|
83 | | -If `class_member` is True, the value is set in the class' `__dict__` (i.e. becomes a class attribute) |
84 | | -instead of instance's `__dict__` (instance's attribute). |
85 | | -Custom getter-setter-deleter are not compatible with this option currently. `class_member` takes precedence over fget-fset-fdel, |
| 80 | +If `class_member` is True, the value is set in the class' `__dict__` (i.e. becomes a class attribute) |
| 81 | +instead of instance's `__dict__` (instance's attribute). |
| 82 | +Custom getter-setter-deleter are not compatible with this option currently. `class_member` takes precedence over fget-fset-fdel, |
86 | 83 | which in turn has precedence over `default`. |
87 | 84 |
|
88 | 85 | ```py title="class member" linenums="1" hl_lines="21-22" |
89 | | ---8<-- "docs/howto/code/properties/common_args_2.py:7:20" |
90 | | ---8<-- "docs/howto/code/properties/common_args_2.py:22:24" |
91 | | ---8<-- "docs/howto/code/properties/common_args_2.py:51:57" |
92 | | ---8<-- "docs/howto/code/properties/common_args_2.py:98:100" |
93 | | ---8<-- "docs/howto/code/properties/common_args_2.py:103:106" |
| 86 | +--8<-- "docs/beginners-guide/code/properties/common_args_2.py:7:20" |
| 87 | +--8<-- "docs/beginners-guide/code/properties/common_args_2.py:22:24" |
| 88 | +--8<-- "docs/beginners-guide/code/properties/common_args_2.py:51:57" |
| 89 | +--8<-- "docs/beginners-guide/code/properties/common_args_2.py:98:100" |
| 90 | +--8<-- "docs/beginners-guide/code/properties/common_args_2.py:103:106" |
94 | 91 | ``` |
95 | 92 |
|
96 | | -`class_member` can still be used with a default value if there is no custom fget-fset-fdel. |
97 | | - |
| 93 | +`class_member` can still be used with a default value if there is no custom fget-fset-fdel. |
98 | 94 |
|
99 | 95 | ### `remote` |
100 | 96 |
|
101 | 97 | setting `remote` to False makes the inaccessible to a client but accessible to the object locally. This is still useful to type-restrict python attributes to provide an interface to other developers using your class, for example, when someone else inherits your `Thing`. For example, the `Thing`'s `logger` is implemented in this fashion: |
102 | 98 |
|
103 | 99 | ```py title="local properties" linenums="1" hl_lines="8" |
104 | | -import logging |
| 100 | +import logging |
105 | 101 | from hololinked.server.properties import ClassSelector |
106 | 102 |
|
107 | 103 | class Thing: |
108 | 104 | """Subclass from here to expose hardware or python objects on the network""" |
109 | | - |
| 105 | + |
110 | 106 | logger = ClassSelector(class_=logging.Logger, default=None, allow_None=True, |
111 | 107 | remote=False, |
112 | | - doc="""logging.Logger instance to print log messages. |
113 | | - Default logger with a IO-stream handler and network |
| 108 | + doc="""logging.Logger instance to print log messages. |
| 109 | + Default logger with a IO-stream handler and network |
114 | 110 | accessible handler is created if none supplied.""" |
115 | 111 | ) # type: logging.Logger |
116 | 112 | ``` |
117 | 113 |
|
118 | | - |
119 | 114 | ### `state` |
120 | 115 |
|
121 | | -When `state` is specifed, the property is writeable only when the `Thing`'s `StateMachine` is in that specified state (or |
122 | | -in the list of allowed states): |
| 116 | +When `state` is specifed, the property is writeable only when the `Thing`'s `StateMachine` is in that specified state (or |
| 117 | +in the list of allowed states): |
123 | 118 |
|
124 | | -```py title="state machine state" linenums="1" hl_lines="17" |
125 | | ---8<-- "docs/howto/code/properties/common_args_2.py:22:24" |
126 | | ---8<-- "docs/howto/code/properties/common_args_2.py:60:76" |
| 119 | +```py title="state machine state" linenums="1" hl_lines="17" |
| 120 | +--8<-- "docs/beginners-guide/code/properties/common_args_2.py:22:24" |
| 121 | +--8<-- "docs/beginners-guide/code/properties/common_args_2.py:60:76" |
127 | 122 | ``` |
128 | 123 |
|
129 | | -This is also currently applicable only when set operations are called by clients. Local set operations are always executed irrespective of the state machine state. A get operation is always executed as well even from the clients irrespective of the state. |
| 124 | +This is also currently applicable only when set operations are called by clients. Local set operations are always executed irrespective of the state machine state. A get operation is always executed as well even from the clients irrespective of the state. |
130 | 125 |
|
131 | | -`observable` |
132 | | ------------- |
| 126 | +## `observable` |
133 | 127 |
|
134 | 128 | Observable properties push change events when the property is set or read. This is useful when one wants to monitor the |
135 | | -property for changes without polling from the client. The payload of the change event is the new value of the property. |
| 129 | +property for changes without polling from the client. The payload of the change event is the new value of the property. |
136 | 130 |
|
137 | | -```py title="state machine state" linenums="1" hl_lines="19" |
138 | | ---8<-- "docs/howto/code/properties/common_args_2.py:22:24" |
139 | | ---8<-- "docs/howto/code/properties/common_args_2.py:79:95" |
| 131 | +```py title="state machine state" linenums="1" hl_lines="19" |
| 132 | +--8<-- "docs/beginners-guide/code/properties/common_args_2.py:22:24" |
| 133 | +--8<-- "docs/beginners-guide/code/properties/common_args_2.py:79:95" |
140 | 134 | ``` |
141 | 135 |
|
142 | | -`metadata` |
143 | | ----------- |
| 136 | +## `metadata` |
144 | 137 |
|
145 | | -`metadata` is a dictionary that allows storing arbitrary metadata about the property. For example, one can store units of the physical |
146 | | -quantity. |
| 138 | +`metadata` is a dictionary that allows storing arbitrary metadata about the property. For example, one can store units of the physical |
| 139 | +quantity. |
147 | 140 |
|
148 | | -`db_init`, `db_commit` & `db_persist` |
149 | | -------------------------------------- |
| 141 | +## `db_init`, `db_commit` & `db_persist` |
150 | 142 |
|
151 | | -Properties can be stored in a file or a database and loaded from them when the `Thing` is stopped and restarted. This is useful especially to preserve the settings of the hardware when the server undergoes a restart, either through system restart or any other reason. |
| 143 | +Properties can be stored in a file or a database and loaded from them when the `Thing` is stopped and restarted. This is useful especially to preserve the settings of the hardware when the server undergoes a restart, either through system restart or any other reason. |
152 | 144 |
|
153 | | -* `db_init` only loads a property from database, when the value is changed, its not written back to the database. |
| 145 | +- `db_init` only loads a property from database, when the value is changed, its not written back to the database. |
154 | 146 | For this option, the value has to be pre-created in the database in some other fashion. |
155 | 147 |
|
156 | | -* `db_commit` only writes the value into the database when an assignment is called. |
157 | | - |
158 | | -* `db_persist` both stores and loads the property from the database. |
| 148 | +- `db_commit` only writes the value into the database when an assignment is called. |
159 | 149 |
|
160 | | -Supported databases are MySQL, Postgres & SQLite currently. Look at database how-to for supply database configuration. |
| 150 | +- `db_persist` both stores and loads the property from the database. |
161 | 151 |
|
| 152 | +Supported databases are MySQL, Postgres & SQLite currently. Look at database how-to for supply database configuration. |
0 commit comments