Created
March 14, 2019 23:40
-
-
Save JohnnyFang/3920f8fdd4f2717f530f2938c8dd4127 to your computer and use it in GitHub Desktop.
iterator for streamed data coming from fictional sensor - Extended iter() format lesson
This file contains 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
import datetime | |
import itertools | |
import random | |
class Sensor: | |
def __iter__(self): | |
return self | |
def __next__(self): | |
return random.random() | |
sensor = Sensor() | |
timestamps = iter(datetime.datetime.now, None) | |
for stam, value in itertools.islice(zip(timestamps, sensor), 10): | |
print(stamp, value) | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment