Skip to content

Instantly share code, notes, and snippets.

@thodnev
Created August 26, 2016 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thodnev/43b238d90a5a8e4b2fe671196d4c79a6 to your computer and use it in GitHub Desktop.
Save thodnev/43b238d90a5a8e4b2fe671196d4c79a6 to your computer and use it in GitHub Desktop.
class Polling:
def __init__(self, routine):
self.routine = routine
def __call__(self, *args, **kwargs):
self.__last = self.routine(*args, **kwargs)
return self.__last
@property
def last(self):
return self.__last
if __name__ == '__main__':
test = ['1st', '2nd', '3rd', '..more..']
from itertools import cycle
cycled = iter(cycle(test))
@Polling
def joy_sim(*args, **kwargs):
print('\tcalled with', args, kwargs, end='')
return next(cycled)
for i in range(10):
res = joy_sim(i)
prev = joy_sim.last
assert prev == res
print(' Result:', res, '\t\tLast:', prev)
# Which gives:
## called with (0,) {} Result: 1st Last: 1st
## called with (1,) {} Result: 2nd Last: 2nd
## called with (2,) {} Result: 3rd Last: 3rd
## called with (3,) {} Result: ..more.. Last: ..more..
## called with (4,) {} Result: 1st Last: 1st
## called with (5,) {} Result: 2nd Last: 2nd
## called with (6,) {} Result: 3rd Last: 3rd
## called with (7,) {} Result: ..more.. Last: ..more..
## called with (8,) {} Result: 1st Last: 1st
## called with (9,) {} Result: 2nd Last: 2nd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment