Update Lesson 4 Task 1 to have better code and collect more data
This commit is contained in:
parent
8f7b7c9ff1
commit
c7c250333b
1 changed files with 27 additions and 19 deletions
|
@ -5,34 +5,42 @@
|
||||||
# Return codes:
|
# Return codes:
|
||||||
# 0 - OK
|
# 0 - OK
|
||||||
|
|
||||||
import platform, psutil
|
import platform, psutil, os, serial.tools.list_ports
|
||||||
|
|
||||||
|
def getValue(checkedValue):
|
||||||
|
if checkedValue:
|
||||||
|
return "yes"
|
||||||
|
else:
|
||||||
|
return "no"
|
||||||
|
|
||||||
|
def printList(names, values):
|
||||||
|
for i in range(len(names)):
|
||||||
|
print(chr(ord("a") + i) + ") " + names[i] + " " + "[" + getValue(values[i]) + "]")
|
||||||
|
print("y) Proceed")
|
||||||
|
|
||||||
|
def writeData(file, name, code):
|
||||||
|
file.write(name + ": " + eval(code) + "\n")
|
||||||
|
|
||||||
|
names = ["CPU", "Architecture", "CPU Family", "RAM", "Operating system", "Disk usage", "Serial ports"]
|
||||||
|
data = [False] * len(names)
|
||||||
|
code = ["platform.processor()", "str(platform.architecture())", "platform.machine()", "str(psutil.virtual_memory())", "os.name", "str(psutil.disk_usage(\".\"))", "str(serial.tools.list_ports.comports())"]
|
||||||
|
|
||||||
data = [False,False,False,False]
|
|
||||||
selection = None
|
selection = None
|
||||||
while True:
|
while True:
|
||||||
selection = input("a) CPU [" + str(data[0]) + "]\nb) RAM [" + str(data[1]) + "]\nc) Architecture [" + str(data[2]) + "]\nd) CPU Family [" + str(data[3]) + "]\ny) Proceed\n")
|
printList(names, data)
|
||||||
|
selection = input()
|
||||||
if selection == "y":
|
if selection == "y":
|
||||||
break
|
break
|
||||||
if not len(selection) == 1 or ord(selection) < ord("a") or ord(selection) > ord("d"):
|
if not len(selection) == 1 or ord(selection) < ord("a") or ord(selection) >= (ord("a") + len(names)):
|
||||||
print("Invalid selection.")
|
print("Invalid selection.")
|
||||||
continue
|
continue
|
||||||
number = ord(selection) - ord("a")
|
number = ord(selection) - ord("a")
|
||||||
data[number] = not data[number]
|
data[number] = not data[number]
|
||||||
|
|
||||||
anythingTrue = False
|
file_out = open("data.txt", "w")
|
||||||
if data[0] or data[1] or data[2] or data[3]:
|
|
||||||
anythingTrue = True
|
|
||||||
|
|
||||||
if anythingTrue:
|
for i in range(len(data)):
|
||||||
file_out = open("data.txt", "w")
|
if data[i]:
|
||||||
|
writeData(file_out, names[i], code[i])
|
||||||
|
|
||||||
if data[0]:
|
file_out.close()
|
||||||
file_out.write("CPU: " + platform.processor() + "\n")
|
|
||||||
if data[1]:
|
|
||||||
file_out.write("RAM: " + str(psutil.virtual_memory()) + "\n")
|
|
||||||
if data[2]:
|
|
||||||
file_out.write("Architecture: " + str(platform.architecture()) + "\n")
|
|
||||||
if data[3]:
|
|
||||||
file_out.write("CPU family: " + platform.machine() + "\n")
|
|
||||||
|
|
||||||
file_out.close()
|
|
||||||
|
|
Reference in a new issue