@@ -32,16 +32,20 @@ def load_stocks_file():
3232 global stocks_file_path
3333 try :
3434 with open (stocks_file_path , "r" ) as file :
35- stocks_data = file .read ()
36- return json .loads ("{" + stocks_data .split ("{" , 1 )[1 ])
35+ stocks_data = json .load (file )
36+ # Remove spaces from keys
37+ stocks_data = {key .strip (): value for key , value in stocks_data .items ()}
38+ return stocks_data
3739 except FileNotFoundError : # If the save file is not found, ask the user to enter the path to the save file
3840 print (Fore .RED + f"File { stocks_file_path } not found." + Style .RESET_ALL )
3941 new_path = input ("Please enter the path to the stocks.save file: " )
4042 try :
4143 with open (new_path , "r" ) as file :
42- stocks_data = file .read ()
44+ stocks_data = json .load (file )
45+ # Remove spaces from keys
46+ stocks_data = {key .strip (): value for key , value in stocks_data .items ()}
4347 stocks_file_path = new_path # Update the save file path
44- return json . loads ( "{" + stocks_data . split ( "{" , 1 )[ 1 ])
48+ return stocks_data
4549 except FileNotFoundError :
4650 print (Fore .RED + f"File { stocks_file_path } not found." + Style .RESET_ALL )
4751 return None
@@ -288,13 +292,16 @@ def main():
288292 elif choice == "9" : # edit stocks
289293 stocks_data = load_stocks_file ()
290294 print (Fore .GREEN + f"File { stocks_file_path } loaded." + Style .RESET_ALL )
291- print (f"Last modified: { datetime .datetime .fromtimestamp (os .path .getmtime (stocks_file_path )).strftime ('%Y-%m-%d %H :%M:%S' )} " )
292- print (f"Last accessed: { datetime .datetime .fromtimestamp (os .path .getatime (stocks_file_path )).strftime ('%Y-%m-%d %H :%M:%S' )} " )
295+ print (f"Last modified: { datetime .datetime .fromtimestamp (os .path .getmtime (stocks_file_path )).strftime ('%Y-%m-%d %I :%M:%S %p ' )} " )
296+ print (f"Last accessed: { datetime .datetime .fromtimestamp (os .path .getatime (stocks_file_path )).strftime ('%Y-%m-%d %I :%M:%S %p ' )} " )
293297 # print(f"Data: {stocks_data}")
294298 # Print the stock tickers
295- tickers = [ticker for ticker in stocks_data .keys () if ticker not in ['fish_found' , 'org_found' ]]
299+ tickers = [ticker . strip () for ticker in stocks_data .keys () if ticker not in ['fish_found' , 'org_found' ]]
296300 tickers_str = ", " .join (tickers )
297301 print (f"Tickers: { tickers_str } " )
302+ # Print the stocks owned
303+ owned_stocks_str = ", " .join ([f"{ ticker .strip ()} ({ Fore .GREEN } { stocks_data [ticker ]['owned' ]} { Style .RESET_ALL } )" for ticker in stocks_data if isinstance (stocks_data [ticker ], dict ) and 'owned' in stocks_data [ticker ] and stocks_data [ticker ]['owned' ] > 0 ])
304+ print (f"Bought stocks: { owned_stocks_str } " )
298305 stocks_data = load_stocks_file ()
299306 if stocks_data is None :
300307 return
@@ -305,7 +312,7 @@ def main():
305312 print ("3. Go back to main menu" )
306313 choice = input ("Enter your choice: " )
307314 if choice == "1" :
308- stock_name = input ("Enter the stock name (Ticker): " )
315+ stock_name = input ("Enter the stock name (Ticker): " ). strip () # remove leading and trailing spaces
309316 if stock_name in stocks_data :
310317 owned_stocks = input ("Enter the number of owned stocks: " )
311318 stocks_data [stock_name ]['owned' ] = int (owned_stocks )
@@ -314,7 +321,7 @@ def main():
314321 else :
315322 print (Fore .RED + f"Stock { stock_name } not found." + Style .RESET_ALL )
316323 elif choice == "2" :
317- stock_name = input ("Enter the stock name (Ticker): " )
324+ stock_name = input ("Enter the stock name (Ticker): " ). strip () # remove leading and trailing spaces
318325 if stock_name in stocks_data :
319326 stocks_data [stock_name ]['owned' ] = 0
320327 save_stocks_file (stocks_data )
0 commit comments