slowly cleaning things up

cleanup
m455 2018-08-04 10:11:03 -04:00
parent ed25c9d717
commit a16412ee4e
1 changed files with 44 additions and 40 deletions

View File

@ -5,63 +5,67 @@ import os
import sys import sys
import random import random
#create list.txt the_list = "list.txt"
def createlist(): program_name = "nicethings"
thefile = open('/home/m455/code/projects/nicethings/list.txt', 'w+') top_line = "-----------------------------------------\n"
thefile.close() bottom_line = "\n-----------------------------------------"
print("list.txt created, try your command again and it should work :)")
#display random nice message if no arguments are given messages = {'list_created': "{} created, try your command again and it should work :)".format(the_list),
def showrandomline(): 'list_empty': "sorry, there's nothing in {}\nTry adding something to the list by typing:\n{} \"your message here\"".format(the_list, program_name),
thefile = open('/home/m455/code/projects/nicethings/list.txt', 'r') 'args_too_many': "Sorry, I only accept one argument.\nTry using:\n{} \"your message here\"".format(program_name),
filecontents = thefile.read(1) 'input_null': "Sorry, I couldn't find any input.\n\nTry using:\n{} \"your message here\"".format(program_name),
if not filecontents: 'item_added': "You have added the following to the list:\n",
print("sorry, there's nothing in list.txt.\nTry adding something to the list by typing:\nnicethings \"your message here\"") 'try_again': "sorry, I didn't get that, try typing y or n:",
'file_not_found': "{} doesn't exist\ndo you want me to make a {} file right now? (y/n)".format(the_list, the_list)}
def write_to_file(list_file, content):
temp = open(list_file, 'a')
temp.write(content)
temp.close()
def create_file(list_file):
open(list_file, 'a').close()
print(messages['list_created'])
def show_random_line(list_file):
thefile = open(list_file, 'r').read(1)
temp = open(list_file, 'r')
contents = temp.read(1)
if not contents:
print(messages['list_empty'])
else: else:
print(random.choice(list(open('/home/m455/code/projects/nicethings/list.txt', 'r'))).rstrip('\n')) print(random.choice(list(open(the_list, 'r'))).rstrip('\n'))
#too many arguments error def add_to_list():
def toomanyargs(): write_to_file(the_list, sys.argv[1] + "\n")
print("Sorry, I only accept one argument.\nTry using:\nnicethings \"your message here\"") print(top_line + messages['item_added'] + sys.argv[1] + bottom_line)
#no input error def hangle_args():
def noinputerror():
print("Sorry, I couldn't find any input.\n\nTry using:\nnicethings \"your message here\"")
#add user input to file and display to user what was added
def addtofile():
thefile = open('/home/m455/code/projects/nicethings/list.txt', 'a')
thefile.write(sys.argv[1] + "\n")
thefile.close()
print("-----------------------------------------\nYou have added the following to the list:\n\n{}\n-----------------------------------------".format(sys.argv[1]))
#how to handle different user inputs
def handleargs():
if len(sys.argv) <= 1: if len(sys.argv) <= 1:
showrandomline() show_random_line(the_list)
elif len(sys.argv) > 2: elif len(sys.argv) > 2:
toomanyargs() too_many_args()
elif sys.argv[1].isspace() == True or sys.argv[1] == "": elif sys.argv[1].isspace() == True or sys.argv[1] == "":
noinputerror() print(messages['input_null'])
else: else:
addtofile() add_to_list()
def handlechoice(): def prompt_file_creation():
userchoice = input() userchoice = input()
if userchoice == "y": if userchoice == "y":
createlist() create_file(the_list)
elif userchoice == "n": elif userchoice == "n":
print("bye bye") print("bye bye")
else: else:
print("sorry, I didn't get that, type either y or n:") print(messages['try_again'])
handlechoice() prompt_file_creation()
def main(): def main():
if os.path.exists('/home/m455/code/projects/nicethings/list.txt') == True: if os.path.exists(the_list) == True:
handleargs() hangle_args()
else: else:
print("list.txt doesn't exist\ndo you want me to make a list.txt file right now? (y/n)") print(messages['file_not_found'])
handlechoice() prompt_file_creation()
if __name__ == '__main__': if __name__ == '__main__':
main() main()