I see some long if-else blocks in the code. It would make sense to replace these with dictionary look-ups.
# OLD/CURRENT CODE:
def update_fpointer(self, nid, mode=ADD, replace=None):
if mode is self.ADD:
pass
elif mode is self.DELETE:
pass
elif mode is self.INSERT:
pass
elif mode is self.REPLACE:
pass
# IDEA FOR NEW CODE
class node:
#: Mode constants for routine `update_fpointer()`.
(ADD, DELETE, INSERT, REPLACE) = list(range(4))
fpointer_lookup_table = {ADD: "append", DELETE: "remove", INSERT: "append", REPLACE: "replace"}
def update_fpointer(self, nid, mode=ADD, replace=None):
f_name = self.fpointer_lookup_table[mode]
f = gettattr(self, f_name)
args = update_fpointer_arg_select(nid, mode, replace)
return f(*args)
def replace(self, nid, replacement)
if replace is None:
raise NodePropertyError(
'Argument "replace" should be provided when mode is {}'.format(mode)
)
ind = self._fpointer.index(nid)
self._fpointer[ind] = replace
A second example:
# OLD CODE
@fpointer.setter
def fpointer(self, value):
"""Set the value of `_fpointer`."""
if value is None:
self._fpointer = list()
elif isinstance(value, list):
self._fpointer = value
elif isinstance(value, dict):
self._fpointer = list(value.keys())
elif isinstance(value, set):
self._fpointer = list(value)
else: # TODO: add deprecated routine
pass
# IDEA FOR NEW CODE
class node:
# fpointer_setter_lookup_table = {"NoneType":lambda x: list(), "list":lambda x : x, etc....}
@fpointer.setter
def fpointer(self, value):
"""Set the value of `_fpointer`."""
imp = fpointer_setter_lookup_table[value.__class__.__name__]
self._fpointer = imp(value)
I see some long if-else blocks in the code. It would make sense to replace these with dictionary look-ups.
A second example: