Repository navigation
Expand file tree
/
Copy path_functions.py
More file actions
113 lines (97 loc) · 4.01 KB
/
Copy path_functions.py
File metadata and controls
113 lines (97 loc) · 4.01 KB
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
# -*- coding: utf-8 -*-
#
# Copyright 2020-2026 Ramil Nugmanov <nougmanoff@protonmail.com>
# This file is part of chython.
#
# chython is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, see <https://www.gnu.org/licenses/>.
#
from functools import wraps
from itertools import product
from warnings import warn
_SENTINEL = object()
def renamed_name(old, new):
"""Announce a superseded attribute spelling, naming what replaced it.
ONE FUNCTION SO THE MESSAGE HAS ONE WORDING. Every alias routes through here, so the text a
consumer greps for while porting is the same text in all of them, and the removal is one edit.
`stacklevel=3` charges the warning to the CALLER, which is the only person who can act on it: the
three frames are `warn` -> this function -> the property's fget or fset -> the consumer's line.
THE NUMBER IS MEASURED, NOT REASONED. Its counterpart in the compiled core is 1 for the same
intent, because neither a `cdef` helper nor a compiled `def` pushes a Python frame; so the right
number is a property of the call chain rather than of the source, and both are asserted by a test
on the blamed line rather than trusted.
"""
warn(f'`{old}` was renamed to `{new}` and will be removed in a later release; use `{new}`',
DeprecationWarning, stacklevel=3)
def cached_method(func):
"""Cache no-argument method result in instance __dict__. Cleared by flush_cache().
Thread-safe for concurrent reads without locking:
- dict.get/setitem are atomic in CPython 3.14 free-threaded mode
- Wrapped functions are pure (deterministic, read-only on self)
- Duplicate computation on cold cache is benign (same result)
- Mutations must be sequential (caller's responsibility)
"""
key = f'__cached_method_{func.__name__}'
@wraps(func)
def wrapper(self):
val = self.__dict__.get(key, _SENTINEL)
if val is not _SENTINEL:
return val
val = func(self)
self.__dict__[key] = val
return val
return wrapper
# lazy itertools.product with diagonal combination precedence
def lazy_product(*args):
if len(args) == 1:
for x in args[0]:
yield x,
elif not args:
yield ()
else:
gens = [iter(x) for x in args]
empty = [False] * len(args)
pools = [[] for _ in range(len(args))]
indices = set()
reached = 0
while True:
out = []
ind = []
for n, (p, g, e) in enumerate(zip(pools, gens, empty)):
if e:
out.append(p[-1])
else:
try:
x = next(g)
except StopIteration:
if not p: # one of gens empty
return
reached += 1
if reached == len(args):
break
out.append(p[-1])
empty[n] = True
else:
p.append(x)
out.append(x)
ind.append(len(p) - 1)
else:
yield tuple(out)
indices.add(tuple(ind))
continue
break
for ind in product(*(range(len(p)) for p in pools)):
if ind in indices:
continue
yield tuple(p[x] for x, p in zip(ind, pools))
__all__ = ['cached_method', 'lazy_product', 'renamed_name']