0

I have a script that prints a float between -1 and 1, for example -0.25.

For visual reference, I'd like to print that on the output as a "gauge":

input:

print(float)

desired output:

-1 =======|==0==========+1

How would you suggest getting this done? Is there a library that can facilitate?

pepe
  • 9,799
  • 25
  • 110
  • 188

1 Answers1

2

One relatively naive approach:

def gauge(x, width=20):
    pos = int((x+1) / 2 * width)
    g = ['='] * width
    m = width // 2
    if pos == m:
        sep = '[0]'
    else:
        g[pos] = '|'
        sep = ' 0 '
    g = ''.join(g)
    return '-1 {}{}{} +1'.format(g[:m], sep, g[m:])

>>> gauge(-0.25)
'-1 =======|== 0 ========== +1'
>>> gauge(-0.25, 50)
'-1 ==================|====== 0 ========================= +1'
>>> gauge(0.33, 50)
'-1 ========================= 0 ========|================ +1'
>>> gauge(0.0, 50)
'-1 =========================[0]========================= +1'
>>> gauge(0.99, 50)
'-1 ========================= 0 ========================| +1'
>>> gauge(-0.99, 50)
'-1 |======================== 0 ========================= +1'
user2390182
  • 72,016
  • 6
  • 67
  • 89