patternpythonModerate
PyDOS shell simulation
Viewed 0 times
pydossimulationshell
Problem
I have a big project called 'PyDOS', written in Python 3.4, and it's going very well. Post your answers of anything that could make it look cleaner.
```
import time
import os
import sys
import random
import operator
def textviewer():
os.system('cls' if os.name == 'nt' else 'clear')
print ("Text Viewer.")
file_name = input("Enter a text file to view: ")
file = open(file_name, "r")
print ("Loading text...")
time.sleep(4)
os.system('cls' if os.name == 'nt' else 'clear')
print(file.read())
input("Press enter to close")
def edit():
os.system('cls' if os.name == 'nt' else 'clear')
print ("EDIT")
print ("-------------")
print ("Note: Naming this current document the same as a different document will replace the other document with this one.")
filename = input("Plese enter a file name.")
file = open(filename, "w")
print ("FILE: " +filename+".")
line1 = input()
line2 = input()
line3 = input()
line4 = input()
line5 = input()
file.write(line1)
file.write("\n")
file.write(line2)
file.write("\n")
file.write(line3)
file.write("\n")
file.write(line4)
file.write("\n")
file.write(line5)
file.close()
print ("File successfully saved as: " +filename+"!")
time.sleep(3)
while True:
os.system('cls' if os.name == 'nt' else 'clear')
print ()
print ("PyDOS VERSION 1.5")
shell = input("> ")
if shell == "textviewer":
print ("Loading Text Viewer...")
time.sleep(3)
textviewer()
elif shell == "edit":
print ("Loading edit...")
time.sleep(3)
edit()
elif shell == "dir":
print ("The drive name is A:")
print ()
print ("NAME: TYPE: MODIFIED:")
print ("SHUTDOWN.EXE .EXE 12/01/15 ")
print ("EDIT.EXE .EXE 12/01/15 ")
print ("TEXTVIEWER.EXE .EXE 12/01/15 ")
elif shell == "cls":
os.system('cls' if os.name == 'nt' else 'clear')
elif s
```
import time
import os
import sys
import random
import operator
def textviewer():
os.system('cls' if os.name == 'nt' else 'clear')
print ("Text Viewer.")
file_name = input("Enter a text file to view: ")
file = open(file_name, "r")
print ("Loading text...")
time.sleep(4)
os.system('cls' if os.name == 'nt' else 'clear')
print(file.read())
input("Press enter to close")
def edit():
os.system('cls' if os.name == 'nt' else 'clear')
print ("EDIT")
print ("-------------")
print ("Note: Naming this current document the same as a different document will replace the other document with this one.")
filename = input("Plese enter a file name.")
file = open(filename, "w")
print ("FILE: " +filename+".")
line1 = input()
line2 = input()
line3 = input()
line4 = input()
line5 = input()
file.write(line1)
file.write("\n")
file.write(line2)
file.write("\n")
file.write(line3)
file.write("\n")
file.write(line4)
file.write("\n")
file.write(line5)
file.close()
print ("File successfully saved as: " +filename+"!")
time.sleep(3)
while True:
os.system('cls' if os.name == 'nt' else 'clear')
print ()
print ("PyDOS VERSION 1.5")
shell = input("> ")
if shell == "textviewer":
print ("Loading Text Viewer...")
time.sleep(3)
textviewer()
elif shell == "edit":
print ("Loading edit...")
time.sleep(3)
edit()
elif shell == "dir":
print ("The drive name is A:")
print ()
print ("NAME: TYPE: MODIFIED:")
print ("SHUTDOWN.EXE .EXE 12/01/15 ")
print ("EDIT.EXE .EXE 12/01/15 ")
print ("TEXTVIEWER.EXE .EXE 12/01/15 ")
elif shell == "cls":
os.system('cls' if os.name == 'nt' else 'clear')
elif s
Solution
5 lines ought to be enough for everyone
The following has no meaning. Why 5?
Instead let the user end:
Now you can write:
Programming with
So you just started programming and think "Oh the if statements are so good they feel so CS" (I felt that way too when I started programming when I was 15 so I understand you).
BUT so many if and elif are repetitive and you should not repeat yourself.
I would suggest the high level dictionary to accomplish the task more shortly.
Be
It is common and good practice to show some help/info if the user types in help.
Who sleeps too much does not catch fish annoys the user
Why do you sleep up to 4 seconds?
Ok, this emulates an ancient terminal but really, you don't want to make the user wait so much without reason. I suggest
The following has no meaning. Why 5?
line1 = input()
line2 = input()
line3 = input()
line4 = input()
line5 = input()Instead let the user end:
def get_lines():
print("Enter 'END' to end.")
lines = []
line = input()
while line != 'END':
lines.append(line)
line = input()
return linesNow you can write:
lines = get_lines()
file.write('\n'.join(lines))Programming with
withwith is an extremely useful idiom because it handles closing files automagically.with open(filename,'r') as f:
f.read()
# The file is closed automatically :)If considered harmful (if overused)So you just started programming and think "Oh the if statements are so good they feel so CS" (I felt that way too when I started programming when I was 15 so I understand you).
BUT so many if and elif are repetitive and you should not repeat yourself.
I would suggest the high level dictionary to accomplish the task more shortly.
command_actions = {
'textviewer' : textviewer,
'edit' : edit,
'dir' : print_dir,
'cls' : clear_screen,
'shutdown' : shutdown,
'help' : give_help
}
if shell in command_actions:
action = command_actions[shell]
action()
else:
error_message()Be
helpfulIt is common and good practice to show some help/info if the user types in help.
Who sleeps too much does not catch fish annoys the user
Why do you sleep up to 4 seconds?
time.sleep(4)Ok, this emulates an ancient terminal but really, you don't want to make the user wait so much without reason. I suggest
time.sleep(0.5)Code Snippets
line1 = input()
line2 = input()
line3 = input()
line4 = input()
line5 = input()def get_lines():
print("Enter 'END' to end.")
lines = []
line = input()
while line != 'END':
lines.append(line)
line = input()
return lineslines = get_lines()
file.write('\n'.join(lines))with open(filename,'r') as f:
f.read()
# The file is closed automatically :)command_actions = {
'textviewer' : textviewer,
'edit' : edit,
'dir' : print_dir,
'cls' : clear_screen,
'shutdown' : shutdown,
'help' : give_help
}
if shell in command_actions:
action = command_actions[shell]
action()
else:
error_message()Context
StackExchange Code Review Q#83790, answer score: 17
Revisions (0)
No revisions yet.