Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

functools.py 32 KB

You have to be logged in to leave a comment. Sign In
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
  1. """functools.py - Tools for working with functions and callable objects
  2. """
  3. # Python module wrapper for _functools C module
  4. # to allow utilities written in Python to be added
  5. # to the functools module.
  6. # Written by Nick Coghlan <ncoghlan at gmail.com>,
  7. # Raymond Hettinger <python at rcn.com>,
  8. # and Łukasz Langa <lukasz at langa.pl>.
  9. # Copyright (C) 2006-2013 Python Software Foundation.
  10. # See C source code for _functools credits/copyright
  11. __all__ = ['update_wrapper', 'wraps', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES',
  12. 'total_ordering', 'cmp_to_key', 'lru_cache', 'reduce', 'partial',
  13. 'partialmethod', 'singledispatch']
  14. try:
  15. from _functools import reduce
  16. except ImportError:
  17. pass
  18. from abc import get_cache_token
  19. from collections import namedtuple
  20. # import types, weakref # Deferred to single_dispatch()
  21. from reprlib import recursive_repr
  22. from _thread import RLock
  23. ################################################################################
  24. ### update_wrapper() and wraps() decorator
  25. ################################################################################
  26. # update_wrapper() and wraps() are tools to help write
  27. # wrapper functions that can handle naive introspection
  28. WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__qualname__', '__doc__',
  29. '__annotations__')
  30. WRAPPER_UPDATES = ('__dict__',)
  31. def update_wrapper(wrapper,
  32. wrapped,
  33. assigned = WRAPPER_ASSIGNMENTS,
  34. updated = WRAPPER_UPDATES):
  35. """Update a wrapper function to look like the wrapped function
  36. wrapper is the function to be updated
  37. wrapped is the original function
  38. assigned is a tuple naming the attributes assigned directly
  39. from the wrapped function to the wrapper function (defaults to
  40. functools.WRAPPER_ASSIGNMENTS)
  41. updated is a tuple naming the attributes of the wrapper that
  42. are updated with the corresponding attribute from the wrapped
  43. function (defaults to functools.WRAPPER_UPDATES)
  44. """
  45. for attr in assigned:
  46. try:
  47. value = getattr(wrapped, attr)
  48. except AttributeError:
  49. pass
  50. else:
  51. setattr(wrapper, attr, value)
  52. for attr in updated:
  53. getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
  54. # Issue #17482: set __wrapped__ last so we don't inadvertently copy it
  55. # from the wrapped function when updating __dict__
  56. wrapper.__wrapped__ = wrapped
  57. # Return the wrapper so this can be used as a decorator via partial()
  58. return wrapper
  59. def wraps(wrapped,
  60. assigned = WRAPPER_ASSIGNMENTS,
  61. updated = WRAPPER_UPDATES):
  62. """Decorator factory to apply update_wrapper() to a wrapper function
  63. Returns a decorator that invokes update_wrapper() with the decorated
  64. function as the wrapper argument and the arguments to wraps() as the
  65. remaining arguments. Default arguments are as for update_wrapper().
  66. This is a convenience function to simplify applying partial() to
  67. update_wrapper().
  68. """
  69. return partial(update_wrapper, wrapped=wrapped,
  70. assigned=assigned, updated=updated)
  71. ################################################################################
  72. ### total_ordering class decorator
  73. ################################################################################
  74. # The total ordering functions all invoke the root magic method directly
  75. # rather than using the corresponding operator. This avoids possible
  76. # infinite recursion that could occur when the operator dispatch logic
  77. # detects a NotImplemented result and then calls a reflected method.
  78. def _gt_from_lt(self, other, NotImplemented=NotImplemented):
  79. 'Return a > b. Computed by @total_ordering from (not a < b) and (a != b).'
  80. op_result = self.__lt__(other)
  81. if op_result is NotImplemented:
  82. return op_result
  83. return not op_result and self != other
  84. def _le_from_lt(self, other, NotImplemented=NotImplemented):
  85. 'Return a <= b. Computed by @total_ordering from (a < b) or (a == b).'
  86. op_result = self.__lt__(other)
  87. return op_result or self == other
  88. def _ge_from_lt(self, other, NotImplemented=NotImplemented):
  89. 'Return a >= b. Computed by @total_ordering from (not a < b).'
  90. op_result = self.__lt__(other)
  91. if op_result is NotImplemented:
  92. return op_result
  93. return not op_result
  94. def _ge_from_le(self, other, NotImplemented=NotImplemented):
  95. 'Return a >= b. Computed by @total_ordering from (not a <= b) or (a == b).'
  96. op_result = self.__le__(other)
  97. if op_result is NotImplemented:
  98. return op_result
  99. return not op_result or self == other
  100. def _lt_from_le(self, other, NotImplemented=NotImplemented):
  101. 'Return a < b. Computed by @total_ordering from (a <= b) and (a != b).'
  102. op_result = self.__le__(other)
  103. if op_result is NotImplemented:
  104. return op_result
  105. return op_result and self != other
  106. def _gt_from_le(self, other, NotImplemented=NotImplemented):
  107. 'Return a > b. Computed by @total_ordering from (not a <= b).'
  108. op_result = self.__le__(other)
  109. if op_result is NotImplemented:
  110. return op_result
  111. return not op_result
  112. def _lt_from_gt(self, other, NotImplemented=NotImplemented):
  113. 'Return a < b. Computed by @total_ordering from (not a > b) and (a != b).'
  114. op_result = self.__gt__(other)
  115. if op_result is NotImplemented:
  116. return op_result
  117. return not op_result and self != other
  118. def _ge_from_gt(self, other, NotImplemented=NotImplemented):
  119. 'Return a >= b. Computed by @total_ordering from (a > b) or (a == b).'
  120. op_result = self.__gt__(other)
  121. return op_result or self == other
  122. def _le_from_gt(self, other, NotImplemented=NotImplemented):
  123. 'Return a <= b. Computed by @total_ordering from (not a > b).'
  124. op_result = self.__gt__(other)
  125. if op_result is NotImplemented:
  126. return op_result
  127. return not op_result
  128. def _le_from_ge(self, other, NotImplemented=NotImplemented):
  129. 'Return a <= b. Computed by @total_ordering from (not a >= b) or (a == b).'
  130. op_result = self.__ge__(other)
  131. if op_result is NotImplemented:
  132. return op_result
  133. return not op_result or self == other
  134. def _gt_from_ge(self, other, NotImplemented=NotImplemented):
  135. 'Return a > b. Computed by @total_ordering from (a >= b) and (a != b).'
  136. op_result = self.__ge__(other)
  137. if op_result is NotImplemented:
  138. return op_result
  139. return op_result and self != other
  140. def _lt_from_ge(self, other, NotImplemented=NotImplemented):
  141. 'Return a < b. Computed by @total_ordering from (not a >= b).'
  142. op_result = self.__ge__(other)
  143. if op_result is NotImplemented:
  144. return op_result
  145. return not op_result
  146. _convert = {
  147. '__lt__': [('__gt__', _gt_from_lt),
  148. ('__le__', _le_from_lt),
  149. ('__ge__', _ge_from_lt)],
  150. '__le__': [('__ge__', _ge_from_le),
  151. ('__lt__', _lt_from_le),
  152. ('__gt__', _gt_from_le)],
  153. '__gt__': [('__lt__', _lt_from_gt),
  154. ('__ge__', _ge_from_gt),
  155. ('__le__', _le_from_gt)],
  156. '__ge__': [('__le__', _le_from_ge),
  157. ('__gt__', _gt_from_ge),
  158. ('__lt__', _lt_from_ge)]
  159. }
  160. def total_ordering(cls):
  161. """Class decorator that fills in missing ordering methods"""
  162. # Find user-defined comparisons (not those inherited from object).
  163. roots = {op for op in _convert if getattr(cls, op, None) is not getattr(object, op, None)}
  164. if not roots:
  165. raise ValueError('must define at least one ordering operation: < > <= >=')
  166. root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__
  167. for opname, opfunc in _convert[root]:
  168. if opname not in roots:
  169. opfunc.__name__ = opname
  170. setattr(cls, opname, opfunc)
  171. return cls
  172. ################################################################################
  173. ### cmp_to_key() function converter
  174. ################################################################################
  175. def cmp_to_key(mycmp):
  176. """Convert a cmp= function into a key= function"""
  177. class K(object):
  178. __slots__ = ['obj']
  179. def __init__(self, obj):
  180. self.obj = obj
  181. def __lt__(self, other):
  182. return mycmp(self.obj, other.obj) < 0
  183. def __gt__(self, other):
  184. return mycmp(self.obj, other.obj) > 0
  185. def __eq__(self, other):
  186. return mycmp(self.obj, other.obj) == 0
  187. def __le__(self, other):
  188. return mycmp(self.obj, other.obj) <= 0
  189. def __ge__(self, other):
  190. return mycmp(self.obj, other.obj) >= 0
  191. __hash__ = None
  192. return K
  193. try:
  194. from _functools import cmp_to_key
  195. except ImportError:
  196. pass
  197. ################################################################################
  198. ### partial() argument application
  199. ################################################################################
  200. # Purely functional, no descriptor behaviour
  201. class partial:
  202. """New function with partial application of the given arguments
  203. and keywords.
  204. """
  205. __slots__ = "func", "args", "keywords", "__dict__", "__weakref__"
  206. def __new__(*args, **keywords):
  207. if not args:
  208. raise TypeError("descriptor '__new__' of partial needs an argument")
  209. if len(args) < 2:
  210. raise TypeError("type 'partial' takes at least one argument")
  211. cls, func, *args = args
  212. if not callable(func):
  213. raise TypeError("the first argument must be callable")
  214. args = tuple(args)
  215. if hasattr(func, "func"):
  216. args = func.args + args
  217. tmpkw = func.keywords.copy()
  218. tmpkw.update(keywords)
  219. keywords = tmpkw
  220. del tmpkw
  221. func = func.func
  222. self = super(partial, cls).__new__(cls)
  223. self.func = func
  224. self.args = args
  225. self.keywords = keywords
  226. return self
  227. def __call__(*args, **keywords):
  228. if not args:
  229. raise TypeError("descriptor '__call__' of partial needs an argument")
  230. self, *args = args
  231. newkeywords = self.keywords.copy()
  232. newkeywords.update(keywords)
  233. return self.func(*self.args, *args, **newkeywords)
  234. @recursive_repr()
  235. def __repr__(self):
  236. qualname = type(self).__qualname__
  237. args = [repr(self.func)]
  238. args.extend(repr(x) for x in self.args)
  239. args.extend(f"{k}={v!r}" for (k, v) in self.keywords.items())
  240. if type(self).__module__ == "functools":
  241. return f"functools.{qualname}({', '.join(args)})"
  242. return f"{qualname}({', '.join(args)})"
  243. def __reduce__(self):
  244. return type(self), (self.func,), (self.func, self.args,
  245. self.keywords or None, self.__dict__ or None)
  246. def __setstate__(self, state):
  247. if not isinstance(state, tuple):
  248. raise TypeError("argument to __setstate__ must be a tuple")
  249. if len(state) != 4:
  250. raise TypeError(f"expected 4 items in state, got {len(state)}")
  251. func, args, kwds, namespace = state
  252. if (not callable(func) or not isinstance(args, tuple) or
  253. (kwds is not None and not isinstance(kwds, dict)) or
  254. (namespace is not None and not isinstance(namespace, dict))):
  255. raise TypeError("invalid partial state")
  256. args = tuple(args) # just in case it's a subclass
  257. if kwds is None:
  258. kwds = {}
  259. elif type(kwds) is not dict: # XXX does it need to be *exactly* dict?
  260. kwds = dict(kwds)
  261. if namespace is None:
  262. namespace = {}
  263. self.__dict__ = namespace
  264. self.func = func
  265. self.args = args
  266. self.keywords = kwds
  267. try:
  268. from _functools import partial
  269. except ImportError:
  270. pass
  271. # Descriptor version
  272. class partialmethod(object):
  273. """Method descriptor with partial application of the given arguments
  274. and keywords.
  275. Supports wrapping existing descriptors and handles non-descriptor
  276. callables as instance methods.
  277. """
  278. def __init__(self, func, *args, **keywords):
  279. if not callable(func) and not hasattr(func, "__get__"):
  280. raise TypeError("{!r} is not callable or a descriptor"
  281. .format(func))
  282. # func could be a descriptor like classmethod which isn't callable,
  283. # so we can't inherit from partial (it verifies func is callable)
  284. if isinstance(func, partialmethod):
  285. # flattening is mandatory in order to place cls/self before all
  286. # other arguments
  287. # it's also more efficient since only one function will be called
  288. self.func = func.func
  289. self.args = func.args + args
  290. self.keywords = func.keywords.copy()
  291. self.keywords.update(keywords)
  292. else:
  293. self.func = func
  294. self.args = args
  295. self.keywords = keywords
  296. def __repr__(self):
  297. args = ", ".join(map(repr, self.args))
  298. keywords = ", ".join("{}={!r}".format(k, v)
  299. for k, v in self.keywords.items())
  300. format_string = "{module}.{cls}({func}, {args}, {keywords})"
  301. return format_string.format(module=self.__class__.__module__,
  302. cls=self.__class__.__qualname__,
  303. func=self.func,
  304. args=args,
  305. keywords=keywords)
  306. def _make_unbound_method(self):
  307. def _method(*args, **keywords):
  308. call_keywords = self.keywords.copy()
  309. call_keywords.update(keywords)
  310. cls_or_self, *rest = args
  311. call_args = (cls_or_self,) + self.args + tuple(rest)
  312. return self.func(*call_args, **call_keywords)
  313. _method.__isabstractmethod__ = self.__isabstractmethod__
  314. _method._partialmethod = self
  315. return _method
  316. def __get__(self, obj, cls):
  317. get = getattr(self.func, "__get__", None)
  318. result = None
  319. if get is not None:
  320. new_func = get(obj, cls)
  321. if new_func is not self.func:
  322. # Assume __get__ returning something new indicates the
  323. # creation of an appropriate callable
  324. result = partial(new_func, *self.args, **self.keywords)
  325. try:
  326. result.__self__ = new_func.__self__
  327. except AttributeError:
  328. pass
  329. if result is None:
  330. # If the underlying descriptor didn't do anything, treat this
  331. # like an instance method
  332. result = self._make_unbound_method().__get__(obj, cls)
  333. return result
  334. @property
  335. def __isabstractmethod__(self):
  336. return getattr(self.func, "__isabstractmethod__", False)
  337. ################################################################################
  338. ### LRU Cache function decorator
  339. ################################################################################
  340. _CacheInfo = namedtuple("CacheInfo", ["hits", "misses", "maxsize", "currsize"])
  341. class _HashedSeq(list):
  342. """ This class guarantees that hash() will be called no more than once
  343. per element. This is important because the lru_cache() will hash
  344. the key multiple times on a cache miss.
  345. """
  346. __slots__ = 'hashvalue'
  347. def __init__(self, tup, hash=hash):
  348. self[:] = tup
  349. self.hashvalue = hash(tup)
  350. def __hash__(self):
  351. return self.hashvalue
  352. def _make_key(args, kwds, typed,
  353. kwd_mark = (object(),),
  354. fasttypes = {int, str},
  355. tuple=tuple, type=type, len=len):
  356. """Make a cache key from optionally typed positional and keyword arguments
  357. The key is constructed in a way that is flat as possible rather than
  358. as a nested structure that would take more memory.
  359. If there is only a single argument and its data type is known to cache
  360. its hash value, then that argument is returned without a wrapper. This
  361. saves space and improves lookup speed.
  362. """
  363. # All of code below relies on kwds preserving the order input by the user.
  364. # Formerly, we sorted() the kwds before looping. The new way is *much*
  365. # faster; however, it means that f(x=1, y=2) will now be treated as a
  366. # distinct call from f(y=2, x=1) which will be cached separately.
  367. key = args
  368. if kwds:
  369. key += kwd_mark
  370. for item in kwds.items():
  371. key += item
  372. if typed:
  373. key += tuple(type(v) for v in args)
  374. if kwds:
  375. key += tuple(type(v) for v in kwds.values())
  376. elif len(key) == 1 and type(key[0]) in fasttypes:
  377. return key[0]
  378. return _HashedSeq(key)
  379. def lru_cache(maxsize=128, typed=False):
  380. """Least-recently-used cache decorator.
  381. If *maxsize* is set to None, the LRU features are disabled and the cache
  382. can grow without bound.
  383. If *typed* is True, arguments of different types will be cached separately.
  384. For example, f(3.0) and f(3) will be treated as distinct calls with
  385. distinct results.
  386. Arguments to the cached function must be hashable.
  387. View the cache statistics named tuple (hits, misses, maxsize, currsize)
  388. with f.cache_info(). Clear the cache and statistics with f.cache_clear().
  389. Access the underlying function with f.__wrapped__.
  390. See: http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used
  391. """
  392. # Users should only access the lru_cache through its public API:
  393. # cache_info, cache_clear, and f.__wrapped__
  394. # The internals of the lru_cache are encapsulated for thread safety and
  395. # to allow the implementation to change (including a possible C version).
  396. # Early detection of an erroneous call to @lru_cache without any arguments
  397. # resulting in the inner function being passed to maxsize instead of an
  398. # integer or None. Negative maxsize is treated as 0.
  399. if isinstance(maxsize, int):
  400. if maxsize < 0:
  401. maxsize = 0
  402. elif maxsize is not None:
  403. raise TypeError('Expected maxsize to be an integer or None')
  404. def decorating_function(user_function):
  405. wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
  406. return update_wrapper(wrapper, user_function)
  407. return decorating_function
  408. def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
  409. # Constants shared by all lru cache instances:
  410. sentinel = object() # unique object used to signal cache misses
  411. make_key = _make_key # build a key from the function arguments
  412. PREV, NEXT, KEY, RESULT = 0, 1, 2, 3 # names for the link fields
  413. cache = {}
  414. hits = misses = 0
  415. full = False
  416. cache_get = cache.get # bound method to lookup a key or return None
  417. cache_len = cache.__len__ # get cache size without calling len()
  418. lock = RLock() # because linkedlist updates aren't threadsafe
  419. root = [] # root of the circular doubly linked list
  420. root[:] = [root, root, None, None] # initialize by pointing to self
  421. if maxsize == 0:
  422. def wrapper(*args, **kwds):
  423. # No caching -- just a statistics update
  424. nonlocal misses
  425. misses += 1
  426. result = user_function(*args, **kwds)
  427. return result
  428. elif maxsize is None:
  429. def wrapper(*args, **kwds):
  430. # Simple caching without ordering or size limit
  431. nonlocal hits, misses
  432. key = make_key(args, kwds, typed)
  433. result = cache_get(key, sentinel)
  434. if result is not sentinel:
  435. hits += 1
  436. return result
  437. misses += 1
  438. result = user_function(*args, **kwds)
  439. cache[key] = result
  440. return result
  441. else:
  442. def wrapper(*args, **kwds):
  443. # Size limited caching that tracks accesses by recency
  444. nonlocal root, hits, misses, full
  445. key = make_key(args, kwds, typed)
  446. with lock:
  447. link = cache_get(key)
  448. if link is not None:
  449. # Move the link to the front of the circular queue
  450. link_prev, link_next, _key, result = link
  451. link_prev[NEXT] = link_next
  452. link_next[PREV] = link_prev
  453. last = root[PREV]
  454. last[NEXT] = root[PREV] = link
  455. link[PREV] = last
  456. link[NEXT] = root
  457. hits += 1
  458. return result
  459. misses += 1
  460. result = user_function(*args, **kwds)
  461. with lock:
  462. if key in cache:
  463. # Getting here means that this same key was added to the
  464. # cache while the lock was released. Since the link
  465. # update is already done, we need only return the
  466. # computed result and update the count of misses.
  467. pass
  468. elif full:
  469. # Use the old root to store the new key and result.
  470. oldroot = root
  471. oldroot[KEY] = key
  472. oldroot[RESULT] = result
  473. # Empty the oldest link and make it the new root.
  474. # Keep a reference to the old key and old result to
  475. # prevent their ref counts from going to zero during the
  476. # update. That will prevent potentially arbitrary object
  477. # clean-up code (i.e. __del__) from running while we're
  478. # still adjusting the links.
  479. root = oldroot[NEXT]
  480. oldkey = root[KEY]
  481. oldresult = root[RESULT]
  482. root[KEY] = root[RESULT] = None
  483. # Now update the cache dictionary.
  484. del cache[oldkey]
  485. # Save the potentially reentrant cache[key] assignment
  486. # for last, after the root and links have been put in
  487. # a consistent state.
  488. cache[key] = oldroot
  489. else:
  490. # Put result in a new link at the front of the queue.
  491. last = root[PREV]
  492. link = [last, root, key, result]
  493. last[NEXT] = root[PREV] = cache[key] = link
  494. # Use the cache_len bound method instead of the len() function
  495. # which could potentially be wrapped in an lru_cache itself.
  496. full = (cache_len() >= maxsize)
  497. return result
  498. def cache_info():
  499. """Report cache statistics"""
  500. with lock:
  501. return _CacheInfo(hits, misses, maxsize, cache_len())
  502. def cache_clear():
  503. """Clear the cache and cache statistics"""
  504. nonlocal hits, misses, full
  505. with lock:
  506. cache.clear()
  507. root[:] = [root, root, None, None]
  508. hits = misses = 0
  509. full = False
  510. wrapper.cache_info = cache_info
  511. wrapper.cache_clear = cache_clear
  512. return wrapper
  513. try:
  514. from _functools import _lru_cache_wrapper
  515. except ImportError:
  516. pass
  517. ################################################################################
  518. ### singledispatch() - single-dispatch generic function decorator
  519. ################################################################################
  520. def _c3_merge(sequences):
  521. """Merges MROs in *sequences* to a single MRO using the C3 algorithm.
  522. Adapted from http://www.python.org/download/releases/2.3/mro/.
  523. """
  524. result = []
  525. while True:
  526. sequences = [s for s in sequences if s] # purge empty sequences
  527. if not sequences:
  528. return result
  529. for s1 in sequences: # find merge candidates among seq heads
  530. candidate = s1[0]
  531. for s2 in sequences:
  532. if candidate in s2[1:]:
  533. candidate = None
  534. break # reject the current head, it appears later
  535. else:
  536. break
  537. if candidate is None:
  538. raise RuntimeError("Inconsistent hierarchy")
  539. result.append(candidate)
  540. # remove the chosen candidate
  541. for seq in sequences:
  542. if seq[0] == candidate:
  543. del seq[0]
  544. def _c3_mro(cls, abcs=None):
  545. """Computes the method resolution order using extended C3 linearization.
  546. If no *abcs* are given, the algorithm works exactly like the built-in C3
  547. linearization used for method resolution.
  548. If given, *abcs* is a list of abstract base classes that should be inserted
  549. into the resulting MRO. Unrelated ABCs are ignored and don't end up in the
  550. result. The algorithm inserts ABCs where their functionality is introduced,
  551. i.e. issubclass(cls, abc) returns True for the class itself but returns
  552. False for all its direct base classes. Implicit ABCs for a given class
  553. (either registered or inferred from the presence of a special method like
  554. __len__) are inserted directly after the last ABC explicitly listed in the
  555. MRO of said class. If two implicit ABCs end up next to each other in the
  556. resulting MRO, their ordering depends on the order of types in *abcs*.
  557. """
  558. for i, base in enumerate(reversed(cls.__bases__)):
  559. if hasattr(base, '__abstractmethods__'):
  560. boundary = len(cls.__bases__) - i
  561. break # Bases up to the last explicit ABC are considered first.
  562. else:
  563. boundary = 0
  564. abcs = list(abcs) if abcs else []
  565. explicit_bases = list(cls.__bases__[:boundary])
  566. abstract_bases = []
  567. other_bases = list(cls.__bases__[boundary:])
  568. for base in abcs:
  569. if issubclass(cls, base) and not any(
  570. issubclass(b, base) for b in cls.__bases__
  571. ):
  572. # If *cls* is the class that introduces behaviour described by
  573. # an ABC *base*, insert said ABC to its MRO.
  574. abstract_bases.append(base)
  575. for base in abstract_bases:
  576. abcs.remove(base)
  577. explicit_c3_mros = [_c3_mro(base, abcs=abcs) for base in explicit_bases]
  578. abstract_c3_mros = [_c3_mro(base, abcs=abcs) for base in abstract_bases]
  579. other_c3_mros = [_c3_mro(base, abcs=abcs) for base in other_bases]
  580. return _c3_merge(
  581. [[cls]] +
  582. explicit_c3_mros + abstract_c3_mros + other_c3_mros +
  583. [explicit_bases] + [abstract_bases] + [other_bases]
  584. )
  585. def _compose_mro(cls, types):
  586. """Calculates the method resolution order for a given class *cls*.
  587. Includes relevant abstract base classes (with their respective bases) from
  588. the *types* iterable. Uses a modified C3 linearization algorithm.
  589. """
  590. bases = set(cls.__mro__)
  591. # Remove entries which are already present in the __mro__ or unrelated.
  592. def is_related(typ):
  593. return (typ not in bases and hasattr(typ, '__mro__')
  594. and issubclass(cls, typ))
  595. types = [n for n in types if is_related(n)]
  596. # Remove entries which are strict bases of other entries (they will end up
  597. # in the MRO anyway.
  598. def is_strict_base(typ):
  599. for other in types:
  600. if typ != other and typ in other.__mro__:
  601. return True
  602. return False
  603. types = [n for n in types if not is_strict_base(n)]
  604. # Subclasses of the ABCs in *types* which are also implemented by
  605. # *cls* can be used to stabilize ABC ordering.
  606. type_set = set(types)
  607. mro = []
  608. for typ in types:
  609. found = []
  610. for sub in typ.__subclasses__():
  611. if sub not in bases and issubclass(cls, sub):
  612. found.append([s for s in sub.__mro__ if s in type_set])
  613. if not found:
  614. mro.append(typ)
  615. continue
  616. # Favor subclasses with the biggest number of useful bases
  617. found.sort(key=len, reverse=True)
  618. for sub in found:
  619. for subcls in sub:
  620. if subcls not in mro:
  621. mro.append(subcls)
  622. return _c3_mro(cls, abcs=mro)
  623. def _find_impl(cls, registry):
  624. """Returns the best matching implementation from *registry* for type *cls*.
  625. Where there is no registered implementation for a specific type, its method
  626. resolution order is used to find a more generic implementation.
  627. Note: if *registry* does not contain an implementation for the base
  628. *object* type, this function may return None.
  629. """
  630. mro = _compose_mro(cls, registry.keys())
  631. match = None
  632. for t in mro:
  633. if match is not None:
  634. # If *match* is an implicit ABC but there is another unrelated,
  635. # equally matching implicit ABC, refuse the temptation to guess.
  636. if (t in registry and t not in cls.__mro__
  637. and match not in cls.__mro__
  638. and not issubclass(match, t)):
  639. raise RuntimeError("Ambiguous dispatch: {} or {}".format(
  640. match, t))
  641. break
  642. if t in registry:
  643. match = t
  644. return registry.get(match)
  645. def singledispatch(func):
  646. """Single-dispatch generic function decorator.
  647. Transforms a function into a generic function, which can have different
  648. behaviours depending upon the type of its first argument. The decorated
  649. function acts as the default implementation, and additional
  650. implementations can be registered using the register() attribute of the
  651. generic function.
  652. """
  653. # There are many programs that use functools without singledispatch, so we
  654. # trade-off making singledispatch marginally slower for the benefit of
  655. # making start-up of such applications slightly faster.
  656. import types, weakref
  657. registry = {}
  658. dispatch_cache = weakref.WeakKeyDictionary()
  659. cache_token = None
  660. def dispatch(cls):
  661. """generic_func.dispatch(cls) -> <function implementation>
  662. Runs the dispatch algorithm to return the best available implementation
  663. for the given *cls* registered on *generic_func*.
  664. """
  665. nonlocal cache_token
  666. if cache_token is not None:
  667. current_token = get_cache_token()
  668. if cache_token != current_token:
  669. dispatch_cache.clear()
  670. cache_token = current_token
  671. try:
  672. impl = dispatch_cache[cls]
  673. except KeyError:
  674. try:
  675. impl = registry[cls]
  676. except KeyError:
  677. impl = _find_impl(cls, registry)
  678. dispatch_cache[cls] = impl
  679. return impl
  680. def register(cls, func=None):
  681. """generic_func.register(cls, func) -> func
  682. Registers a new implementation for the given *cls* on a *generic_func*.
  683. """
  684. nonlocal cache_token
  685. if func is None:
  686. if isinstance(cls, type):
  687. return lambda f: register(cls, f)
  688. ann = getattr(cls, '__annotations__', {})
  689. if not ann:
  690. raise TypeError(
  691. f"Invalid first argument to `register()`: {cls!r}. "
  692. f"Use either `@register(some_class)` or plain `@register` "
  693. f"on an annotated function."
  694. )
  695. func = cls
  696. # only import typing if annotation parsing is necessary
  697. from typing import get_type_hints
  698. argname, cls = next(iter(get_type_hints(func).items()))
  699. assert isinstance(cls, type), (
  700. f"Invalid annotation for {argname!r}. {cls!r} is not a class."
  701. )
  702. registry[cls] = func
  703. if cache_token is None and hasattr(cls, '__abstractmethods__'):
  704. cache_token = get_cache_token()
  705. dispatch_cache.clear()
  706. return func
  707. def wrapper(*args, **kw):
  708. if not args:
  709. raise TypeError(f'{funcname} requires at least '
  710. '1 positional argument')
  711. return dispatch(args[0].__class__)(*args, **kw)
  712. funcname = getattr(func, '__name__', 'singledispatch function')
  713. registry[object] = func
  714. wrapper.register = register
  715. wrapper.dispatch = dispatch
  716. wrapper.registry = types.MappingProxyType(registry)
  717. wrapper._clear_cache = dispatch_cache.clear
  718. update_wrapper(wrapper, func)
  719. return wrapper
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...