-
-
Save dgraham/e9fc82eae48a57e207a6897430bef3b3 to your computer and use it in GitHub Desktop.
Lab 12—Code review
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
#!/usr/bin/python | |
# Snippet 1 created for CSCI 3308 Lab 10 by: kaoudis@colorado.edu | |
# A simple state-change-powered directory event logger. | |
# for initial run: | |
# $ chmod +x snippet1.py && ./snippet1.py | |
# As JSON files are added to the input directory (or wherever path points) output will change. | |
# It is assumed all inputs are proper JSON. If this is not the case, the code observing inputs should be changed. | |
from ast import literal_eval | |
from sys import exit | |
from os import listdir | |
import time | |
class o: | |
i = 1; b = {} | |
def __init__(self): | |
self.p = "/home/user/path/to/input" | |
self.b = dict([(j, None) for j in listdir(self.p)]) | |
def cp(self, new_p): | |
self.p = new_p | |
def ci(self, new_i): | |
self.i = new_i | |
def r(self): | |
av = 0; d = 0; a = 0; i = 0 | |
sc = dict([(j, time.time()) for j in listdir(self.p)]) | |
new = [j for j in sc if not j in self.b] | |
if new: | |
st = time.time() | |
f = open(self.path+'/'+new[0], 'r') | |
sh = literal_eval(f.read()) | |
f.close() | |
self.b[new[0]] = st | |
if sh['Type'] == 'alarm': | |
a = a + 1 | |
if sh['Type'] == 'Door': | |
d = d + 1 | |
if sh['Type'] == 'img': | |
i = i + 1 | |
fin = time.time() | |
av = fin - st | |
#print("DCnt: "+str(d)+" ICnt: "+str(i)+" ACnt: "+str(a)+" AvgPT: "+str( round(av, 5) ) ) | |
time.sleep(self.i) | |
def s(self): | |
exit(0) | |
if __name__ == '__main__': | |
w = o() | |
try: | |
while True: | |
w.r() | |
except KeyboardInterrupt: | |
w.s() | |
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
#!/usr/bin/python | |
# Snippet 2 created for CSCI 3308 Lab 10 by: kaoudis@colorado.edu | |
# A simple state-change-powered directory event logger. | |
# for initial run: | |
# $ chmod +x snippet2.py && ./snippet2.py | |
# As JSON files are added to the input directory (or wherever path points) output will change. | |
# It is assumed all inputs are proper JSON. If this is not the case, the code observing inputs should be changed. | |
from ast import literal_eval | |
from sys import exit | |
from os import listdir | |
import time | |
class observer: | |
interval = 1 | |
before = {} | |
def __init__(self): | |
# where should our path variable lead to? todo: figure it out | |
self.before = dict([(json, None) for json in listdir(self.path)]) | |
print("Watching "+self.path+" for JSON alerts...") | |
def run(self): | |
avg = 0; door = 0; alarm = 0; img = 0 | |
#check for new things in the directory <path> | |
state_change = dict([(json, time.time()) for json in listdir(self.path) if json]) | |
new = [json for json in state_change if not json in self.before or state_change] | |
if new: | |
f = open(self.path+'/'+new[0], 'r') | |
showme = literal_eval(f.read()) | |
self.before[new[0]] = start # new state is now current state | |
start = time.time() # what am I timing? where should this go? | |
#check alarm type | |
if showme['Type'] == 'alarm': | |
alarm = alarm + 1 | |
if showme['Type'] == 'Door': | |
door = door + 1 | |
if showme['Type'] == 'img': | |
img = img + 1 | |
finish = time.time() | |
avg = finish - start | |
print("DoorCnt: "+str(door)+" ImgCnt: "+str(img)+" AlarmCnt: "+str(alarm)+" AvgProcessingTime: "+str( round(avg, 5) ) ) | |
#time.sleep(self.interval) todo: look up the sleep function? | |
## the below runs this stuff, I know there's a way to do this nicely but I forgot ## | |
watch = observer() | |
while True: | |
watch.run() | |
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
#!/usr/bin/python | |
# Snippet 3 created for CSCI 3308 Lab 10 by: kaoudis@colorado.edu | |
# A simple state-change-powered directory event logger. | |
# for initial run: | |
# $ chmod +x snippet3.py && ./snippet3.py | |
# As JSON files are added to the input directory (or wherever path points) output will change. | |
# It is assumed all inputs are proper JSON. If this is not the case, the code observing inputs should be changed. | |
from ast import literal_eval | |
from sys import exit | |
from os import listdir | |
import time | |
class observer: | |
interval = 1 #sleep 1 second between checks | |
before = {} | |
def __init__(self): | |
self.path = "/home/kelly/Development/Git/coding_exercise/input" | |
self.before = dict([(json, None) for json in listdir(self.path)]) | |
print("Watching "+self.path+" for JSON alerts...") | |
def change_path(self, new_path): | |
self.path = new_path | |
def change_interval(self, new_interval): | |
self.interval = new_interval | |
def run(self): | |
avg = 0; door = 0; alarm = 0; img = 0 | |
#check for new things in the directory <path> | |
state_change = dict([(json, time.time()) for json in listdir(self.path)]) | |
new = [json for json in state_change if not json in self.before] | |
if new: | |
start = time.time() | |
f = open(self.path+'/'+new[0], 'r') | |
showme = literal_eval(f.read()) | |
f.close() | |
self.before[new[0]] = start # new state is now current state | |
#check alarm type | |
if showme['Type'] == 'alarm': | |
alarm = alarm + 1 | |
if showme['Type'] == 'Door': | |
door = door + 1 | |
if showme['Type'] == 'img': | |
img = img + 1 | |
finish = time.time() | |
#according to the directions avg should be for the time interval (one second) not for total processing | |
avg = finish - start | |
print("DoorCnt: "+str(door)+" ImgCnt: "+str(img)+" AlarmCnt: "+str(alarm)+" AvgProcessingTime: "+str( round(avg, 5) ) ) | |
time.sleep(self.interval) | |
def stop(self): | |
print(" ") | |
print("Finished watching for alerts!") | |
exit(0) | |
if __name__ == '__main__': | |
watch = observer() | |
try: | |
while True: | |
watch.run() | |
except KeyboardInterrupt: | |
watch.stop() | |
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
#!/usr/bin/python | |
# Snippet 4 created for CSCI 3308 Lab 10 by: kaoudis@colorado.edu | |
# A simple state-change-powered directory event logger. | |
# for initial run: | |
# $ chmod +x snippet4.py && ./snippet4.py | |
# As JSON files are added to the input directory (or wherever path points) output will change. | |
# It is assumed all inputs are proper JSON. If this is not the case, the code observing inputs should be changed. | |
from ast import literal_eval | |
from sys import exit | |
from subprocess import check_output | |
from os import listdir | |
import time | |
class observer: | |
interval = 1 #sleep 1 second between checks | |
before = {} | |
def __init__(self): | |
self.path = input("Enter the name of the directory you'd like to watch: ") | |
self.before = dict([(json, None) for json in check_output(["ls", "-lFa", self.path], shell=True) ]) | |
print("Watching "+self.path+" for JSON alerts...") | |
def change_path(self, new_path): | |
self.path = new_path | |
def change_interval(self, new_interval): | |
self.interval = new_interval | |
def run(self): | |
avg = 0; door = 0; alarm = 0; img = 0 | |
#check for new things in the directory <path> | |
state_change = dict([(json, time.time()) for json in listdir(self.path)]) | |
new = [json for json in state_change if not json in self.before] | |
if new: | |
start = time.time() | |
f = open(self.path+'/'+new[0], 'r') | |
showme = literal_eval(f.read()) | |
f.close() | |
self.before[new[0]] = start # new state is now current state | |
#check alarm type | |
if showme['Type'] == 'alarm': | |
alarm = alarm + 1 | |
if showme['Type'] == 'Door': | |
door = door + 1 | |
if showme['Type'] == 'img': | |
img = img + 1 | |
finish = time.time() | |
avg = finish - start | |
print("DoorCnt: "+str(door)+" ImgCnt: "+str(img)+" AlarmCnt: "+str(alarm)+" AvgProcessingTime: "+str( round(avg, 5) ) ) | |
time.sleep(self.interval) | |
def stop(self): | |
print(" ") | |
print("Finished watching for alerts!") | |
exit(0) | |
if __name__ == '__main__': | |
watch = observer() | |
try: | |
while True: | |
watch.run() | |
except KeyboardInterrupt: | |
watch.stop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment