Improve sorting code
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
# YYYY-MM-DD
|
||||
|
||||
## 2022-06-16
|
||||
|
||||
- Edit uvloop marker.
|
||||
- Improve sorting code.
|
||||
|
||||
## 2022-05-28
|
||||
|
||||
- Remove some sources.
|
||||
|
||||
@@ -82,6 +82,14 @@ class Folder:
|
||||
self.path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
def speed_sorting_key(proxy: Proxy) -> float:
|
||||
return proxy.timeout
|
||||
|
||||
|
||||
def alphabet_sorting_key(proxy: Proxy) -> Tuple[int, ...]:
|
||||
return tuple(map(int, proxy.socket_address.replace(":", ".").split(".")))
|
||||
|
||||
|
||||
class ProxyScraperChecker:
|
||||
"""HTTP, SOCKS4, SOCKS5 proxies scraper and checker."""
|
||||
|
||||
@@ -313,22 +321,14 @@ class ProxyScraperChecker:
|
||||
|
||||
@property
|
||||
def sorted_proxies(self) -> Dict[str, List[Proxy]]:
|
||||
key = self._sorting_key
|
||||
key: Union[
|
||||
Callable[[Proxy], float], Callable[[Proxy], Tuple[int, ...]]
|
||||
] = (speed_sorting_key if self.sort_by_speed else alphabet_sorting_key)
|
||||
return {
|
||||
proto: sorted(proxies, key=key)
|
||||
for proto, proxies in self.proxies.items()
|
||||
}
|
||||
|
||||
@property
|
||||
def _sorting_key(
|
||||
self,
|
||||
) -> Union[Callable[[Proxy], float], Callable[[Proxy], Tuple[int, ...]]]:
|
||||
if self.sort_by_speed:
|
||||
return lambda proxy: proxy.timeout
|
||||
return lambda proxy: tuple(
|
||||
map(int, proxy.socket_address.replace(":", ".").split("."))
|
||||
)
|
||||
|
||||
@property
|
||||
def _progress(self) -> Progress:
|
||||
return Progress(
|
||||
|
||||
Reference in New Issue
Block a user