Skip to content

Commit a01f0bc

Browse files
authored
Merge pull request #152 from jayceslesar/maint/refurb
🧹 refurb cleanup + one typo
2 parents 58c9491 + 949b445 commit a01f0bc

File tree

8 files changed

+26
-23
lines changed

8 files changed

+26
-23
lines changed

plotly_resampler/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""**plotly\_resampler**: visualizing large sequences."""
22

3+
import contextlib
4+
35
from .aggregation import LTTB, EfficientLTTB, EveryNthPoint
46
from .figure_resampler import FigureResampler, FigureWidgetResampler
57
from .registering import register_plotly_resampler, unregister_plotly_resampler
@@ -20,12 +22,11 @@
2022
]
2123

2224

23-
try: # Enable ipywidgets on google colab!
25+
# Enable ipywidgets on google colab!
26+
with contextlib.suppress(ImportError, ModuleNotFoundError):
2427
import sys
2528

2629
if "google.colab" in sys.modules:
2730
from google.colab import output
2831

2932
output.enable_custom_widget_manager()
30-
except (ImportError, ModuleNotFoundError):
31-
pass

plotly_resampler/aggregation/aggregators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __init__(self, interleave_gaps: bool = True, nan_position="end"):
7676
super().__init__(
7777
interleave_gaps,
7878
nan_position,
79-
dtype_regex_list=[rf"{dtype}\d*" for dtype in ["float", "int", "uint"]]
79+
dtype_regex_list=[rf"{dtype}\d*" for dtype in ("float", "int", "uint")]
8080
+ ["category", "bool"],
8181
)
8282

@@ -278,7 +278,7 @@ def __init__(self, interleave_gaps: bool = True, nan_position="end"):
278278
super().__init__(
279279
interleave_gaps,
280280
nan_position,
281-
dtype_regex_list=[rf"{dtype}\d*" for dtype in ["float", "int", "uint"]]
281+
dtype_regex_list=[rf"{dtype}\d*" for dtype in ("float", "int", "uint")]
282282
+ ["category", "bool"],
283283
)
284284

plotly_resampler/aggregation/algorithms/lttb_c.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def downsample(x: np.ndarray, y: np.ndarray, n_out: int) -> np.ndarray:
3535
"""
3636
if x.dtype == np.int64 and y.dtype == np.float64:
3737
return downsample_int_double(x, y, n_out)
38-
elif x.dtype == np.int64 and y.dtype == np.int64:
38+
elif x.dtype == y.dtype == np.int64:
3939
return downsample_int_int(x, y, n_out)
4040
elif x.dtype == np.int64 and y.dtype == np.float32:
4141
return downsample_int_float(x, y, n_out)

plotly_resampler/aggregation/algorithms/lttbc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static PyObject *downsample_int_double(PyObject *self, PyObject *args)
156156
}
157157

158158
// This method only returns the index positions of the selected points.
159-
// almost everything can be sucessfully parsed to this so this will be
159+
// almost everything can be successfully parsed to this so this will be
160160
// our fallback method
161161
static PyObject *downsample_double_double(PyObject *self, PyObject *args)
162162
{

plotly_resampler/figure_resampler/figure_resampler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def show_dash(
377377
mode is None or mode in available_modes
378378
), f"mode must be one of {available_modes}"
379379
graph_properties = {} if graph_properties is None else graph_properties
380-
assert "config" not in graph_properties.keys() # There is a param for config
380+
assert "config" not in graph_properties # There is a param for config
381381

382382
# 0. Check if the traces need to be updated when there is a xrange set
383383
# This will be the case when the users has set a xrange (via the `update_layout`
@@ -388,7 +388,7 @@ def show_dash(
388388
if x_range: # when not None
389389
relayout_dict[f"{xaxis_str}.range[0]"] = x_range[0]
390390
relayout_dict[f"{xaxis_str}.range[1]"] = x_range[1]
391-
if len(relayout_dict):
391+
if relayout_dict: # when not empty
392392
update_data = self.construct_update_data(relayout_dict)
393393

394394
if not self._is_no_update(update_data): # when there is an update

plotly_resampler/figure_resampler/figure_resampler_interface.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,12 @@ def _check_update_figure_dict(
433433
if (
434434
xaxis_filter_short == "x"
435435
and (
436-
x_anchor_trace not in [None, "x"]
436+
x_anchor_trace not in (None, "x")
437437
and xaxis_matches != xaxis_filter_short
438438
)
439439
) or (
440440
xaxis_filter_short != "x"
441-
and (xaxis_filter_short not in [x_anchor_trace, xaxis_matches])
441+
and (xaxis_filter_short not in (x_anchor_trace, xaxis_matches))
442442
):
443443
continue
444444

@@ -1260,7 +1260,7 @@ def construct_update_data(
12601260
# 1. Base case - there is an x-range specified in the front-end
12611261
start_matches = self._re_matches(re.compile(r"xaxis\d*.range\[0]"), cl_k)
12621262
stop_matches = self._re_matches(re.compile(r"xaxis\d*.range\[1]"), cl_k)
1263-
if len(start_matches) and len(stop_matches):
1263+
if start_matches and stop_matches: # when both are not empty
12641264
for t_start_key, t_stop_key in zip(start_matches, stop_matches):
12651265
# Check if the xaxis<NUMB> part of xaxis<NUMB>.[0-1] matches
12661266
xaxis = t_start_key.split(".")[0]
@@ -1280,7 +1280,7 @@ def construct_update_data(
12801280
)
12811281
spike_matches = self._re_matches(re.compile(r"xaxis\d*.showspikes"), cl_k)
12821282
# 2.1 Reset-axes -> autorange & reset to the global data view
1283-
if len(autorange_matches) and len(spike_matches):
1283+
if autorange_matches and spike_matches: # when both are not empty
12841284
for autorange_key in autorange_matches:
12851285
if relayout_data[autorange_key]:
12861286
xaxis = autorange_key.split(".")[0]
@@ -1291,14 +1291,16 @@ def construct_update_data(
12911291
)
12921292
# 2.1. Autorange -> do nothing, the autorange will be applied on the
12931293
# current front-end view
1294-
elif len(autorange_matches) and not len(spike_matches):
1294+
elif (
1295+
autorange_matches and not spike_matches
1296+
): # when only autorange is not empty
12951297
# PreventUpdate returns a 204 status code response on the
12961298
# relayout post request
12971299
return dash.no_update
12981300

12991301
# If we do not have any traces to be updated, we will return an empty
13001302
# request response
1301-
if len(updated_trace_indices) == 0:
1303+
if not updated_trace_indices: # when updated_trace_indices is empty
13021304
# PreventUpdate returns a 204 status-code response on the relayout post
13031305
# request
13041306
return dash.no_update
@@ -1336,7 +1338,7 @@ def _parse_dtype_orjson(series: np.ndarray) -> np.ndarray:
13361338
# * float16 and float128 aren't supported with latest orjson versions (3.8.1)
13371339
# * this method assumes that the it will not get a float128 series
13381340
# -> this method can be removed if orjson supports float16
1339-
if series.dtype in [np.float16]:
1341+
if series.dtype == np.float16:
13401342
return series.astype(np.float32)
13411343
return series
13421344

plotly_resampler/figure_resampler/figurewidget_resampler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def _update_x_ranges(self, layout, *x_ranges, force_update: bool = False):
168168
# -> save current xaxis range to _prev_layout
169169
self._prev_layout[xaxis_str]["range"] = x_range
170170

171-
if len(relayout_dict):
171+
if relayout_dict: # when not empty
172172
# Construct the update data
173173
update_data = self.construct_update_data(relayout_dict)
174174

plotly_resampler/figure_resampler/utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def timedelta_to_str(td: pd.Timedelta) -> str:
128128
if c.days > 0:
129129
out_str += f"{c.days}D"
130130
if c.hours > 0 or c.minutes > 0 or c.seconds > 0 or c.milliseconds > 0:
131-
out_str += "_" if len(out_str) else ""
131+
out_str += "_" if out_str else "" # add seperator if non-empty
132132

133133
if c.hours > 0:
134134
out_str += f"{c.hours}h"
@@ -142,11 +142,11 @@ def timedelta_to_str(td: pd.Timedelta) -> str:
142142
else:
143143
out_str += f"{c.seconds}s"
144144
elif c.milliseconds > 0:
145-
out_str += f"{str(c.milliseconds)}ms"
145+
out_str += f"{c.milliseconds}ms"
146146
if c.microseconds > 0:
147-
out_str += f"{str(c.microseconds)}us"
147+
out_str += f"{c.microseconds}us"
148148
if c.nanoseconds > 0:
149-
out_str += f"{str(c.nanoseconds)}ns"
149+
out_str += f"{c.nanoseconds}ns"
150150
return out_str
151151

152152

@@ -156,14 +156,14 @@ def round_td_str(td: pd.Timedelta) -> str:
156156
.. seealso::
157157
:func:`timedelta_to_str`
158158
"""
159-
for t_s in ["D", "H", "min", "s", "ms", "us", "ns"]:
159+
for t_s in ("D", "H", "min", "s", "ms", "us", "ns"):
160160
if td > 0.95 * pd.Timedelta(f"1{t_s}"):
161161
return timedelta_to_str(td.round(t_s))
162162

163163

164164
def round_number_str(number: float) -> str:
165165
if number > 0.95:
166-
for unit, scaling in [("M", int(1e6)), ("k", int(1e3))]:
166+
for unit, scaling in (("M", int(1e6)), ("k", int(1e3))):
167167
if number / scaling > 0.95:
168168
return f"{round(number / scaling)}{unit}"
169169
return str(round(number))

0 commit comments

Comments
 (0)