Skip to content

Commit

Permalink
Small refactor to make _isdupitem more DRY.
Browse files Browse the repository at this point in the history
  • Loading branch information
jab committed Oct 30, 2019
1 parent 46beafb commit ad75f8d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
14 changes: 9 additions & 5 deletions bidict/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,20 @@ def _dedup_item(self, key, val, on_dup):
# else neither isdupkey nor isdupval.
return dedup_result

@staticmethod
def _isdupitem(key, val, dedup_result):
isdupkey, isdupval, oldkey, oldval = dedup_result
isdupitem = oldkey == key
assert isdupitem == (oldval == val), '%r %r %r' % (key, val, dedup_result)
def _isdupitem(self, key, val, dedup_result):
"""Return whether (key, val) duplicates an existing item."""
isdupkey, isdupval, invbyval, fwdbykey = dedup_result
isdupitem = self._isdupitem_helper(key, val, invbyval, fwdbykey)
if isdupitem:
assert isdupkey
assert isdupval
return isdupitem

def _isdupitem_helper(self, key, val, oldkey, oldval): # pylint: disable=no-self-use
isdupitem = oldkey == key
assert isdupitem == (oldval == val), '%r %r %r %r' % (key, val, oldkey, oldval)
return isdupitem

@classmethod
def _get_on_dup(cls, on_dup=None):
if on_dup is None:
Expand Down
7 changes: 1 addition & 6 deletions bidict/_orderedbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,8 @@ def _pop(self, key):
nodefwd.nxt.prv = nodefwd.prv
return val

def _isdupitem(self, key, val, dedup_result):
"""Return whether (key, val) duplicates an existing item."""
isdupkey, isdupval, nodeinv, nodefwd = dedup_result
def _isdupitem_helper(self, key, val, nodeinv, nodefwd): # pylint: disable=arguments-differ
isdupitem = nodeinv is nodefwd
if isdupitem:
assert isdupkey
assert isdupval
return isdupitem

def _write_item(self, key, val, dedup_result): # pylint: disable=too-many-locals
Expand Down

0 comments on commit ad75f8d

Please sign in to comment.