Modernized python script print statements.

This commit is contained in:
Ed Braaten 2020-10-09 11:26:29 -07:00
parent 787c1866e2
commit eb12c8f9cf

View file

@ -15,13 +15,13 @@ verinfo = subprocess.check_output(["git", "describe", "--abbrev=7",
"--dirty", "--always", "--tags"]) "--dirty", "--always", "--tags"])
# Obligatory Hello World with some version info # Obligatory Hello World with some version info
print "Hello Python World! Version: " +verinfo print("Hello Python World! Version: {}".format(verinfo))
# Show list of command line arguments # Show list of command line arguments
print "Command line args: ", print("Command line args: "),
for i in sys.argv[1:]: for i in sys.argv[1:]:
print " " + i, print(" {}".format(i))
print "" print("")
# Determine number of physical CPU sockets # Determine number of physical CPU sockets
if os.name == 'nt': if os.name == 'nt':
@ -29,52 +29,52 @@ if os.name == 'nt':
else: else:
socketcount = subprocess.check_output(["bash","-c", socketcount = subprocess.check_output(["bash","-c",
"(cat /proc/cpuinfo | grep 'physical id' | sort | uniq | wc -l)"]) "(cat /proc/cpuinfo | grep 'physical id' | sort | uniq | wc -l)"])
print "CPU socket count: ", socketcount print("CPU socket count: {}".format(socketcount))
# print out our animals array in reverse-sorted order # print out our animals array in reverse-sorted order
print "\nValue of animals array:" print("\nValue of animals array:")
for i in sorted(animals, reverse=True): for i in sorted(animals, reverse=True):
print "item = " + i print("item = " + i)
if (i == animals[0]): if (i == animals[0]):
print " Condition is TRUE, \"" +i+ "\" == " + animals[0] print(" Condition is TRUE, \"" +i+ "\" == " + animals[0])
else: else:
print " Condition is FALSE, \"" +i+ "\" != " + animals[0] print(" Condition is FALSE, \"" +i+ "\" != " + animals[0])
# print our fruit_color dictionary (key/value pairs) # print our fruit_color dictionary (key/value pairs)
print "\nValue of fruit_color hash:" print("\nValue of fruit_color hash:")
for fruit,color in sorted(fruit_color.items(), reverse=True): for fruit,color in sorted(fruit_color.items(), reverse=True):
print fruit, " = ", color print(fruit, " = ", color)
# Read a non-existent environment variable and supply default value... # Read a non-existent environment variable and supply default value...
some_env_var = os.getenv("PROBABLY_NOT_THERE", "Default Value") some_env_var = os.getenv("PROBABLY_NOT_THERE", "Default Value")
print "\nPROBABLY_NOT_THERE is set to '"+some_env_var+"'\n" print("\nPROBABLY_NOT_THERE is set to '"+some_env_var+"'\n")
# Let's read in the PATH variable from the OS and print it out... # Let's read in the PATH variable from the OS and print it out...
path_env_var = os.environ['PATH'] path_env_var = os.environ['PATH']
print "OS PATH var is set to: '"+path_env_var+"'\n" print("OS PATH var is set to: '"+path_env_var+"'\n")
# Let's manipulate PATH variable and then restore it # Let's manipulate PATH variable and then restore it
print "Adding :/test to PATH variable" print("Adding :/test to PATH variable")
os.environ['PATH'] = os.environ['PATH'] + ":/test" os.environ['PATH'] = os.environ['PATH'] + ":/test"
print "OS PATH var is set to: '"+os.environ['PATH']+"'\n" print("OS PATH var is set to: '"+os.environ['PATH']+"'\n")
print "Restoring original PATH value." print("Restoring original PATH value.")
os.environ['PATH'] = path_env_var os.environ['PATH'] = path_env_var
print "OS PATH var is set to: '"+path_env_var+"'\n" print("OS PATH var is set to: '"+path_env_var+"'\n")
# Let's create the file test.out and write animals array to it... # Let's create the file test.out and write animals array to it...
print "\nWriting test.out file..." print("\nWriting test.out file...")
file = open("test.out", "w") file = open("test.out", "w")
for i in animals: for i in animals:
file.write(i+"\n") file.write(i+"\n")
file.close() file.close()
# Now open the file we wrote and read it back... # Now open the file we wrote and read it back...
print "\nContents of test.out file:" print("\nContents of test.out file:")
file = open("test.out", "r") file = open("test.out", "r")
linenum = 1 linenum = 1
for line in file: for line in file:
print "line "+str(linenum)+": "+line, print("line "+str(linenum)+": "+line)
linenum += 1 linenum += 1
file.close() file.close()