Skip to content

Instantly share code, notes, and snippets.

@mahmoudhossam
Created January 28, 2012 11:36
Show Gist options
  • Save mahmoudhossam/1694026 to your computer and use it in GitHub Desktop.
Save mahmoudhossam/1694026 to your computer and use it in GitHub Desktop.
Writes devices' status to a file.
import sys
files = ['lamp.txt', 'tv.txt', 'computer.txt']
device_number = int(sys.argv[1])
state = int(sys.argv[2])
#checks if the device number given is valid.
def check_devnum():
return 0 < device_number < 3
#checks if state is valid.
def check_state():
return state == 0 or state == 1
#writes state to file.
def write_config(destination, state):
# WARNING: the next line will discard the file's contents.
f = open(destination, 'w')
f.write('state='+str(state))
f.close()
print("Done.")
if check_devnum() and check_state():
file_name = files[device_number]
write_config(file_name, state)
elif not check_state():
print("Invalid state.")
elif not check_devnum():
print("Invalid device number.")
else:
print("Invalid device number and state.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment