summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
inline | side by side (from parent 1:
9e208e0)
Unclutter the construction of the annotation text for measurements.
Prefer one code block over multiple scatterred lines. Only do string
formatting when an annotation gets emitted. Prefer .format() for its
better control over generated text. Fixup remaining Python idioms in
the unit conversion block while we are here.
self.put(ss, es, self.out_ann, [cls, data])
def decode(self):
self.put(ss, es, self.out_ann, [cls, data])
def decode(self):
- last_measurement = None
timeout_ms = self.options['timeout_ms']
want_unit = self.options['unit']
show_all = self.options['changes'] == 'no'
timeout_ms = self.options['timeout_ms']
want_unit = self.options['unit']
show_all = self.options['changes'] == 'no'
if negative:
number = -number
if is_inch:
if negative:
number = -number
if is_inch:
if want_unit == 'mm':
number *= mm_per_inch
is_inch = False
else:
if want_unit == 'mm':
number *= mm_per_inch
is_inch = False
else:
if want_unit == 'inch':
number = round(number / mm_per_inch, 4)
is_inch = True
if want_unit == 'inch':
number = round(number / mm_per_inch, 4)
is_inch = True
+ unit = 'in' if is_inch else 'mm'
- units = "in" if is_inch else "mm"
-
- measurement = (str(number) + units)
-
- if show_all or measurement != last_measurement:
+ # Construct and emit an annotation.
+ if show_all or (number, unit) != last_sent:
self.putg(self.ss, self.es, 0, [
self.putg(self.ss, self.es, 0, [
- measurement,
- str(number),
+ '{number}{unit}'.format(**locals()),
+ '{number}'.format(**locals()),
- last_measurement = measurement
+ last_sent = (number, unit)
- # Prepare for next packet.
+ # Reset internal state for the start of the next packet.