Skip to content

Commit d4b6b81

Browse files
committed
refactoring+optimization (pandas df)
1 parent 77e5e33 commit d4b6b81

File tree

4 files changed

+39
-61
lines changed

4 files changed

+39
-61
lines changed

smct/multimonitortool.py

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,27 @@
1212

1313

1414
def get_monitor_selection_list():
15-
_monitor_selection_list = []
1615
monitor_df = _get_monitor_df()
17-
# pylint: disable=unused-variable
18-
for index, row in monitor_df.iterrows():
19-
_monitor_id = row[MMT_CSV_NAME][-1]
20-
_monitor_name = row[MMT_CSV_MONITOR_NAME]
21-
_monitor_serial = row[MMT_CSV_SERIAL_NUMBER]
22-
_display_string = f"{_monitor_id} | {_monitor_name} | {_monitor_serial}"
23-
_monitor_selection_list.append(_display_string)
24-
log(f"Monitor detected: {_display_string}")
25-
26-
_monitor_selection_list.sort()
27-
return _monitor_selection_list
16+
monitor_df["display_string"] = monitor_df.apply(
17+
lambda row: f"{row[MMT_CSV_NAME][-1]} | {row[MMT_CSV_MONITOR_NAME]} | {row[MMT_CSV_SERIAL_NUMBER]}",
18+
axis=1,
19+
)
20+
monitor_df["display_string"].apply(lambda x: log(f"Monitor detected: {x}"))
21+
return sorted(monitor_df["display_string"].tolist())
2822

2923

3024
def _get_monitor_df():
3125
_run_mmt_command("/scomma", paths.MMT_CSV_PATH)
32-
data = pd.read_csv(paths.MMT_CSV_PATH)
33-
return data
26+
return pd.read_csv(paths.MMT_CSV_PATH)
3427

3528

3629
def _run_mmt_command(command, destination):
30+
command_line = [config.get_mmt_path_value(), command, destination]
3731
try:
38-
subprocess.run(
39-
[
40-
config.get_mmt_path_value(),
41-
command,
42-
destination,
43-
],
44-
check=True,
45-
)
46-
log(f"MultiMonitorTool.exe {command} {destination}")
32+
subprocess.run(command_line, check=True)
33+
log(f"{' '.join(command_line)}")
4734
except subprocess.CalledProcessError as error:
48-
log(f"MultiMonitorTool.exe {command} {destination} failed: {error}")
35+
log(f"{' '.join(command_line)} failed: {error}")
4936

5037

5138
def save_mmt_config():
@@ -57,11 +44,8 @@ def enable_monitor():
5744

5845

5946
def disable_monitor():
60-
selected_monitor_id = get_selected_monitor_id()
61-
if selected_monitor_id:
47+
if selected_monitor_id := get_selected_monitor_id():
6248
_run_mmt_command("/disable", selected_monitor_id)
63-
else:
64-
log("disable_monitor() - Can't find selected monitor id")
6549

6650

6751
def enable_all_disabled_monitors():
@@ -72,32 +56,31 @@ def enable_all_disabled_monitors():
7256

7357

7458
def get_selected_monitor_id():
75-
# pylint: disable=unused-variable
76-
for index, monitor in _get_monitor_df().iterrows():
77-
if (
78-
monitor[MMT_CSV_MONITOR_NAME] == config.get_monitor_name_value()
79-
and str(monitor[MMT_CSV_SERIAL_NUMBER]) == config.get_monitor_serial_value()
80-
):
81-
return monitor[MMT_CSV_NAME][-1]
59+
condition = (
60+
_get_monitor_df()[MMT_CSV_MONITOR_NAME] == config.get_monitor_name_value()
61+
) & (
62+
_get_monitor_df()[MMT_CSV_SERIAL_NUMBER].astype(str)
63+
== config.get_monitor_serial_value()
64+
)
65+
filtered_df = _get_monitor_df().loc[condition]
66+
if not filtered_df.empty:
67+
return filtered_df.iloc[0][MMT_CSV_NAME][-1]
8268

8369

8470
def _get_all_disabled_monitor_ids():
8571
monitor_df = _get_monitor_df()
86-
disabled_monitor_ids = []
87-
# pylint: disable=unused-variable
88-
for index, monitor in monitor_df.iterrows():
89-
if monitor[MMT_CSV_ACTIVE].lower() == MMT_CSV_NO:
90-
disabled_monitor_ids.append(monitor[MMT_CSV_NAME][-1])
91-
return disabled_monitor_ids
72+
return (
73+
monitor_df[monitor_df[MMT_CSV_ACTIVE].str.lower() == MMT_CSV_NO][MMT_CSV_NAME]
74+
.apply(lambda x: x[-1])
75+
.tolist()
76+
)
9277

9378

9479
def is_selected_monitor_enabled():
95-
# pylint: disable=unused-variable
96-
for index, monitor in _get_monitor_df().iterrows():
97-
if (
98-
monitor[MMT_CSV_MONITOR_NAME] == config.get_monitor_name_value()
99-
and str(monitor[MMT_CSV_SERIAL_NUMBER]) == config.get_monitor_serial_value()
100-
and monitor[MMT_CSV_ACTIVE].lower() == MMT_CSV_YES
101-
):
102-
return True
103-
return False
80+
df = _get_monitor_df()
81+
condition = (
82+
(df[MMT_CSV_MONITOR_NAME] == config.get_monitor_name_value())
83+
& (df[MMT_CSV_SERIAL_NUMBER].astype(str) == config.get_monitor_serial_value())
84+
& (df[MMT_CSV_ACTIVE].str.lower() == MMT_CSV_YES)
85+
)
86+
return condition.any()

smct/paths.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ def get_base_path():
1414

1515
def strip_package_name_from_path(path):
1616
suffix = "\\smct"
17-
if path.endswith(suffix):
18-
return path[: -len(suffix)]
19-
else:
20-
return path
17+
return path[: -len(suffix)] if path.endswith(suffix) else path
2118

2219

2320
# PATHS

smct/tray.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ def open_folder_clicked():
3535
def startup_with_windows_clicked():
3636
_current_start_with_windows_value = config.get_start_with_windows_value()
3737
config.set_start_with_windows_value(not _current_start_with_windows_value)
38-
_toggled_start_with_windows_value = config.get_start_with_windows_value()
3938

40-
if _toggled_start_with_windows_value:
39+
if config.get_start_with_windows_value():
4140
registry.add_to_autostart()
4241
else:
4342
registry.remove_from_autostart()

smct/ui.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,17 @@ def _init_mmt_selection_frame():
7070

7171

7272
def _browse_button_callback():
73-
_exe_path = filedialog.askopenfilename(
73+
if _exe_path := filedialog.askopenfilename(
7474
title=ui_strings.SELECT_MMT_LABEL,
7575
filetypes=[("MultiMonitorTool", "multimonitortool.exe")],
76-
)
77-
if not _exe_path:
78-
print(ui_strings.NO_FILE_SELECTED)
79-
else:
76+
):
8077
config.set_mmt_path_value(_exe_path)
8178
_SELECT_MMT_EXE_FRAME.destroy()
8279
multimonitortool.enable_all_disabled_monitors()
8380
multimonitortool.save_mmt_config()
8481
_init_monitor_selection_frame()
82+
else:
83+
print(ui_strings.NO_FILE_SELECTED)
8584

8685

8786
def _init_monitor_selection_frame():

0 commit comments

Comments
 (0)