From 9b4db83db034fd4f29323ca94bbac2963c3b3c16 Mon Sep 17 00:00:00 2001 From: brimoor Date: Fri, 20 Dec 2024 06:58:53 -0500 Subject: [PATCH] avoid unnecessary len() computation --- eta/core/utils.py | 13 +++++++------ setup.py | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/eta/core/utils.py b/eta/core/utils.py index b13c7e42..fa40a464 100644 --- a/eta/core/utils.py +++ b/eta/core/utils.py @@ -1612,7 +1612,7 @@ def __init__( num_pct_decimals = 0 - self._total = self._get_total(total) + self._total = self._get_total(total, quiet) self._iterator = None self._iteration = 0 self._show_percentage = show_percentage @@ -1673,10 +1673,7 @@ def __len__(self): def __call__(self, iterable): if self._total is None: - try: - self._total = len(iterable) - except: - self._total = None + self._total = self._get_total(iterable, self._quiet) if self._total is None: self._show_remaining_time = False @@ -1926,10 +1923,14 @@ def draw(self, force=False): self._draw(force=force) @staticmethod - def _get_total(total): + def _get_total(total, quiet): if is_numeric(total) or total is None: return total + if quiet: + # Don't compute `len()` unnecessarily + return None + try: return len(total) except: diff --git a/setup.py b/setup.py index 3c9bb76b..a9252316 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ from wheel.bdist_wheel import bdist_wheel -VERSION = "0.13.0" +VERSION = "0.13.1" class BdistWheelCustom(bdist_wheel): @@ -140,7 +140,7 @@ def get_version(): "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11" + "Programming Language :: Python :: 3.11", ], entry_points={"console_scripts": ["eta=eta.core.cli:main"]}, python_requires=">=3.9",