Last active
January 27, 2017 19:10
-
-
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
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
'''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