Skip to content

Commit 58629de

Browse files
author
Jon M. Mease
committed
Cleanup / review of callbacks.py
1 parent 5bd77f3 commit 58629de

File tree

2 files changed

+188
-32
lines changed

2 files changed

+188
-32
lines changed

plotly/callbacks.py

+165-32
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
import typing as typ
1+
from plotly.utils import _list_repr_elided
22

33

44
class InputDeviceState:
5-
def __init__(self, ctrl=None, alt=None, shift=None, meta=None, button=None, buttons=None, **_):
5+
def __init__(self,
6+
ctrl=None,
7+
alt=None,
8+
shift=None,
9+
meta=None,
10+
button=None,
11+
buttons=None,
12+
**_):
13+
614
self._ctrl = ctrl
715
self._alt = alt
816
self._meta = meta
@@ -12,15 +20,22 @@ def __init__(self, ctrl=None, alt=None, shift=None, meta=None, button=None, butt
1220

1321
def __repr__(self):
1422
return """\
15-
InputState(ctrl={ctrl},
16-
alt={alt},
17-
shift={shift},
18-
meta={meta},
19-
button={button},
20-
buttons={buttons})"""
23+
InputDeviceState(
24+
ctrl={ctrl},
25+
alt={alt},
26+
shift={shift},
27+
meta={meta},
28+
button={button},
29+
buttons={buttons})""".format(
30+
ctrl=repr(self.ctrl),
31+
alt=repr(self.alt),
32+
meta=repr(self.meta),
33+
shift=repr(self.shift),
34+
button=repr(self.button),
35+
buttons=repr(self.buttons))
2136

2237
@property
23-
def alt(self) -> bool:
38+
def alt(self):
2439
"""
2540
Whether alt key pressed
2641
@@ -31,7 +46,7 @@ def alt(self) -> bool:
3146
return self._alt
3247

3348
@property
34-
def ctrl(self) -> bool:
49+
def ctrl(self):
3550
"""
3651
Whether ctrl key pressed
3752
@@ -42,7 +57,7 @@ def ctrl(self) -> bool:
4257
return self._ctrl
4358

4459
@property
45-
def shift(self) -> bool:
60+
def shift(self):
4661
"""
4762
Whether shift key pressed
4863
@@ -53,7 +68,7 @@ def shift(self) -> bool:
5368
return self._shift
5469

5570
@property
56-
def meta(self) -> bool:
71+
def meta(self):
5772
"""
5873
Whether meta key pressed
5974
@@ -64,12 +79,15 @@ def meta(self) -> bool:
6479
return self._meta
6580

6681
@property
67-
def button(self) -> int:
82+
def button(self):
6883
"""
69-
Integer code for the button that was pressed on the mouse to trigger the event
84+
Integer code for the button that was pressed on the mouse to trigger
85+
the event
7086
71-
- 0: Main button pressed, usually the left button or the un-initialized state
72-
- 1: Auxiliary button pressed, usually the wheel button or the middle button (if present)
87+
- 0: Main button pressed, usually the left button or the
88+
un-initialized state
89+
- 1: Auxiliary button pressed, usually the wheel button or the middle
90+
button (if present)
7391
- 2: Secondary button pressed, usually the right button
7492
- 3: Fourth button, typically the Browser Back button
7593
- 4: Fifth button, typically the Browser Forward button
@@ -81,9 +99,10 @@ def button(self) -> int:
8199
return self._button
82100

83101
@property
84-
def buttons(self) -> int:
102+
def buttons(self):
85103
"""
86-
Integer code for which combination of buttons are pressed on the mouse when the event is triggered.
104+
Integer code for which combination of buttons are pressed on the
105+
mouse when the event is triggered.
87106
88107
- 0: No button or un-initialized
89108
- 1: Primary button (usually left)
@@ -92,9 +111,11 @@ def buttons(self) -> int:
92111
- 8: 4th button (typically the "Browser Back" button)
93112
- 16: 5th button (typically the "Browser Forward" button)
94113
95-
Combinations of buttons are represented as the decimal form of the bitmask of the values above.
114+
Combinations of buttons are represented as the decimal form of the
115+
bitmask of the values above.
96116
97-
For example, pressing both the primary (1) and auxilary (4) buttons will result in a code of 5
117+
For example, pressing both the primary (1) and auxilary (4) buttons
118+
will result in a code of 5
98119
99120
Returns
100121
-------
@@ -105,31 +126,85 @@ def buttons(self) -> int:
105126

106127
class Points:
107128

108-
def __init__(self, point_inds=None, xs=None, ys=None, trace_name=None, trace_index=None):
129+
def __init__(self,
130+
point_inds=None,
131+
xs=None,
132+
ys=None,
133+
trace_name=None,
134+
trace_index=None):
135+
109136
self._point_inds = point_inds
110137
self._xs = xs
111138
self._ys = ys
112139
self._trace_name = trace_name
113140
self._trace_index = trace_index
114141

142+
def __repr__(self):
143+
return """\
144+
Points(point_inds={point_inds},
145+
xs={xs},
146+
ys={ys},
147+
trace_name={trace_name},
148+
trace_index={trace_index})""".format(
149+
point_inds=_list_repr_elided(self.point_inds),
150+
xs=_list_repr_elided(self.xs),
151+
ys=_list_repr_elided(self.ys),
152+
trace_name=repr(self.trace_name),
153+
trace_index=repr(self.trace_index))
154+
115155
@property
116-
def point_inds(self) -> typ.List[int]:
156+
def point_inds(self):
157+
"""
158+
List of selected indexes into the trace's points
159+
160+
Returns
161+
-------
162+
list[int]
163+
"""
117164
return self._point_inds
118165

119166
@property
120-
def xs(self) -> typ.List:
167+
def xs(self):
168+
"""
169+
List of x-coordinates of selected points
170+
171+
Returns
172+
-------
173+
list[float]
174+
"""
121175
return self._xs
122176

123177
@property
124-
def ys(self) -> typ.List:
178+
def ys(self):
179+
"""
180+
List of y-coordinates of selected points
181+
182+
Returns
183+
-------
184+
list[float]
185+
"""
125186
return self._ys
126187

127188
@property
128-
def trace_name(self) -> str:
189+
def trace_name(self):
190+
"""
191+
Name of the trace
192+
193+
Returns
194+
-------
195+
str
196+
"""
129197
return self._trace_name
130198

131199
@property
132-
def trace_index(self) -> int:
200+
def trace_index(self):
201+
"""
202+
Index of the trace in the figure
203+
204+
Returns
205+
-------
206+
int
207+
"""
133208
return self._trace_index
134209

135210

@@ -139,16 +214,44 @@ def __init__(self, xrange=None, yrange=None, **_):
139214
self._xrange = xrange
140215
self._yrange = yrange
141216

217+
def __repr__(self):
218+
return """\
219+
BoxSelector(xrange={xrange},
220+
yrange={yrange})""".format(
221+
xrange=self.xrange,
222+
yrange=self.yrange)
223+
142224
@property
143-
def type(self) -> str:
225+
def type(self):
226+
"""
227+
The selector's type
228+
229+
Returns
230+
-------
231+
str
232+
"""
144233
return self._type
145234

146235
@property
147-
def xrange(self) -> typ.Tuple[float, float]:
236+
def xrange(self):
237+
"""
238+
x-axis range extents of the box selection
239+
240+
Returns
241+
-------
242+
(float, float)
243+
"""
148244
return self._xrange
149245

150246
@property
151-
def yrange(self) -> typ.Tuple[float, float]:
247+
def yrange(self):
248+
"""
249+
y-axis range extents of the box selection
250+
251+
Returns
252+
-------
253+
(float, float)
254+
"""
152255
return self._yrange
153256

154257

@@ -158,14 +261,44 @@ def __init__(self, xs=None, ys=None, **_):
158261
self._xs = xs
159262
self._ys = ys
160263

264+
def __repr__(self):
265+
return """\
266+
LassoSelector(xs={xs},
267+
ys={ys})""".format(
268+
xs=_list_repr_elided(self.xs, 8),
269+
ys=_list_repr_elided(self.ys, 8))
270+
161271
@property
162-
def type(self) -> str:
272+
def type(self):
273+
"""
274+
The selector's type
275+
276+
Returns
277+
-------
278+
str
279+
"""
163280
return self._type
164281

165282
@property
166-
def xs(self) -> typ.List[float]:
283+
def xs(self):
284+
"""
285+
list of x-axis coordinates of each point in the lasso selection
286+
boundary
287+
288+
Returns
289+
-------
290+
list[float]
291+
"""
167292
return self._xs
168293

169294
@property
170-
def ys(self) -> typ.List[float]:
295+
def ys(self):
296+
"""
297+
list of y-axis coordinates of each point in the lasso selection
298+
boundary
299+
300+
Returns
301+
-------
302+
list[float]
303+
"""
171304
return self._ys

plotly/utils.py

+23
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,26 @@ def _memoize(*all_args, **kwargs):
490490
return result
491491

492492
return decorator(_memoize)
493+
494+
495+
def _list_repr_elided(v, n=20):
496+
"""
497+
Return a string representation for of a list where list is elided if
498+
it has more than n elements
499+
500+
Parameters
501+
----------
502+
v : list
503+
Input list
504+
n :
505+
Maximum number of elements to display
506+
507+
Returns
508+
-------
509+
str
510+
"""
511+
if len(v) <= n:
512+
return str(v)
513+
else:
514+
disp_v = v[:n // 2 - 1] + ['...'] + v[-n // 2 + 1:]
515+
return '[' + ', '.join([str(e) for e in disp_v]) + ']'

0 commit comments

Comments
 (0)