tidy & abstract comparison logic

This commit is contained in:
Casper da Costa-Luis 2018-04-29 20:48:03 +00:00
parent 2e829ddeb6
commit 01af3ac032
No known key found for this signature in database
GPG Key ID: FE7655C3733E352C
2 changed files with 27 additions and 19 deletions

View File

@ -12,7 +12,8 @@ from __future__ import absolute_import
from __future__ import division
# compatibility functions and utilities
from ._utils import _supports_unicode, _environ_cols_wrapper, _range, _unich, \
_term_move_up, _unicode, WeakSet, _basestring, _OrderedDict
_term_move_up, _unicode, WeakSet, _basestring, _OrderedDict, \
Comparable
from ._monitor import TMonitor
# native libraries
import sys
@ -108,7 +109,7 @@ class TqdmDefaultWriteLock(object):
self.release()
class tqdm(object):
class tqdm(Comparable):
"""
Decorate an iterable object, returning an iterator which acts exactly
like the original iterable, but prints a dynamically updating
@ -886,23 +887,9 @@ class tqdm(object):
self.unit_scale, 1 / self.avg_time if self.avg_time else None,
self.bar_format, self.postfix, self.unit_divisor)
def __lt__(self, other):
return abs(self.pos) < abs(other.pos)
def __le__(self, other):
return (self < other) or (self == other)
def __eq__(self, other):
return abs(self.pos) == abs(other.pos)
def __ne__(self, other):
return not (self == other)
def __gt__(self, other):
return not (self <= other)
def __ge__(self, other):
return not (self < other)
@property
def _comparable(self):
return abs(getattr(self, "pos", 1 << 31))
def __hash__(self):
return id(self)

View File

@ -118,6 +118,27 @@ if True: # pragma: no cover
return d
class Comparable(object):
"""Assumes child has self._comparable attr/@property"""
def __lt__(self, other):
return self._comparable < other._comparable
def __le__(self, other):
return (self < other) or (self == other)
def __eq__(self, other):
return self._comparable == other._comparable
def __ne__(self, other):
return not (self == other)
def __gt__(self, other):
return not (self <= other)
def __ge__(self, other):
return not (self < other)
def _is_utf(encoding):
try:
u'\u2588\u2589'.encode(encoding)