Python: Orders Manager P4
Learning : Orders Management System using Python and Pandas
Subject: File Exists and Adding new Record
In the last post of our system, we develop the file_exists function, the function is checking if the file not exists then will create ond and enter a dummy data. Now we need to add a code to the application body that call this function and if the file exists the application will run without applying the or creating any file. Here is the code in the application body:
Header here
if file_exists() != ‘exit’ :
# calling the menu
user_enter = the_menu()
”’
Validation: If the user enter any thing else than numbers
or (q for quit) nothing will happen.
”’
while user_enter !=’q’ or ‘Q’ :
if user_enter in [‘q’,’Q’] :
print(‘\n You select to Exit the application.’)
save_it = input(‘\n Do your want to save your work/changes [y or n] ? ‘)
if save_it in [‘y’,’Y’]:
save_the_df (df)
break
elif user_enter not in [‘1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9’] :
user_enter = the_menu()
else:
user_choice(user_enter)
user_enter = the_menu()
![]() |
In this post we will talk about the Adding new record function, since we may start from new file we need to enter some records in our data file. Here is the def add_new_record() that will help us to enter our data.
Add New Record Function
def add_new_record(old_df):
clear() # To clear the terminal.
app_header()
# First we will fetch the columns from the df
col_list = []
for each in old_df.columns :
col_list.append(each)
print(col_list)
# Get max id and increase it by 1
next_id = old_df[‘order_no’].max()+1
new_row={}
# let user enter the new record.
print(‘\n Enter the data for each field then press Enter.\n’ )
print(‘ If you just press Enter NaN will be entered.’)
for each in col_list:
if each !=’order_no’:
print(‘ Enter data for ‘,each)
new_row.update({each:(input(‘ : ‘))})
new_row.update({‘order_no’:next_id})
old_df = old_df.append([new_row])
for each in col_list :
if (old_df.loc[old_df[‘order_no’] == next_id, each][0]) ==”:
(old_df.loc[old_df[‘order_no’] == next_id,[each]]) = float(‘NaN’)
print(‘\n New Record added successfully..\n’)
# print out last 5 rows to show the new record.
print(‘\n The new record in the df..\n ‘,old_df.tail(5))
global df # Reset the df as global variable
df = old_df
input(‘\n\n\n\n ** Press any key to continue .. . . ‘)
![]() |
In the coming post, we will work on the date validation function also the user choice loop so we can run the application and test it.