Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Last active January 27, 2017 19:10
Show Gist options
  • Save vlad-bezden/26eda2e4b29c77df1e2ba0a647dc4e91 to your computer and use it in GitHub Desktop.
Save vlad-bezden/26eda2e4b29c77df1e2ba0a647dc4e91 to your computer and use it in GitHub Desktop.
Example of sensor data simulator with timestamp and random data. Provides example on zip, iterable, iterators and timer
'''Example of how to simulate sensor data'''
import datetime
import itertools
import random
import time
class Sensor:
'''Sensor simulator
Simulates sensor with random data.
In a real world data will come from a real
sensor.
'''
def __iter__(self):
return self
def __next__(self):
return random.random()
def main():
'''Main driver of the module'''
sensor = Sensor()
# create endless datetime iterator
timestamp = iter(datetime.datetime.today, None)
def data():
'''Generates combination of timestamp and sensor data
Returns:
tuple of time and sensor data
'''
return zip(timestamp, sensor)
# run forever
# for stamp, value in data():
# run only 10 times
for stamp, value in itertools.islice(data(), 10):
print(stamp.strftime('%A, %b %d, %Y %H:%M:%S'), value)
time.sleep(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment