Skip to content

Commit

Permalink
Fix Python3 errors, optimize IPSet.discard()
Browse files Browse the repository at this point in the history
  • Loading branch information
autocracy committed Mar 27, 2013
1 parent 4ae6751 commit 7a99c88
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
13 changes: 6 additions & 7 deletions IPy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ def __iter__(self):
yield prefix

def __len__(self):
return reduce(lambda total, prefix: total+len(prefix), self.prefixes, 0)
return sum(len(prefix) for prefix in self.prefixes)

def __add__(self, other):
return IPSet(self.prefixes + other.prefixes)
Expand Down Expand Up @@ -1090,15 +1090,14 @@ def discard(self, value):

# First check if this prefix contains anything in our list
found = False
d = 0
for i in range(len(self.prefixes)):
if self.prefixes[i] in del_prefix:
self.prefixes[i] = None
if self.prefixes[i - d] in del_prefix:
self.prefixes.pop(i - d)
d = d + 1
found = True

if found:
# Filter None values
self.prefixes = filter(lambda a: a is not None, self.prefixes)

# If the prefix was bigger than an existing prefix, then it's
# certainly not a subset of one, so skip the rest
continue
Expand Down Expand Up @@ -1136,7 +1135,7 @@ def optimize(self):
while run_again:
# Filter None values. This happens when a subset is eliminated
# above, or when two prefixes are merged below
self.prefixes = filter(lambda a: a is not None, self.prefixes)
self.prefixes = [a for a in self.prefixes if a is not None]

# We'll set run_again to True when we make changes that require
# re-evaluation of the whole list
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
PYTHON=python

tests:
@echo "[ run unit tests ]"
PYTHONPATH=$(PWD) $(PYTHON) test/test_IPy.py || exit $$?
@echo "[ run unit tests in python 2 ]"
PYTHONPATH=$(PWD) $(PYTHON)2 test/test_IPy.py || exit $$?
@echo "[ run unit tests in python 3 ]"
PYTHONPATH=$(PWD) $(PYTHON)3 test/test_IPy.py || exit $$?
@echo
@echo "[ test README ]"
$(PYTHON) test_doc.py || exit $$?
Expand Down

0 comments on commit 7a99c88

Please sign in to comment.