patternpythonMinor
A program to display, update and save a dictionary as .csv
Viewed 0 times
updatecsvprogramsaveanddictionarydisplay
Problem
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)
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)
Solution
It's a pretty good program for a beginner.
A few points:
Put your
You do this a lot:
Anytime you find yourself repeating code, it'd be helpful to write a function instead. Something like:
Now your code can simply say:
And avoid repeating the loop everytime you ask a question.
Your program doesn't make its structure very clear. Given what your program does, your main function should read something like:
Note that by reading this, you can see the program processes first the default settings, and then as many other settings files as you have inclination to use the program.
A few points:
Put your
import csv once at the top of the file, not in the functions that use csv. Importing is best thought of as an action at the start of your program, not as something that happens while performing an action.You do this a lot:
while load_answer not in yes_no:
load_answer = generic_prompt(load_question)
if load_answer == 'yes':Anytime you find yourself repeating code, it'd be helpful to write a function instead. Something like:
def ask_boolean_question(prompt):
answer = ''
while answer not in yes_no:
answer = generic_prompt(prompt)
return answer == 'yes'Now your code can simply say:
if ask_boolean_question(load_question):And avoid repeating the loop everytime you ask a question.
Your program doesn't make its structure very clear. Given what your program does, your main function should read something like:
def main():
manipulate_settings(default_settings)
while ask_boolean_question(load_question):
filename = generic_prompt(filename_question)
manipulate_settings(filename)Note that by reading this, you can see the program processes first the default settings, and then as many other settings files as you have inclination to use the program.
Code Snippets
while load_answer not in yes_no:
load_answer = generic_prompt(load_question)
if load_answer == 'yes':def ask_boolean_question(prompt):
answer = ''
while answer not in yes_no:
answer = generic_prompt(prompt)
return answer == 'yes'if ask_boolean_question(load_question):def main():
manipulate_settings(default_settings)
while ask_boolean_question(load_question):
filename = generic_prompt(filename_question)
manipulate_settings(filename)Context
StackExchange Code Review Q#59763, answer score: 5
Revisions (0)
No revisions yet.