def write_month_data_to_file( monthData , commodity ): month = DateUtils.format_month( monthData.month ) year = str( monthData.year ) datafile = "price_data/" + year + " " + month + "/" + commodity + ".csv" #create the new month folder, if necessary if ( not os.path.exists( os.path.dirname( datafile ) ) ): os.makedirs( os.path.dirname( datafile ) ) f = open( datafile , "w" ) f.write( str( monthData ) )
def read_month_data( month , year , commodity ): rtn = MonthData( month , year ) dir = "price_data/" + str( year ) + " " + DateUtils.format_month( month ) try : file = open( dir + "/" + commodity + ".csv" , "r" ) lines = file.readlines() for line in lines : datapoint = DataPoint.from_csv_month_data( year , month , line ) rtn.set( int( datapoint.get_day() ) , datapoint ) except IOError: #there is no data, so ignore the error and return default #values of 0 for daily and average prices pass return rtn