Skip to content

Commit

Permalink
implemented missing abstract methods for Bounded
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfischer2781 committed Jul 29, 2018
1 parent 119c5cb commit 84da593
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions graphi/types/bounded.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,47 @@ def _ensure_bounds(self):
for tail, head in blacklist:
del self._graph[tail:head]

def __getitem__(self, item):
return self._graph[item]

def __setitem__(self, item, value):
# do not add edges exceeding our maximum distance
if isinstance(item, slice) and value > self.value_bound:
return
elif isinstance(value, abc_collection.Mapping):
value = {node: value for node, value in value.items() if value <= self.value_bound}
super(Bounded, self).__setitem__(item, value)

def __delitem__(self, item):
del self._graph[item]

def __iter__(self):
return iter(self._graph)

def __len__(self):
return len(self._graph)

def __bool__(self):
return bool(self._graph)

__nonzero__ = __bool__

def __contains__(self, item):
return item in self._graph

def update(self, other):
if isinstance(other, (abc.Graph, abc_collection.Mapping)):
try:
other_bound = getattr(other, 'value_bound')
except AttributeError:
other = Bounded(other, value_bound=self.value_bound)
else:
try:
if other_bound > self.value_bound:
other = Bounded(other, value_bound=self.value_bound)
except TypeError as err:
raise ValueError('cannot update with bounds %r and %r: %s' % (self.value_bound, other_bound, err))
self._graph.update(other)

def clear(self):
self._graph = type(self._graph)()

0 comments on commit 84da593

Please sign in to comment.