get rid of get_new

- fixes #1084
- fixes #509
This commit is contained in:
Casper da Costa-Luis 2020-11-27 14:24:05 +00:00
parent bded8fa87d
commit 9224b7b7ce
No known key found for this signature in database
GPG Key ID: 986B408043AE090D
6 changed files with 5 additions and 79 deletions

View File

@ -919,9 +919,7 @@ For further customisation,
(e.g. GUIs such as notebook or plotting packages). In the latter case:
1. ``def __init__()`` to call ``super().__init__(..., gui=True)`` to disable
terminal ``status_printer`` creation. Otherwise (if terminal is required),
``def __new__()`` to call ``cls.get_new()`` (see below) to ensure correct
nested positioning.
terminal ``status_printer`` creation.
2. Redefine: ``close()``, ``clear()``, ``display()``.
Consider overloading ``display()`` to use e.g.
@ -935,23 +933,6 @@ above recommendation:
- `tqdm/contrib/telegram.py <https://github.com/tqdm/tqdm/blob/master/tqdm/contrib/telegram.py>`__
- `tqdm/contrib/discord.py <https://github.com/tqdm/tqdm/blob/master/tqdm/contrib/discord.py>`__
Note that multiple different ``tqdm`` subclasses which all write to the terminal
(``gui=False``) can cause positioning issues when used simultaneously (in nested
mode). To fix this, custom subclasses which expect to write to the terminal
should define a ``__new__()`` method as follows:
.. code:: python
from tqdm import tqdm as std_tqdm
class TqdmExt(std_tqdm):
def __new__(cls, *args, **kwargs):
return cls.get_new(super(TqdmExt, cls), std_tqdm, *args, **kwargs)
This approach is used ``tqdm.asyncio`` and ``tqdm.contrib.telegram/discord``.
However it is not necessary for ``tqdm.notebook/gui`` since they don't use the
terminal.
Dynamic Monitor/Meter
~~~~~~~~~~~~~~~~~~~~~

View File

@ -1136,9 +1136,7 @@ For further customisation,
(e.g. GUIs such as notebook or plotting packages). In the latter case:
1. ``def __init__()`` to call ``super().__init__(..., gui=True)`` to disable
terminal ``status_printer`` creation. Otherwise (if terminal is required),
``def __new__()`` to call ``cls.get_new()`` (see below) to ensure correct
nested positioning.
terminal ``status_printer`` creation.
2. Redefine: ``close()``, ``clear()``, ``display()``.
Consider overloading ``display()`` to use e.g.
@ -1152,23 +1150,6 @@ above recommendation:
- `tqdm/contrib/telegram.py <https://github.com/tqdm/tqdm/blob/master/tqdm/contrib/telegram.py>`__
- `tqdm/contrib/discord.py <https://github.com/tqdm/tqdm/blob/master/tqdm/contrib/discord.py>`__
Note that multiple different ``tqdm`` subclasses which all write to the terminal
(``gui=False``) can cause positioning issues when used simultaneously (in nested
mode). To fix this, custom subclasses which expect to write to the terminal
should define a ``__new__()`` method as follows:
.. code:: python
from tqdm import tqdm as std_tqdm
class TqdmExt(std_tqdm):
def __new__(cls, *args, **kwargs):
return cls.get_new(super(TqdmExt, cls), std_tqdm, *args, **kwargs)
This approach is used ``tqdm.asyncio`` and ``tqdm.contrib.telegram/discord``.
However it is not necessary for ``tqdm.notebook/gui`` since they don't use the
terminal.
Dynamic Monitor/Meter
~~~~~~~~~~~~~~~~~~~~~

View File

@ -62,9 +62,6 @@ class tqdm_asyncio(std_tqdm):
yield from cls(asyncio.as_completed(fs, loop=loop, timeout=timeout),
total=total, **tqdm_kwargs)
def __new__(cls, *args, **kwargs):
return cls.get_new(super(tqdm_asyncio, cls), std_tqdm, *args, **kwargs)
def tarange(*args, **kwargs):
"""

View File

@ -102,9 +102,6 @@ class tqdm_discord(tqdm_auto):
fmt['bar_format'] = '{l_bar}{bar:10u}{r_bar}'
self.dio.write(self.format_meter(**fmt))
def __new__(cls, *args, **kwargs):
return cls.get_new(super(tqdm_discord, cls), tqdm_auto, *args, **kwargs)
def tdrange(*args, **kwargs):
"""

View File

@ -106,10 +106,6 @@ class tqdm_telegram(tqdm_auto):
fmt['bar_format'] = '{l_bar}{bar:10u}{r_bar}'
self.tgio.write(self.format_meter(**fmt))
def __new__(cls, *args, **kwargs):
return cls.get_new(
super(tqdm_telegram, cls), tqdm_auto, *args, **kwargs)
def ttgrange(*args, **kwargs):
"""

View File

@ -225,6 +225,7 @@ class tqdm(Comparable):
monitor_interval = 10 # set to 0 to disable the thread
monitor = None
_instances = WeakSet()
@staticmethod
def format_sizeof(num, suffix='', divisor=1000):
@ -554,15 +555,10 @@ class tqdm(Comparable):
n_fmt, unit, elapsed_str, rate_fmt, postfix)
def __new__(cls, *_, **__):
# Create a new instance
instance = object.__new__(cls)
# Construct the lock if it does not exist
with cls.get_lock():
# Add to the list of instances
if not hasattr(cls, '_instances'):
cls._instances = WeakSet()
with cls.get_lock(): # also constructs lock if non-existent
cls._instances.add(instance)
# Create the monitoring thread
# create monitoring thread
if cls.monitor_interval and (cls.monitor is None or not
cls.monitor.report()):
try:
@ -572,28 +568,6 @@ class tqdm(Comparable):
" (monitor_interval = 0) due to:\n" + str(e),
TqdmMonitorWarning, stacklevel=2)
cls.monitor_interval = 0
# Return the instance
return instance
@classmethod
def get_new(cls, super_cls, base_cls, *args, **kwargs):
"""
Workaround for mixed-class same-stream nested progressbars.
See [#509](https://github.com/tqdm/tqdm/issues/509)
"""
with cls.get_lock():
try:
cls._instances = base_cls._instances
except AttributeError:
pass
instance = super_cls.__new__(cls, *args, **kwargs)
with cls.get_lock():
try:
# `base_cls` may have been changed so update
cls._instances.update(base_cls._instances)
except AttributeError:
pass
base_cls._instances = cls._instances
return instance
@classmethod