From 5fbbd7369b688dd1d54f4ffa892f411395da8e2a Mon Sep 17 00:00:00 2001 From: Stephen L Date: Mon, 18 Jul 2016 02:00:05 +0200 Subject: [PATCH 1/2] Fix tqdm.write() when not yet instanciated Signed-off-by: Stephen L. --- tqdm/_tqdm.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tqdm/_tqdm.py b/tqdm/_tqdm.py index d36fd8e7..698cc8cd 100644 --- a/tqdm/_tqdm.py +++ b/tqdm/_tqdm.py @@ -308,14 +308,15 @@ class tqdm(object): # Clear all bars inst_cleared = [] - for inst in cls._instances: - # Clear instance if in the target output file - # or if write output + tqdm output are both either - # sys.stdout or sys.stderr (because both are mixed in terminal) - if inst.fp == fp or all(f in (sys.stdout, sys.stderr) - for f in (fp, inst.fp)): - inst.clear() - inst_cleared.append(inst) + if hasattr(cls, '_instances'): + for inst in cls._instances: + # Clear instance if in the target output file + # or if write output + tqdm output are both either + # sys.stdout or sys.stderr (because both are mixed in terminal) + if inst.fp == fp or all(f in (sys.stdout, sys.stderr) + for f in (fp, inst.fp)): + inst.clear() + inst_cleared.append(inst) # Write the message fp.write(s) fp.write(end) From 1fd2234a0ba051d2502e336470fb31b6740bb68e Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Mon, 18 Jul 2016 21:36:15 +0100 Subject: [PATCH 2/2] cleaner solution --- tqdm/_tqdm.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/tqdm/_tqdm.py b/tqdm/_tqdm.py index 698cc8cd..14b07b1c 100644 --- a/tqdm/_tqdm.py +++ b/tqdm/_tqdm.py @@ -308,15 +308,14 @@ class tqdm(object): # Clear all bars inst_cleared = [] - if hasattr(cls, '_instances'): - for inst in cls._instances: - # Clear instance if in the target output file - # or if write output + tqdm output are both either - # sys.stdout or sys.stderr (because both are mixed in terminal) - if inst.fp == fp or all(f in (sys.stdout, sys.stderr) - for f in (fp, inst.fp)): - inst.clear() - inst_cleared.append(inst) + for inst in getattr(cls, '_instances', []): + # Clear instance if in the target output file + # or if write output + tqdm output are both either + # sys.stdout or sys.stderr (because both are mixed in terminal) + if inst.fp == fp or all(f in (sys.stdout, sys.stderr) + for f in (fp, inst.fp)): + inst.clear() + inst_cleared.append(inst) # Write the message fp.write(s) fp.write(end)