Skip to content

Commit

Permalink
iterable comparison
Browse files Browse the repository at this point in the history
- fixes #1288
  • Loading branch information
casperdcl committed Sep 2, 2022
1 parent 05e3d32 commit d2f2366
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions tqdm/utils.py
Expand Up @@ -64,15 +64,44 @@ def __format__(self, _):


class Comparable(object):
"""Assumes child has self._comparable attr/@property"""
"""
Compares `self._comparable` & `other._comparable` (fallback `self.iterable` & `other`)
"""
def __lt__(self, other):
return self._comparable < other._comparable
if hasattr(other, '_comparable'):
return self._comparable < other._comparable
if not isinstance(self.iterable, other.__class__):
raise TypeError(("'<' not supported between instances of"
" {i.__class__.__name__!r} and {j.__class__.__name__!r}").format(
i=self.iterable, j=other))
for i, j in zip(self, other):
if i != j:
return i < j
return len(self) < len(other)

def __le__(self, other):
return (self < other) or (self == other)
if hasattr(other, '_comparable'):
return self._comparable <= other._comparable
if not isinstance(self.iterable, other.__class__):
raise TypeError(("'<=' not supported between instances of"
" {i.__class__.__name__!r} and {j.__class__.__name__!r}").format(
i=self.iterable, j=other))
for i, j in zip(self, other):
if i != j:
return i <= j
return len(self) <= len(other)

def __eq__(self, other):
return self._comparable == other._comparable
if hasattr(other, '_comparable'):
return self._comparable == other._comparable
if not isinstance(self.iterable, other.__class__):
raise TypeError(("'==' not supported between instances of"
" {i.__class__.__name__!r} and {j.__class__.__name__!r}").format(
i=self.iterable, j=other))
for i, j in zip(self, other):
if i != j:
return False
return len(self) == len(other)

def __ne__(self, other):
return not self == other
Expand Down

0 comments on commit d2f2366

Please sign in to comment.