Skip to content

Commit 558f603

Browse files
committed
feat: add logical comparisons and __or__ concatenation
1 parent b731672 commit 558f603

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

MirrorDict/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,37 @@ def __delitem__(self, key):
482482
else:
483483
raise KeyError(f'del MirrorDict[key] does not have key="{key}".')
484484

485+
def __ior__(self, other): # dict concat with assignment, a |= b
486+
return self.update(other)
487+
488+
def __or__(self, other): # dict concat, a | b
489+
return MirrorDict(self, other)
490+
491+
def __ror__(self, other): # reverse dict concat, b | a
492+
return MirrorDict(other, self)
493+
494+
def __eq__(self, other): # compare self == other.
495+
if isinstance(other, MirrorDict):
496+
return self._key == other._key
497+
return self._key == other
498+
499+
def __ne__(self, other): # compare self != other.
500+
if isinstance(other, MirrorDict):
501+
return self._key != other._key
502+
return self._key != other
503+
504+
def __lt__(self, other): # compare self < other.
505+
raise TypeError(f"'<' not supported between instances of 'MirrorDict' and '{type(other)}'")
506+
507+
def __le__(self, other): # compare self <= other.
508+
raise TypeError(f"'<=' not supported between instances of 'MirrorDict' and '{type(other)}'")
509+
510+
def __gt__(self, other): # compare self > other.
511+
raise TypeError(f"'>' not supported between instances of 'MirrorDict' and '{type(other)}'")
512+
513+
def __ge__(self, other): # compare self >= other.
514+
raise TypeError(f"'>=' not supported between instances of 'MirrorDict' and '{type(other)}'")
515+
485516

486517
# %% -----------------------------------------------------------------------------------------------
487518

0 commit comments

Comments
 (0)