Update Lesson 4 Task 1 to have better code and collect more data

This commit is contained in:
Stanislav Mykhailenko 2023-01-23 22:41:53 +02:00
parent 8f7b7c9ff1
commit c7c250333b
GPG key ID: 1E95E66A9C9D6A36

View file

@ -5,34 +5,42 @@
# Return codes:
# 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
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":
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.")
continue
number = ord(selection) - ord("a")
data[number] = not data[number]
anythingTrue = False
if data[0] or data[1] or data[2] or data[3]:
anythingTrue = True
file_out = open("data.txt", "w")
if anythingTrue:
file_out = open("data.txt", "w")
for i in range(len(data)):
if data[i]:
writeData(file_out, names[i], code[i])
if data[0]:
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()
file_out.close()