Skip to content

Instantly share code, notes, and snippets.

@scottx611x
Forked from empiricalthought/spinner.py
Last active June 7, 2016 12:49
Show Gist options
  • Save scottx611x/0dc9e332af63f4dc2213bd0a60809462 to your computer and use it in GitHub Desktop.
Save scottx611x/0dc9e332af63f4dc2213bd0a60809462 to your computer and use it in GitHub Desktop.
Python 2 compatible ASCII spinner for progress feedback
import itertools
import sys
def spinner(granularity):
"""Wraps a function in an ASCII spinner display loop. granularity
represents the number of times the function should be called
before moving the spinner."""
spinner_chars = itertools.cycle("\|/-")
def make_spinner(f):
calls = {'x': 0}
def g(*args, **kwargs):
result = f(*args, **kwargs)
if calls['x'] == 0:
sys.stdout.write("\b" + next(spinner_chars))
sys.stdout.flush()
calls['x'] = (calls['x'] + 1) % granularity
return result
return g
return make_spinner
# import time
# @spinner(10)
# def process_chunk():
# time.sleep(.1)
# sys.stdout.write("running... ")
# while True:
# process_chunk()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment