sdata 1.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. sdata/__init__.py +140 -0
  2. sdata/base.py +684 -0
  3. sdata/contrib/__init__.py +0 -0
  4. sdata/contrib/piexif/__init__.py +12 -0
  5. sdata/contrib/piexif/_common.py +94 -0
  6. sdata/contrib/piexif/_dump.py +346 -0
  7. sdata/contrib/piexif/_exceptions.py +2 -0
  8. sdata/contrib/piexif/_exif.py +638 -0
  9. sdata/contrib/piexif/_insert.py +60 -0
  10. sdata/contrib/piexif/_load.py +270 -0
  11. sdata/contrib/piexif/_remove.py +56 -0
  12. sdata/contrib/piexif/_transplant.py +45 -0
  13. sdata/contrib/piexif/_webp.py +240 -0
  14. sdata/contrib/piexif/helper.py +66 -0
  15. sdata/contrib/ranger/__init__.py +8 -0
  16. sdata/contrib/ranger/collections/__init__.py +0 -0
  17. sdata/contrib/ranger/collections/rangebucketmap.py +346 -0
  18. sdata/contrib/ranger/collections/rangemap.py +396 -0
  19. sdata/contrib/ranger/collections/rangeset.py +483 -0
  20. sdata/contrib/ranger/range/__init__.py +0 -0
  21. sdata/contrib/ranger/range/cut.py +285 -0
  22. sdata/contrib/ranger/range/range.py +644 -0
  23. sdata/contrib/sobol_seq.py +394 -0
  24. sdata/contrib/sortedcontainers/LICENSE +13 -0
  25. sdata/contrib/sortedcontainers/__init__.py +74 -0
  26. sdata/contrib/sortedcontainers/sorteddict.py +789 -0
  27. sdata/contrib/sortedcontainers/sortedlist.py +2646 -0
  28. sdata/contrib/sortedcontainers/sortedset.py +733 -0
  29. sdata/contrib/sqlitedict.py +598 -0
  30. sdata/contrib/timeflake/__init__.py +40 -0
  31. sdata/contrib/timeflake/flake.py +69 -0
  32. sdata/contrib/timeflake/utils.py +47 -0
  33. sdata/deprecated/__init__.py +10 -0
  34. sdata/deprecated/data.py +1503 -0
  35. sdata/did/__init__.py +129 -0
  36. sdata/did/_http.py +67 -0
  37. sdata/did/base58btc.py +49 -0
  38. sdata/did/did_github.py +152 -0
  39. sdata/did/did_peer4.py +155 -0
  40. sdata/did/did_web.py +222 -0
  41. sdata/did/diddoc.py +75 -0
  42. sdata/did/ed25519.py +225 -0
  43. sdata/did/eddsa.py +107 -0
  44. sdata/did/errors.py +40 -0
  45. sdata/did/issue_vc.py +193 -0
  46. sdata/did/jose.py +126 -0
  47. sdata/did/keys.py +104 -0
  48. sdata/did/utils_didkey.py +161 -0
  49. sdata/did/verify_vp.py +158 -0
  50. sdata/doe.py +101 -0
  51. sdata/dtypes.py +510 -0
  52. sdata/experiments/__init__.py +331 -0
  53. sdata/experiments/blob0.py +131 -0
  54. sdata/experiments/file.py +42 -0
  55. sdata/experiments/filegroup.py +144 -0
  56. sdata/experiments/image_old.py +313 -0
  57. sdata/experiments/ks2.py +15 -0
  58. sdata/experiments/lapshear.py +8 -0
  59. sdata/experiments/material.py +7 -0
  60. sdata/experiments/parameters_deprecated.py +394 -0
  61. sdata/experiments/workbook.py +226 -0
  62. sdata/format.py +125 -0
  63. sdata/imagemeta.py +533 -0
  64. sdata/interactive.py +118 -0
  65. sdata/iolib/__init__.py +51 -0
  66. sdata/iolib/hashtree.py +264 -0
  67. sdata/iolib/json1sqlitestore.py +554 -0
  68. sdata/iolib/jsonsqlitestore.py +704 -0
  69. sdata/iolib/pud.py +242 -0
  70. sdata/iolib/reader.py +262 -0
  71. sdata/iolib/writer.py +419 -0
  72. sdata/metadata.py +882 -0
  73. sdata/parameter.py +414 -0
  74. sdata/schema.py +273 -0
  75. sdata/sclass/__init__.py +111 -0
  76. sdata/sclass/bfo_classes.py +362 -0
  77. sdata/sclass/blob.py +362 -0
  78. sdata/sclass/content.py +87 -0
  79. sdata/sclass/dataframe.py +1251 -0
  80. sdata/sclass/dataframegroup.py +187 -0
  81. sdata/sclass/dependency_graph.py +85 -0
  82. sdata/sclass/filereference.py +124 -0
  83. sdata/sclass/image.py +218 -0
  84. sdata/sclass/lazy_namespace.py +155 -0
  85. sdata/sclass/process.py +383 -0
  86. sdata/sclass/process_graph.py +221 -0
  87. sdata/sclass/zipfile.py +135 -0
  88. sdata/semantic.py +346 -0
  89. sdata/suuid.py +136 -0
  90. sdata/timestamp.py +336 -0
  91. sdata/tools.py +14 -0
  92. sdata/units.py +602 -0
  93. sdata/vocab.py +149 -0
  94. sdata-1.5.0.dist-info/METADATA +264 -0
  95. sdata-1.5.0.dist-info/RECORD +98 -0
  96. sdata-1.5.0.dist-info/WHEEL +5 -0
  97. sdata-1.5.0.dist-info/licenses/LICENSE-MIT +21 -0
  98. sdata-1.5.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,789 @@
1
+ """Sorted Dict
2
+ ==============
3
+
4
+ :doc:`Sorted Containers<index>` is an Apache2 licensed Python sorted
5
+ collections library, written in pure-Python, and fast as C-extensions. The
6
+ :doc:`introduction<introduction>` is the best way to get started.
7
+
8
+ Sorted dict implementations:
9
+
10
+ .. currentmodule:: sortedcontainers
11
+
12
+ * :class:`SortedDict`
13
+ * :class:`SortedKeysView`
14
+ * :class:`SortedItemsView`
15
+ * :class:`SortedValuesView`
16
+
17
+ """
18
+
19
+ import sys
20
+ import warnings
21
+
22
+ from .sortedlist import SortedList, recursive_repr
23
+ from .sortedset import SortedSet
24
+
25
+ ###############################################################################
26
+ # BEGIN Python 2/3 Shims
27
+ ###############################################################################
28
+
29
+ try:
30
+ from collections.abc import ItemsView, KeysView, ValuesView, Sequence
31
+ except ImportError:
32
+ from collections import ItemsView, KeysView, ValuesView, Sequence
33
+
34
+ ###############################################################################
35
+ # END Python 2/3 Shims
36
+ ###############################################################################
37
+
38
+
39
+ class SortedDict(dict):
40
+ """Sorted dict is a sorted mutable mapping.
41
+
42
+ Sorted dict keys are maintained in sorted order. The design of sorted dict
43
+ is simple: sorted dict inherits from dict to store items and maintains a
44
+ sorted list of keys.
45
+
46
+ Sorted dict keys must be hashable and comparable. The hash and total
47
+ ordering of keys must not change while they are stored in the sorted dict.
48
+
49
+ Mutable mapping methods:
50
+
51
+ * :func:`SortedDict.__getitem__` (inherited from dict)
52
+ * :func:`SortedDict.__setitem__`
53
+ * :func:`SortedDict.__delitem__`
54
+ * :func:`SortedDict.__iter__`
55
+ * :func:`SortedDict.__len__` (inherited from dict)
56
+
57
+ Methods for adding items:
58
+
59
+ * :func:`SortedDict.setdefault`
60
+ * :func:`SortedDict.update`
61
+
62
+ Methods for removing items:
63
+
64
+ * :func:`SortedDict.clear`
65
+ * :func:`SortedDict.pop`
66
+ * :func:`SortedDict.popitem`
67
+
68
+ Methods for looking up items:
69
+
70
+ * :func:`SortedDict.__contains__` (inherited from dict)
71
+ * :func:`SortedDict.get` (inherited from dict)
72
+ * :func:`SortedDict.peekitem`
73
+
74
+ Methods for views:
75
+
76
+ * :func:`SortedDict.keys`
77
+ * :func:`SortedDict.items`
78
+ * :func:`SortedDict.values`
79
+
80
+ Methods for miscellany:
81
+
82
+ * :func:`SortedDict.copy`
83
+ * :func:`SortedDict.fromkeys`
84
+ * :func:`SortedDict.__reversed__`
85
+ * :func:`SortedDict.__eq__` (inherited from dict)
86
+ * :func:`SortedDict.__ne__` (inherited from dict)
87
+ * :func:`SortedDict.__repr__`
88
+ * :func:`SortedDict._check`
89
+
90
+ Sorted list methods available (applies to keys):
91
+
92
+ * :func:`SortedList.bisect_left`
93
+ * :func:`SortedList.bisect_right`
94
+ * :func:`SortedList.count`
95
+ * :func:`SortedList.index`
96
+ * :func:`SortedList.irange`
97
+ * :func:`SortedList.islice`
98
+ * :func:`SortedList._reset`
99
+
100
+ Additional sorted list methods available, if key-function used:
101
+
102
+ * :func:`SortedKeyList.bisect_key_left`
103
+ * :func:`SortedKeyList.bisect_key_right`
104
+ * :func:`SortedKeyList.irange_key`
105
+
106
+ Sorted dicts may only be compared for equality and inequality.
107
+
108
+ """
109
+ def __init__(self, *args, **kwargs):
110
+ """Initialize sorted dict instance.
111
+
112
+ Optional key-function argument defines a callable that, like the `key`
113
+ argument to the built-in `sorted` function, extracts a comparison key
114
+ from each dictionary key. If no function is specified, the default
115
+ compares the dictionary keys directly. The key-function argument must
116
+ be provided as a positional argument and must come before all other
117
+ arguments.
118
+
119
+ Optional iterable argument provides an initial sequence of pairs to
120
+ initialize the sorted dict. Each pair in the sequence defines the key
121
+ and corresponding value. If a key is seen more than once, the last
122
+ value associated with it is stored in the new sorted dict.
123
+
124
+ Optional mapping argument provides an initial mapping of items to
125
+ initialize the sorted dict.
126
+
127
+ If keyword arguments are given, the keywords themselves, with their
128
+ associated values, are added as items to the dictionary. If a key is
129
+ specified both in the positional argument and as a keyword argument,
130
+ the value associated with the keyword is stored in the
131
+ sorted dict.
132
+
133
+ Sorted dict keys must be hashable, per the requirement for Python's
134
+ dictionaries. Keys (or the result of the key-function) must also be
135
+ comparable, per the requirement for sorted lists.
136
+
137
+ >>> d = {'alpha': 1, 'beta': 2}
138
+ >>> SortedDict([('alpha', 1), ('beta', 2)]) == d
139
+ True
140
+ >>> SortedDict({'alpha': 1, 'beta': 2}) == d
141
+ True
142
+ >>> SortedDict(alpha=1, beta=2) == d
143
+ True
144
+
145
+ """
146
+ if args and (args[0] is None or callable(args[0])):
147
+ _key = self._key = args[0]
148
+ args = args[1:]
149
+ else:
150
+ _key = self._key = None
151
+
152
+ self._list = SortedList(key=_key)
153
+
154
+ # Reaching through ``self._list`` repeatedly adds unnecessary overhead
155
+ # so cache references to sorted list methods.
156
+
157
+ _list = self._list
158
+ self._list_add = _list.add
159
+ self._list_clear = _list.clear
160
+ self._list_iter = _list.__iter__
161
+ self._list_reversed = _list.__reversed__
162
+ self._list_pop = _list.pop
163
+ self._list_remove = _list.remove
164
+ self._list_update = _list.update
165
+
166
+ # Expose some sorted list methods publicly.
167
+
168
+ self.bisect_left = _list.bisect_left
169
+ self.bisect = _list.bisect_right
170
+ self.bisect_right = _list.bisect_right
171
+ self.index = _list.index
172
+ self.irange = _list.irange
173
+ self.islice = _list.islice
174
+ self._reset = _list._reset
175
+
176
+ if _key is not None:
177
+ self.bisect_key_left = _list.bisect_key_left
178
+ self.bisect_key_right = _list.bisect_key_right
179
+ self.bisect_key = _list.bisect_key
180
+ self.irange_key = _list.irange_key
181
+
182
+ self._update(*args, **kwargs)
183
+
184
+
185
+ @property
186
+ def key(self):
187
+ """Function used to extract comparison key from keys.
188
+
189
+ Sorted dict compares keys directly when the key function is none.
190
+
191
+ """
192
+ return self._key
193
+
194
+
195
+ @property
196
+ def iloc(self):
197
+ """Cached reference of sorted keys view.
198
+
199
+ Deprecated in version 2 of Sorted Containers. Use
200
+ :func:`SortedDict.keys` instead.
201
+
202
+ """
203
+ # pylint: disable=attribute-defined-outside-init
204
+ try:
205
+ return self._iloc
206
+ except AttributeError:
207
+ warnings.warn(
208
+ 'sorted_dict.iloc is deprecated.'
209
+ ' Use SortedDict.keys() instead.',
210
+ DeprecationWarning,
211
+ stacklevel=2,
212
+ )
213
+ _iloc = self._iloc = SortedKeysView(self)
214
+ return _iloc
215
+
216
+
217
+ def clear(self):
218
+
219
+ """Remove all items from sorted dict.
220
+
221
+ Runtime complexity: `O(n)`
222
+
223
+ """
224
+ dict.clear(self)
225
+ self._list_clear()
226
+
227
+
228
+ def __delitem__(self, key):
229
+ """Remove item from sorted dict identified by `key`.
230
+
231
+ ``sd.__delitem__(key)`` <==> ``del sd[key]``
232
+
233
+ Runtime complexity: `O(log(n))` -- approximate.
234
+
235
+ >>> sd = SortedDict({'a': 1, 'b': 2, 'c': 3})
236
+ >>> del sd['b']
237
+ >>> sd
238
+ SortedDict({'a': 1, 'c': 3})
239
+ >>> del sd['z']
240
+ Traceback (most recent call last):
241
+ ...
242
+ KeyError: 'z'
243
+
244
+ :param key: `key` for item lookup
245
+ :raises KeyError: if key not found
246
+
247
+ """
248
+ dict.__delitem__(self, key)
249
+ self._list_remove(key)
250
+
251
+
252
+ def __iter__(self):
253
+ """Return an iterator over the keys of the sorted dict.
254
+
255
+ ``sd.__iter__()`` <==> ``iter(sd)``
256
+
257
+ Iterating the sorted dict while adding or deleting items may raise a
258
+ :exc:`RuntimeError` or fail to iterate over all keys.
259
+
260
+ """
261
+ return self._list_iter()
262
+
263
+
264
+ def __reversed__(self):
265
+ """Return a reverse iterator over the keys of the sorted dict.
266
+
267
+ ``sd.__reversed__()`` <==> ``reversed(sd)``
268
+
269
+ Iterating the sorted dict while adding or deleting items may raise a
270
+ :exc:`RuntimeError` or fail to iterate over all keys.
271
+
272
+ """
273
+ return self._list_reversed()
274
+
275
+
276
+ def __setitem__(self, key, value):
277
+ """Store item in sorted dict with `key` and corresponding `value`.
278
+
279
+ ``sd.__setitem__(key, value)`` <==> ``sd[key] = value``
280
+
281
+ Runtime complexity: `O(log(n))` -- approximate.
282
+
283
+ >>> sd = SortedDict()
284
+ >>> sd['c'] = 3
285
+ >>> sd['a'] = 1
286
+ >>> sd['b'] = 2
287
+ >>> sd
288
+ SortedDict({'a': 1, 'b': 2, 'c': 3})
289
+
290
+ :param key: key for item
291
+ :param value: value for item
292
+
293
+ """
294
+ if key not in self:
295
+ self._list_add(key)
296
+ dict.__setitem__(self, key, value)
297
+
298
+ _setitem = __setitem__
299
+
300
+
301
+ def copy(self):
302
+ """Return a shallow copy of the sorted dict.
303
+
304
+ Runtime complexity: `O(n)`
305
+
306
+ :return: new sorted dict
307
+
308
+ """
309
+ return self.__class__(self._key, self.items())
310
+
311
+ __copy__ = copy
312
+
313
+
314
+ @classmethod
315
+ def fromkeys(cls, iterable, value=None):
316
+ """Return a new sorted dict initailized from `iterable` and `value`.
317
+
318
+ Items in the sorted dict have keys from `iterable` and values equal to
319
+ `value`.
320
+
321
+ Runtime complexity: `O(n*log(n))`
322
+
323
+ :return: new sorted dict
324
+
325
+ """
326
+ return cls((key, value) for key in iterable)
327
+
328
+
329
+ def keys(self):
330
+ """Return new sorted keys view of the sorted dict's keys.
331
+
332
+ See :class:`SortedKeysView` for details.
333
+
334
+ :return: new sorted keys view
335
+
336
+ """
337
+ return SortedKeysView(self)
338
+
339
+
340
+ def items(self):
341
+ """Return new sorted items view of the sorted dict's items.
342
+
343
+ See :class:`SortedItemsView` for details.
344
+
345
+ :return: new sorted items view
346
+
347
+ """
348
+ return SortedItemsView(self)
349
+
350
+
351
+ def values(self):
352
+ """Return new sorted values view of the sorted dict's values.
353
+
354
+ See :class:`SortedValuesView` for details.
355
+
356
+ :return: new sorted values view
357
+
358
+ """
359
+ return SortedValuesView(self)
360
+
361
+
362
+ if sys.hexversion < 0x03000000:
363
+ def __make_raise_attributeerror(original, alternate):
364
+ # pylint: disable=no-self-argument
365
+ message = (
366
+ 'SortedDict.{original}() is not implemented.'
367
+ ' Use SortedDict.{alternate}() instead.'
368
+ ).format(original=original, alternate=alternate)
369
+ def method(self):
370
+ # pylint: disable=missing-docstring,unused-argument
371
+ raise AttributeError(message)
372
+ method.__name__ = original # pylint: disable=non-str-assignment-to-dunder-name
373
+ method.__doc__ = message
374
+ return property(method)
375
+
376
+ iteritems = __make_raise_attributeerror('iteritems', 'items')
377
+ iterkeys = __make_raise_attributeerror('iterkeys', 'keys')
378
+ itervalues = __make_raise_attributeerror('itervalues', 'values')
379
+ viewitems = __make_raise_attributeerror('viewitems', 'items')
380
+ viewkeys = __make_raise_attributeerror('viewkeys', 'keys')
381
+ viewvalues = __make_raise_attributeerror('viewvalues', 'values')
382
+
383
+
384
+ class _NotGiven(object):
385
+ # pylint: disable=too-few-public-methods
386
+ def __repr__(self):
387
+ return '<not-given>'
388
+
389
+ __not_given = _NotGiven()
390
+
391
+ def pop(self, key, default=__not_given):
392
+ """Remove and return value for item identified by `key`.
393
+
394
+ If the `key` is not found then return `default` if given. If `default`
395
+ is not given then raise :exc:`KeyError`.
396
+
397
+ Runtime complexity: `O(log(n))` -- approximate.
398
+
399
+ >>> sd = SortedDict({'a': 1, 'b': 2, 'c': 3})
400
+ >>> sd.pop('c')
401
+ 3
402
+ >>> sd.pop('z', 26)
403
+ 26
404
+ >>> sd.pop('y')
405
+ Traceback (most recent call last):
406
+ ...
407
+ KeyError: 'y'
408
+
409
+ :param key: `key` for item
410
+ :param default: `default` value if key not found (optional)
411
+ :return: value for item
412
+ :raises KeyError: if `key` not found and `default` not given
413
+
414
+ """
415
+ if key in self:
416
+ self._list_remove(key)
417
+ return dict.pop(self, key)
418
+ else:
419
+ if default is self.__not_given:
420
+ raise KeyError(key)
421
+ return default
422
+
423
+
424
+ def popitem(self, index=-1):
425
+ """Remove and return ``(key, value)`` pair at `index` from sorted dict.
426
+
427
+ Optional argument `index` defaults to -1, the last item in the sorted
428
+ dict. Specify ``index=0`` for the first item in the sorted dict.
429
+
430
+ If the sorted dict is empty, raises :exc:`KeyError`.
431
+
432
+ If the `index` is out of range, raises :exc:`IndexError`.
433
+
434
+ Runtime complexity: `O(log(n))`
435
+
436
+ >>> sd = SortedDict({'a': 1, 'b': 2, 'c': 3})
437
+ >>> sd.popitem()
438
+ ('c', 3)
439
+ >>> sd.popitem(0)
440
+ ('a', 1)
441
+ >>> sd.popitem(100)
442
+ Traceback (most recent call last):
443
+ ...
444
+ IndexError: list index out of range
445
+
446
+ :param int index: `index` of item (default -1)
447
+ :return: key and value pair
448
+ :raises KeyError: if sorted dict is empty
449
+ :raises IndexError: if `index` out of range
450
+
451
+ """
452
+ if not self:
453
+ raise KeyError('popitem(): dictionary is empty')
454
+
455
+ key = self._list_pop(index)
456
+ value = dict.pop(self, key)
457
+ return (key, value)
458
+
459
+
460
+ def peekitem(self, index=-1):
461
+ """Return ``(key, value)`` pair at `index` in sorted dict.
462
+
463
+ Optional argument `index` defaults to -1, the last item in the sorted
464
+ dict. Specify ``index=0`` for the first item in the sorted dict.
465
+
466
+ Unlike :func:`SortedDict.popitem`, the sorted dict is not modified.
467
+
468
+ If the `index` is out of range, raises :exc:`IndexError`.
469
+
470
+ Runtime complexity: `O(log(n))`
471
+
472
+ >>> sd = SortedDict({'a': 1, 'b': 2, 'c': 3})
473
+ >>> sd.peekitem()
474
+ ('c', 3)
475
+ >>> sd.peekitem(0)
476
+ ('a', 1)
477
+ >>> sd.peekitem(100)
478
+ Traceback (most recent call last):
479
+ ...
480
+ IndexError: list index out of range
481
+
482
+ :param int index: index of item (default -1)
483
+ :return: key and value pair
484
+ :raises IndexError: if `index` out of range
485
+
486
+ """
487
+ key = self._list[index]
488
+ return key, self[key]
489
+
490
+
491
+ def setdefault(self, key, default=None):
492
+ """Return value for item identified by `key` in sorted dict.
493
+
494
+ If `key` is in the sorted dict then return its value. If `key` is not
495
+ in the sorted dict then insert `key` with value `default` and return
496
+ `default`.
497
+
498
+ Optional argument `default` defaults to none.
499
+
500
+ Runtime complexity: `O(log(n))` -- approximate.
501
+
502
+ >>> sd = SortedDict()
503
+ >>> sd.setdefault('a', 1)
504
+ 1
505
+ >>> sd.setdefault('a', 10)
506
+ 1
507
+ >>> sd
508
+ SortedDict({'a': 1})
509
+
510
+ :param key: key for item
511
+ :param default: value for item (default None)
512
+ :return: value for item identified by `key`
513
+
514
+ """
515
+ if key in self:
516
+ return self[key]
517
+ dict.__setitem__(self, key, default)
518
+ self._list_add(key)
519
+ return default
520
+
521
+
522
+ def update(self, *args, **kwargs):
523
+ """Update sorted dict with items from `args` and `kwargs`.
524
+
525
+ Overwrites existing items.
526
+
527
+ Optional arguments `args` and `kwargs` may be a mapping, an iterable of
528
+ pairs or keyword arguments. See :func:`SortedDict.__init__` for
529
+ details.
530
+
531
+ :param args: mapping or iterable of pairs
532
+ :param kwargs: keyword arguments mapping
533
+
534
+ """
535
+ if not self:
536
+ dict.update(self, *args, **kwargs)
537
+ self._list_update(dict.__iter__(self))
538
+ return
539
+
540
+ if not kwargs and len(args) == 1 and isinstance(args[0], dict):
541
+ pairs = args[0]
542
+ else:
543
+ pairs = dict(*args, **kwargs)
544
+
545
+ if (10 * len(pairs)) > len(self):
546
+ dict.update(self, pairs)
547
+ self._list_clear()
548
+ self._list_update(dict.__iter__(self))
549
+ else:
550
+ for key in pairs:
551
+ self._setitem(key, pairs[key])
552
+
553
+ _update = update
554
+
555
+
556
+ def __reduce__(self):
557
+ """Support for pickle.
558
+
559
+ The tricks played with caching references in
560
+ :func:`SortedDict.__init__` confuse pickle so customize the reducer.
561
+
562
+ """
563
+ items = dict.copy(self)
564
+ return (type(self), (self._key, items))
565
+
566
+
567
+ @recursive_repr()
568
+ def __repr__(self):
569
+ """Return string representation of sorted dict.
570
+
571
+ ``sd.__repr__()`` <==> ``repr(sd)``
572
+
573
+ :return: string representation
574
+
575
+ """
576
+ _key = self._key
577
+ type_name = type(self).__name__
578
+ key_arg = '' if _key is None else '{0!r}, '.format(_key)
579
+ item_format = '{0!r}: {1!r}'.format
580
+ items = ', '.join(item_format(key, self[key]) for key in self._list)
581
+ return '{0}({1}{{{2}}})'.format(type_name, key_arg, items)
582
+
583
+
584
+ def _check(self):
585
+ """Check invariants of sorted dict.
586
+
587
+ Runtime complexity: `O(n)`
588
+
589
+ """
590
+ _list = self._list
591
+ _list._check()
592
+ assert len(self) == len(_list)
593
+ assert all(key in self for key in _list)
594
+
595
+
596
+ def _view_delitem(self, index):
597
+ """Remove item at `index` from sorted dict.
598
+
599
+ ``view.__delitem__(index)`` <==> ``del view[index]``
600
+
601
+ Supports slicing.
602
+
603
+ Runtime complexity: `O(log(n))` -- approximate.
604
+
605
+ >>> sd = SortedDict({'a': 1, 'b': 2, 'c': 3})
606
+ >>> view = sd.keys()
607
+ >>> del view[0]
608
+ >>> sd
609
+ SortedDict({'b': 2, 'c': 3})
610
+ >>> del view[-1]
611
+ >>> sd
612
+ SortedDict({'b': 2})
613
+ >>> del view[:]
614
+ >>> sd
615
+ SortedDict({})
616
+
617
+ :param index: integer or slice for indexing
618
+ :raises IndexError: if index out of range
619
+
620
+ """
621
+ _mapping = self._mapping
622
+ _list = _mapping._list
623
+ dict_delitem = dict.__delitem__
624
+ if isinstance(index, slice):
625
+ keys = _list[index]
626
+ del _list[index]
627
+ for key in keys:
628
+ dict_delitem(_mapping, key)
629
+ else:
630
+ key = _list.pop(index)
631
+ dict_delitem(_mapping, key)
632
+
633
+
634
+ class SortedKeysView(KeysView, Sequence):
635
+ """Sorted keys view is a dynamic view of the sorted dict's keys.
636
+
637
+ When the sorted dict's keys change, the view reflects those changes.
638
+
639
+ The keys view implements the set and sequence abstract base classes.
640
+
641
+ """
642
+ __slots__ = ()
643
+
644
+
645
+ @classmethod
646
+ def _from_iterable(cls, it):
647
+ return SortedSet(it)
648
+
649
+
650
+ def __getitem__(self, index):
651
+ """Lookup key at `index` in sorted keys views.
652
+
653
+ ``skv.__getitem__(index)`` <==> ``skv[index]``
654
+
655
+ Supports slicing.
656
+
657
+ Runtime complexity: `O(log(n))` -- approximate.
658
+
659
+ >>> sd = SortedDict({'a': 1, 'b': 2, 'c': 3})
660
+ >>> skv = sd.keys()
661
+ >>> skv[0]
662
+ 'a'
663
+ >>> skv[-1]
664
+ 'c'
665
+ >>> skv[:]
666
+ ['a', 'b', 'c']
667
+ >>> skv[100]
668
+ Traceback (most recent call last):
669
+ ...
670
+ IndexError: list index out of range
671
+
672
+ :param index: integer or slice for indexing
673
+ :return: key or list of keys
674
+ :raises IndexError: if index out of range
675
+
676
+ """
677
+ return self._mapping._list[index]
678
+
679
+
680
+ __delitem__ = _view_delitem
681
+
682
+
683
+ class SortedItemsView(ItemsView, Sequence):
684
+ """Sorted items view is a dynamic view of the sorted dict's items.
685
+
686
+ When the sorted dict's items change, the view reflects those changes.
687
+
688
+ The items view implements the set and sequence abstract base classes.
689
+
690
+ """
691
+ __slots__ = ()
692
+
693
+
694
+ @classmethod
695
+ def _from_iterable(cls, it):
696
+ return SortedSet(it)
697
+
698
+
699
+ def __getitem__(self, index):
700
+ """Lookup item at `index` in sorted items view.
701
+
702
+ ``siv.__getitem__(index)`` <==> ``siv[index]``
703
+
704
+ Supports slicing.
705
+
706
+ Runtime complexity: `O(log(n))` -- approximate.
707
+
708
+ >>> sd = SortedDict({'a': 1, 'b': 2, 'c': 3})
709
+ >>> siv = sd.items()
710
+ >>> siv[0]
711
+ ('a', 1)
712
+ >>> siv[-1]
713
+ ('c', 3)
714
+ >>> siv[:]
715
+ [('a', 1), ('b', 2), ('c', 3)]
716
+ >>> siv[100]
717
+ Traceback (most recent call last):
718
+ ...
719
+ IndexError: list index out of range
720
+
721
+ :param index: integer or slice for indexing
722
+ :return: item or list of items
723
+ :raises IndexError: if index out of range
724
+
725
+ """
726
+ _mapping = self._mapping
727
+ _mapping_list = _mapping._list
728
+
729
+ if isinstance(index, slice):
730
+ keys = _mapping_list[index]
731
+ return [(key, _mapping[key]) for key in keys]
732
+
733
+ key = _mapping_list[index]
734
+ return key, _mapping[key]
735
+
736
+
737
+ __delitem__ = _view_delitem
738
+
739
+
740
+ class SortedValuesView(ValuesView, Sequence):
741
+ """Sorted values view is a dynamic view of the sorted dict's values.
742
+
743
+ When the sorted dict's values change, the view reflects those changes.
744
+
745
+ The values view implements the sequence abstract base class.
746
+
747
+ """
748
+ __slots__ = ()
749
+
750
+
751
+ def __getitem__(self, index):
752
+ """Lookup value at `index` in sorted values view.
753
+
754
+ ``siv.__getitem__(index)`` <==> ``siv[index]``
755
+
756
+ Supports slicing.
757
+
758
+ Runtime complexity: `O(log(n))` -- approximate.
759
+
760
+ >>> sd = SortedDict({'a': 1, 'b': 2, 'c': 3})
761
+ >>> svv = sd.values()
762
+ >>> svv[0]
763
+ 1
764
+ >>> svv[-1]
765
+ 3
766
+ >>> svv[:]
767
+ [1, 2, 3]
768
+ >>> svv[100]
769
+ Traceback (most recent call last):
770
+ ...
771
+ IndexError: list index out of range
772
+
773
+ :param index: integer or slice for indexing
774
+ :return: value or list of values
775
+ :raises IndexError: if index out of range
776
+
777
+ """
778
+ _mapping = self._mapping
779
+ _mapping_list = _mapping._list
780
+
781
+ if isinstance(index, slice):
782
+ keys = _mapping_list[index]
783
+ return [_mapping[key] for key in keys]
784
+
785
+ key = _mapping_list[index]
786
+ return _mapping[key]
787
+
788
+
789
+ __delitem__ = _view_delitem