-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.txt
350 lines (253 loc) · 9.72 KB
/
README.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
===========
namedlist
===========
WARNING
=======
This package is no longer maintained, except for exceptional cases.
Please use the built-in dataclasses module instead.
Overview
========
namedlist provides 2 factory functions, namedlist.namedlist and
namedlist.namedtuple. namedlist.namedtuple is similar to
collections.namedtuple, with the following differences:
* namedlist.namedtuple supports per-field default values.
* namedlist.namedtuple supports an optional default value, to be used
by all fields that do not have an explicit default value.
namedlist.namedlist is similar, with this additional difference:
* namedlist.namedlist instances are mutable.
Typical usage
=============
You can use namedlist like a mutable namedtuple::
>>> from namedlist import namedlist
>>> Point = namedlist('Point', 'x y')
>>> p = Point(1, 3)
>>> p.x = 2
>>> assert p.x == 2
>>> assert p.y == 3
Or, you can specify a default value for all fields::
>>> Point = namedlist('Point', 'x y', default=3)
>>> p = Point(y=2)
>>> assert p.x == 3
>>> assert p.y == 2
Or, you can specify per-field default values::
>>> Point = namedlist('Point', [('x', 0), ('y', 100)])
>>> p = Point()
>>> assert p.x == 0
>>> assert p.y == 100
You can also specify the per-field defaults with a mapping, instead
of an iterable. Note that this is only useful with an ordered
mapping, such as an OrderedDict::
>>> from collections import OrderedDict
>>> Point = namedlist('Point', OrderedDict((('y', 0),
... ('x', 100))))
>>> p = Point()
>>> p
Point(y=0, x=100)
The default value will only be used if it is provided and a per-field
default is not used::
>>> Point = namedlist('Point', ['x', ('y', 100)], default=10)
>>> p = Point()
>>> assert p.x == 10
>>> assert p.y == 100
If you use a mapping, the value NO_DEFAULT is convenient to specify
that a field uses the default value::
>>> from namedlist import NO_DEFAULT
>>> Point = namedlist('Point', OrderedDict((('y', NO_DEFAULT),
... ('x', 100))),
... default=5)
>>> p = Point()
>>> assert p.x == 100
>>> assert p.y == 5
namedtuple is similar, except the instances are immutable::
>>> from namedlist import namedtuple
>>> Point = namedtuple('Point', 'x y', default=3)
>>> p = Point(y=2)
>>> assert p.x == 3
>>> assert p.y == 2
>>> p.x = 10
Traceback (most recent call last):
...
AttributeError: can't set attribute
All of the documentation below in the Specifying Fields and Specifying
Defaults sections applies to namedlist.namedlist and
namedlist.namedtuple.
Creating types
==============
Specifying Fields
-----------------
Fields in namedlist.namedlist or namedlist.namedtuple can be specified
as in collections.namedtuple: as either a string specifing the field
names, or as a iterable of field names. These two uses are
equivalent::
>>> Point = namedlist('Point', 'x y')
>>> Point = namedlist('Point', ['x', 'y'])
As are these::
>>> Point = namedtuple('Point', 'x y')
>>> Point = namedtuple('Point', ['x', 'y'])
If using a string, commas are first converted to spaces. So these are
equivalent::
>>> Point = namedlist('Point', 'x y')
>>> Point = namedlist('Point', 'x,y')
Specifying Defaults
-------------------
Per-field defaults can be specified by supplying a 2-tuple (name,
default_value) instead of just a string for the field name. This is
only supported when you specify a list of field names::
>>> Point = namedlist('Point', [('x', 0), ('y', 0)])
>>> p = Point(3)
>>> assert p.x == 3
>>> assert p.y == 0
Or, using namedtuple::
>>> Point = namedtuple('Point', [('x', 0), ('y', 0)])
>>> p = Point(3)
>>> assert p.x == 3
>>> assert p.y == 0
In addition to, or instead of, these per-field defaults, you can also
specify a default value which is used when no per-field default value
is specified::
>>> Point = namedlist('Point', 'x y z', default=0)
>>> p = Point(y=3)
>>> assert p.x == 0
>>> assert p.y == 3
>>> assert p.z == 0
>>> Point = namedlist('Point', [('x', 0), 'y', ('z', 0)], default=4)
>>> p = Point(z=2)
>>> assert p.x == 0
>>> assert p.y == 4
>>> assert p.z == 2
In addition to supplying the field names as an iterable of 2-tuples,
you can also specify a mapping. The keys will be the field names, and
the values will be the per-field default values. This is most useful
with an OrderedDict, as the order of the fields will then be
deterministic. The module variable NO_DEFAULT can be specified if you
want a field to use the per-type default value instead of specifying
it with a field::
>>> Point = namedlist('Point', OrderedDict((('x', 0),
... ('y', NO_DEFAULT),
... ('z', 0),
... )),
... default=4)
>>> p = Point(z=2)
>>> assert p.x == 0
>>> assert p.y == 4
>>> assert p.z == 2
Writing to values
-----------------
Instances of the classes generated by namedlist.namedlist are fully
writable, unlike the tuple-derived classes returned by
collections.namedtuple or namedlist.namedtuple::
>>> Point = namedlist('Point', 'x y')
>>> p = Point(1, 2)
>>> p.y = 4
>>> assert p.x == 1
>>> assert p.y == 4
Specifying __slots__
--------------------
For namedlist.namedlist, by default, the returned class sets __slots__
which is initialized to the field names. While this decreases memory
usage by eliminating the instance dict, it also means that you cannot
create new instance members.
To change this behavior, specify use_slots=False when creating the
namedlist::
>>> Point = namedlist('Point', 'x y', use_slots=False)
>>> p = Point(0, 1)
>>> p.z = 2
>>> assert p.x == 0
>>> assert p.y == 1
>>> assert p.z == 2
However, note that this method does not add the new variable to
_fields, so it is not recognized when iterating over the instance::
>>> list(p)
[0, 1]
>>> sorted(p._asdict().items())
[('x', 0), ('y', 1)]
Additional class members
------------------------
namedlist.namedlist and namedlist.namedtuple classes contain these members:
* _asdict(): Returns a dict which maps field names to their
corresponding values.
* _fields: Tuple of strings listing the field names. Useful for introspection.
Renaming invalid field names
----------------------------
This functionality is identical to collections.namedtuple. If you
specify rename=True, then any invalid field names are changed to _0,
_1, etc. Reasons for a field name to be invalid are:
* Zero length strings.
* Containing characters other than alphanumerics and underscores.
* A conflict with a Python reserved identifier.
* Beginning with a digit.
* Beginning with an underscore.
* Using the same field name more than once.
For example::
>>> Point = namedlist('Point', 'x x for', rename=True)
>>> assert Point._fields == ('x', '_1', '_2')
Mutable default values
----------------------
For namedlist.namelist, be aware of specifying mutable default
values. Due to the way Python handles default values, each instance of
a namedlist will share the default. This is especially problematic
with default values that are lists. For example::
>>> A = namedlist('A', [('x', [])])
>>> a = A()
>>> a.x.append(4)
>>> b = A()
>>> assert b.x == [4]
This is probably not the desired behavior, so see the next section.
Specifying a factory function for default values
------------------------------------------------
For namedlist.namedlist, you can supply a zero-argument callable for a
default, by wrapping it in a FACTORY call. The only change in this
example is to change the default from `[]` to `FACTORY(list)`. But
note that `b.x` is a new list object, not shared with `a.x`::
>>> from namedlist import FACTORY
>>> A = namedlist('A', [('x', FACTORY(list))])
>>> a = A()
>>> a.x.append(4)
>>> b = A()
>>> assert b.x == []
Every time a new instance is created, your callable (in this case,
`list`), will be called to produce a new instance for the default
value.
Iterating over instances
------------------------
Because instances are iterable (like lists or tuples), iteration works
the same way. Values are returned in definition order::
>>> Point = namedlist('Point', 'x y z t')
>>> p = Point(1.0, 42.0, 3.14, 2.71828)
>>> for value in p:
... print(value)
1.0
42.0
3.14
2.71828
namedlist specific functions
============================
_update
-------
`namedlist._update()` is similar to `dict.update()`. It is used to
mutate a namedlist.namedlist instance with new values::
>>> Point = namedlist('Point', 'x y z')
>>> p = Point(1, 2, 3)
>>> p.z = 4
>>> p._update(y=5, x=6)
>>> p
Point(x=6, y=5, z=4)
>>> p._update({'x': 7, 'z': 8})
>>> p
Point(x=7, y=5, z=8)
>>> p._update([('z', 9), ('y', 10)])
>>> p
Point(x=7, y=10, z=9)
Creating and using instances
============================
Because the type returned by namedlist or namedtuple is a normal
Python class, you create instances as you would with any Python class.
Bitbucket vs. GitLab
====================
The repository used to be on Bitbucket in Mercurial format. But
Bitbucket dropped Mercurial support and did not provide any way to
migrate issues to a git repository, even one hosted on Bitbucket. So,
I abandoned Bitbucket and moved the code to GitLab. Thus, all of the
issue were lost, and new issues started again with #1. I'm naming the
GitLab issues as #GH-xx, and the old Bitbucket issues as #BB-xx. I'm
still angry at Bitbucket for forcing this change.