Skip to content

Instantly share code, notes, and snippets.

@poros
Created October 5, 2015 00:33
Show Gist options
  • Save poros/983caf3c8344436dc56b to your computer and use it in GitHub Desktop.
Save poros/983caf3c8344436dc56b to your computer and use it in GitHub Desktop.
Make something iterable
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