From c87d38a36b9c3d5830d5d5b98c1f6fda1ccaca8b Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 13 Dec 2017 12:48:26 -0800 Subject: [PATCH] Add function `generator_printer` so it can be used outside. This way, people who use _as_a_generator can get the same print statements as the normal version without rewriting it themselves. --- timesearch/livestream.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/timesearch/livestream.py b/timesearch/livestream.py index 5a7307e..c3a931d 100644 --- a/timesearch/livestream.py +++ b/timesearch/livestream.py @@ -6,6 +6,19 @@ from . import common from . import tsdb +def generator_printer(generator): + for step in generator: + newtext = '%ds, %dc' % (step['new_submissions'], step['new_comments']) + totalnew = step['new_submissions'] + step['new_comments'] + status = '{now} +{new}'.format(now=common.human(common.get_now()), new=newtext) + print(status, end='') + if totalnew == 0 and common.log.level != common.logging.DEBUG: + # Since there were no news, allow the next line to overwrite status + print('\r', end='', flush=True) + else: + print() + yield None + def livestream( subreddit=None, username=None, @@ -36,19 +49,11 @@ def livestream( if as_a_generator: return generator + generator = generator_printer(generator) + while True: try: step = next(generator) - newtext = '%ds, %dc' % (step['new_submissions'], step['new_comments']) - totalnew = step['new_submissions'] + step['new_comments'] - status = '{now} +{new}'.format(now=common.human(common.get_now()), new=newtext) - print(status, end='') - if totalnew == 0 and common.log.level != common.logging.DEBUG: - # Since there were no news, allow the next line to overwrite status - print('\r', end='', flush=True) - else: - print() - if only_once: break time.sleep(sleepy) @@ -153,10 +158,6 @@ def livestream_argparse(args): else: limit = int(args.limit) - if args.submissions is False and args.comments is False: - args.submissions = True - args.comments = True - return livestream( subreddit=args.subreddit, username=args.username,