Created
October 5, 2015 00:33
-
-
Save poros/983caf3c8344436dc56b to your computer and use it in GitHub Desktop.
Make something iterable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RoutingTable(object): | |
def __init__(self, crappy_rt): | |
self.crappy_rt = crappy_rt | |
def __len__(self): | |
return self.crappy_rt.getSize() | |
def __getitem__(self, index): | |
if index >= len(self): | |
raise IndexError | |
return self.crappy_rt.getRouteByIndex(index) | |
# USAGE | |
rt = RoutingTable(crappy_rt) | |
for route in rt: | |
... | |
first_route = rt[0] | |
# INSTEAD OF | |
for rt_offset in range(rt.getSize()): | |
route = rt[rt_offset] | |
... | |
first_route = rt.getRouteByIndex(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment