Skip to content

Commit

Permalink
Modify spark_print to be min based, not zero based
Browse files Browse the repository at this point in the history
The existing implementation was difficult to use when the range relative
to the integers was small. This change moves the minimum of the spark
line to allow greater resolution in the steps.
  • Loading branch information
tabletcorry committed Nov 30, 2012
1 parent c930568 commit 27d1e4d
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions spark.py
Expand Up @@ -12,17 +12,28 @@
ticks = u'▁▂▃▅▆▇'


def spark_string(ints):
"""Returns a spark string from given iterable of ints."""
step = ((max(ints)) / float(len(ticks) - 1)) or 1
return u''.join(ticks[int(round(i / step))] for i in ints)


def spark_print(ints, stream=None):
def spark_string(ints, fit_min=False):
"""Returns a spark string from given iterable of ints.
Keyword Arguments:
fit_min: Matches the range of the sparkline to the input integers
rather than the default of zero. Useful for large numbers with
relatively small differences between the positions
"""
min_range = 0
if fit_min:
min_range = min(ints)

step_range = max(ints) - min_range
step = ((step_range) / float(len(ticks) - 1)) or 1
return u''.join(ticks[int(round((i - min_range) / step))] for i in ints)


def spark_print(ints, stream=None, fit_min=False):
"""Prints spark to given stream."""
if stream is None:
stream = sys.stdout
stream.write(spark_string(ints).encode('utf-8'))
stream.write(spark_string(ints, fit_min=fit_min).encode('utf-8'))

if __name__ == '__main__':
if len(sys.argv) > 1:
Expand Down

0 comments on commit 27d1e4d

Please sign in to comment.