Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comment out useless code #117

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 20 additions & 47 deletions src/pyutils/throttledclientsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def __init__(

self._set_limit()

def add_filter(self, filter: str | re.Pattern, method: str | None = None):
def add_filter(self, filter: UrlFilter, method: str | None = None):
"""Add a filter to filter list"""
if not (method is None or is_HTTP_method(method=method)):
raise ValueError(f"'method' is not None or a valid HTTP method: {method}")
Expand Down Expand Up @@ -144,11 +144,6 @@ def count(self) -> int:
def errors(self) -> int:
return self._errors

# def get_stats(self) -> dict[str, float]:
# """Get session statistics"""
# res = {'rate' : self.rate, 'rate_limit': self.rate_limit, 'count' : self.count, 'errors': self.errors }
# return res

@property
def stats(self) -> str:
"""Get session statistics as string"""
Expand All @@ -165,30 +160,25 @@ def stats_dict(self) -> dict[str, float | int]:
}
return res

# def get_stats_str(self) -> str:
# """Print session statistics"""
# error("DEPRECIATED: use self.stats")
# return self.print_stats(self.stats_dict)

@classmethod
def print_stats(cls, stats: dict[str, float | int]) -> str:
try:
rate_limit: float = stats["rate_limit"]
rate: float = stats["rate"]
count: float = stats["count"]
errors: float = stats["errors"]

rate_limit_str: str
if rate_limit >= 1 or rate_limit == 0:
rate_limit_str = f"{rate_limit:.1f} requests/sec"
else:
rate_limit_str = f"{1/rate_limit:.1f} secs/request"

return f"rate limit: {rate_limit_str}, rate: {rate:.1f} request/sec, requests: {count:.0f}, errors: {errors:.0f}"
except KeyError as err:
return f"Incorrect stats format: {err}"
except Exception as err:
return f"Unexpected error: {err}"
# @classmethod
# def print_stats(cls, stats: dict[str, float | int]) -> str:
# try:
# rate_limit: float = stats["rate_limit"]
# rate: float = stats["rate"]
# count: float = stats["count"]
# errors: float = stats["errors"]

# rate_limit_str: str
# if rate_limit >= 1 or rate_limit == 0:
# rate_limit_str = f"{rate_limit:.1f} requests/sec"
# else:
# rate_limit_str = f"{1/rate_limit:.1f} secs/request"

# return f"rate limit: {rate_limit_str}, rate: {rate:.1f} request/sec, requests: {count:.0f}, errors: {errors:.0f}"
# except KeyError as err:
# return f"Incorrect stats format: {err}"
# except Exception as err:
# return f"Unexpected error: {err}"

def reset_counters(self) -> dict[str, float | int]:
"""Reset rate counters and return current results"""
Expand Down Expand Up @@ -267,23 +257,6 @@ async def _request(self, *args, **kwargs) -> ClientResponse:
self._errors += 1
return resp

# def is_limited(self, *args: str) -> bool:
# """Check wether the rate limit should be applied"""
# try:
# if self._rate_limit == 0:
# return False
# url: str = args[1]
# for filter in self._filters:
# if isinstance(filter, re.Pattern) and filter.match(url) is not None:
# return self._limit_filtered
# elif isinstance(filter, str) and url.startswith(filter):
# return self._limit_filtered

# return not self._limit_filtered
# except Exception as err:
# error(f"{err}")
# return True

def is_limited(self, method: str, url: str) -> bool:
"""Check whether the rate limit should be applied"""
try:
Expand Down