start moving away from fishhook onto dedicated type sub-classes, which allows for pypy to come along

This commit is contained in:
Stefan Harmuth 2025-01-06 09:14:49 +01:00
parent 270866387e
commit 17c41a0c63
3 changed files with 22 additions and 53 deletions

View File

@ -1,5 +1,4 @@
bs4~=0.0.1
beautifulsoup4~=4.12.2
fishhook~=0.2.9
requests~=2.31.0
beautifulsoup4~=4.12
requests~=2.32
tqdm~=4.66

View File

@ -2,8 +2,6 @@ import datetime
import inspect
import os.path
import sys
from fishhook import hook
from functools import wraps
from typing import Any
@ -140,51 +138,3 @@ def human_readable_time_from_ns(ns: int) -> str:
time_parts.insert(0, "%d%s" % (p, unit))
if ns == 0:
return ", ".join(time_parts)
def cache(func):
saved = {}
@wraps(func)
def new_func(*args):
if args in saved:
return saved[args]
result = func(*args)
saved[args] = result
return result
return new_func
@hook(list)
def intersection(self, *args) -> list:
ret = set(self).intersection(*args)
return list(ret)
@hook(list)
def __and__(self, *args) -> list:
return self.intersection(*args)
@hook(str)
def intersection(self, *args) -> str:
ret = set(self).intersection(*args)
return "".join(list(ret))
@hook(str)
def __and__(self, *args) -> str:
return self.intersection(*args)
@hook(int)
def sum_digits(self) -> int:
s = 0
num = self
while num > 0:
s += num % 10
num //= 10
return s

20
src/tools/types.py Normal file
View File

@ -0,0 +1,20 @@
from __future__ import annotations
from collections.abc import Iterable
class Integer(int):
def digits(self) -> Iterable[int]:
for x in str(self):
yield int(x)
def digits_sum(self) -> int:
return sum(int(x) for x in str(self))
class String(str):
def swap(self, x: int, y: int) -> String:
return String(self[:x] + self[y] + self[x + 1 : y] + self[x] + self[y + 1 :])
class List(list):
pass