Skip to content

Commit 9973af7

Browse files
committed
add rank_df util
1 parent bd143d4 commit 9973af7

File tree

6 files changed

+62
-24
lines changed

6 files changed

+62
-24
lines changed

library/mediafiles/torrents_stop.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
from library import usage
55
from library.mediafiles.torrents_start import start_qBittorrent
6-
from library.utils import arggroups, consts, devices, iterables, nums, path_utils, printing
6+
from library.playback.torrents_info import qbt_get_tracker
7+
from library.utils import arggroups, consts, devices, nums, path_utils, printing
78
from library.utils.log_utils import log
89

910

@@ -88,13 +89,8 @@ def torrents_stop():
8889
new_path = Path(path_utils.mountpoint(torrent.content_path)) / new_path
8990

9091
if args.tracker_dirnames:
91-
tracker = torrent.tracker
92-
if not tracker:
93-
tracker = iterables.safe_unpack(
94-
tr.url for tr in qbt_client.torrents_trackers(torrent.hash) if tr.url.startswith("http")
95-
)
96-
if tracker:
97-
domain = path_utils.domain_from_url(tracker)
92+
domain = qbt_get_tracker(qbt_client, torrent)
93+
if domain:
9894
new_path /= domain
9995

10096
new_path.mkdir(parents=True, exist_ok=True)

library/mediafiles/torrents_stop_incomplete.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
from library import usage
55
from library.mediafiles.torrents_start import start_qBittorrent
6-
from library.utils import arggroups, consts, devices, iterables, path_utils, printing, strings
6+
from library.playback.torrents_info import qbt_get_tracker
7+
from library.utils import arggroups, consts, devices, path_utils, printing, strings
78
from library.utils.log_utils import log
89

910

@@ -114,13 +115,8 @@ def torrents_stop_incomplete():
114115
new_path = Path(path_utils.mountpoint(torrent.content_path)) / new_path
115116

116117
if args.tracker_dirnames:
117-
tracker = torrent.tracker
118-
if not tracker:
119-
tracker = iterables.safe_unpack(
120-
tr.url for tr in qbt_client.torrents_trackers(torrent.hash) if tr.url.startswith("http")
121-
)
122-
if tracker:
123-
domain = path_utils.domain_from_url(tracker)
118+
domain = qbt_get_tracker(qbt_client, torrent)
119+
if domain:
124120
new_path /= domain
125121

126122
new_path.mkdir(parents=True, exist_ok=True)

library/multidb/allocate_torrents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def allocate_torrents():
100100
torrents = list(args.db.query(*sqlgroups.playlists_fs_sql(args, limit=None)))
101101
total_size = sum(d["size"] for d in torrents)
102102
print(f"{len(torrents)} undownloaded torrents. {strings.file_size(total_size)} total space")
103-
iterables.count_category(torrents, "tracker")
103+
iterables.list_dict_value_counts(torrents, "tracker")
104104

105105
if not torrents:
106106
processes.no_media_found()

library/playback/torrents_info.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ def parse_args():
1919
return args
2020

2121

22+
def qbt_get_tracker(qbt_client, torrent):
23+
tracker = torrent.tracker
24+
if not tracker:
25+
tracker = iterables.safe_unpack(
26+
tr.url for tr in qbt_client.torrents_trackers(torrent.hash) if tr.url.startswith("http")
27+
)
28+
return domain_from_url(tracker)
29+
30+
2231
def torrents_info():
2332
args = parse_args()
2433

@@ -70,12 +79,7 @@ def torrents_info():
7079

7180
torrents_by_tracker = {}
7281
for torrent in all_torrents:
73-
tracker = torrent.tracker
74-
if not tracker:
75-
tracker = iterables.safe_unpack(
76-
tr.url for tr in qbt_client.torrents_trackers(torrent.hash) if tr.url.startswith("http")
77-
)
78-
torrents_by_tracker.setdefault(domain_from_url(tracker), []).append(torrent)
82+
torrents_by_tracker.setdefault(qbt_get_tracker(qbt_client, torrent), []).append(torrent)
7983

8084
interesting_states = [
8185
"stoppedUP",

library/utils/iterables.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import math
2+
from collections import Counter
23
from collections.abc import Iterable, Iterator
34
from functools import wraps
45
from typing import Any
@@ -149,7 +150,7 @@ def list_dict_unique(data: list[dict], unique_keys: list[str]) -> list[dict]:
149150
return list_
150151

151152

152-
def count_category(list_of_dicts, key_name):
153+
def list_dict_value_counts(list_of_dicts, key_name):
153154
category_counts = {}
154155
for item in list_of_dicts:
155156
category = item.get(key_name)
@@ -251,3 +252,8 @@ def zipkw(**kwargs):
251252

252253
for combination in zip(*values):
253254
yield dict(zip(keys, combination))
255+
256+
257+
def value_counts(input_list):
258+
counts = Counter(input_list)
259+
return [counts[item] for item in input_list]

library/utils/pd_utils.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,39 @@ def available_name(df, column_name):
4949
else:
5050
column_name = f"{column_name}_1"
5151
return column_name
52+
53+
54+
def rank_dataframe(df, column_weights):
55+
"""
56+
ranked_df = rank_dataframe(
57+
df,
58+
column_weights={
59+
"progress": {"direction": "desc", "weight": 6},
60+
"size": {"direction": "asc", "weight": 3}
61+
}
62+
)
63+
"""
64+
ranks = df[column_weights.keys()].apply(
65+
lambda x: x.rank(
66+
method="min",
67+
na_option="bottom",
68+
ascending=column_weights.get(x.name, {}).get("direction") == "asc",
69+
)
70+
* column_weights.get(x.name, {}).get("weight", 1),
71+
)
72+
73+
unranked_columns = set(df.select_dtypes(include=["number"]).columns) - set(ranks.columns)
74+
if unranked_columns:
75+
print(
76+
"Unranked columns:\n"
77+
+ "\n".join([f""" "{s}": {{ 'direction': 'desc' }}, """ for s in unranked_columns]),
78+
)
79+
80+
scaled_ranks = (ranks - 1) / (len(ranks.columns) - 1)
81+
scaled_df = df.iloc[scaled_ranks.sum(axis=1).sort_values().index]
82+
return scaled_df.reset_index(drop=True)
83+
84+
85+
def count_category(df, key_name):
86+
df[f"{key_name}_count"] = df.groupby(key_name)[key_name].transform("size")
87+
return df

0 commit comments

Comments
 (0)