pocketpy/python/functools.py
Osama Alwaly caa416e5e2 Improve cmath.complex dunder methods and tests; align lru_cache with stdlib
- Add __rtruediv__, __rpow__, __pos__, __bool__, __hash__, and coercion errors
- Extend __pow__ for complex exponents and real-only complex unwrap
- Tests for rdiv, rpow, bool, pos, float/int errors, hash, complex**complex
- lru_cache: accept typed= and **kwargs for host-Python compatibility when PYTHONPATH shadows this module
- Regenerate _generated.c from prebuild.py

Made-with: Cursor
2026-03-24 18:20:41 +02:00

56 lines
1.5 KiB
Python

class cache:
def __init__(self, f):
self.f = f
self.cache = {}
def __call__(self, *args):
if args not in self.cache:
self.cache[args] = self.f(*args)
return self.cache[args]
class lru_cache:
def __init__(self, maxsize=128, typed=False, **kwargs):
self.maxsize = maxsize
self.cache = {}
def __call__(self, f):
def wrapped(*args):
if args in self.cache:
res = self.cache.pop(args)
self.cache[args] = res
return res
res = f(*args)
if len(self.cache) >= self.maxsize:
first_key = next(iter(self.cache))
self.cache.pop(first_key)
self.cache[args] = res
return res
return wrapped
def reduce(function, sequence, initial=...):
it = iter(sequence)
if initial is ...:
try:
value = next(it)
except StopIteration:
raise TypeError("reduce() of empty sequence with no initial value")
else:
value = initial
for element in it:
value = function(value, element)
return value
class partial:
def __init__(self, f, *args, **kwargs):
self.f = f
if not callable(f):
raise TypeError("the first argument must be callable")
self.args = args
self.kwargs = kwargs
def __call__(self, *args, **kwargs):
kwargs.update(self.kwargs)
return self.f(*self.args, *args, **kwargs)