I'm now looking for feedback on v2.0 of this program instead.
I'd like some feedback about my code. What bad habits do I have? What advice could help me write more Pythonic?
I'm trying to write a console application and want a tool to display, update and save settings in a dictionary.
def file_as_string(file_name):
with open(file_name, 'r') as opened_file:
string = opened_file.read().replace('\n', '')
return string
def csv_as_dict(file_name):
file_name = file_name + '.csv'
import csv
dictionary = {}
for key, value in csv.reader(open(file_name)):
dictionary[key] = value
return dictionary
def print_formatted_dict(dictionary, key_title , value_title):
print "{:^30} {:^15}".format('%s', '%s') % (key_title, value_title)
for setting, value in dictionary.items():
print "{:<30} {:<15}".format(setting, value)
def generic_prompt(prompt):
value = raw_input(prompt)
return value
def prompt_for_item(prompt, item):
prompt = prompt + ' ' + item + ': '
value = raw_input(prompt)
return value
def change_dict_value(dictionary, prompt, item):
value = prompt_for_item(prompt, item)
dictionary[item] = (value)
return dictionary
def csv_save_dict(dictionary, file_name):
file_name = file_name + '.csv'
import csv
csv_file = csv.writer(open(file_name, "w"))
for key, value in dictionary.items():
csv_file.writerow([key, value])
def change_dict(dictionary):
specify_question = 'Specify'
change_question = 'Change settings? '
setting_question = 'Setting to change: '
save_question = 'Save settings? '
change_answer = generic_prompt(change_question)
while change_answer not in yes_no:
change_answer = generic_prompt(change_question)
while change_answer == 'yes':
change_item = generic_prompt(setting_question)
while change_item not in dictionary:
change_item = generic_prompt(setting_question)
dictionary = change_dict_value(dictionary, specify_question, change_item)
print_formatted_dict(dictionary, key_title, value_title)
change_answer = generic_prompt(change_question)
while change_answer not in yes_no:
change_answer = generic_prompt(change_question)
if change_answer == 'no':
save_answer = generic_prompt(save_question)
while save_answer not in yes_no:
save_answer = generic_prompt(save_question)
if save_answer == 'yes':
filename_answer = generic_prompt(filename_question)
csv_save_dict(dictionary, filename_answer)
return dictionary
def load_dict(dictionary):
load_answer = generic_prompt(load_question)
while load_answer not in yes_no:
load_answer = generic_prompt(load_question)
if load_answer == 'yes':
filename_answer = generic_prompt(filename_question)
dictionary = csv_as_dict(filename_answer)
print_formatted_dict(dictionary, key_title, value_title)
if load_answer == 'no':
pass
return (dictionary, load_answer)
def load_change_loop(dictionary, load_answer):
while load_answer == 'yes':
dictionary = change_dict(dictionary)
dictionary, load_answer = load_dict(dictionary)
return dictionary
def main():
default_dict = csv_as_dict(default_settings)
print_formatted_dict(default_dict, key_title, value_title)
dictionary = load_change_loop(default_dict, load_answer)
return dictionary
#string variables
default_settings = 'default_settings'
load_question = 'Load a dictionary? '
filename_question = 'File name: '
key_title = 'Setting'
value_title = 'Value'
load_answer = 'yes'
#list variables
yes_no = ['yes', 'no']
main()
The console with highlighted user input looks like this:
The program begins by reading a default_settings.csv file, mine looks like this: