def round(self, cr, uid, currency, amount): """Return ``amount`` rounded according to ``currency``'s rounding rules. :param browse_record currency: currency for which we are rounding :param float amount: the amount to round :return: rounded float """ return float_round(amount, precision_rounding=currency.rounding)
def _regularize_new_asset_depreciations(self, cr, uid, new_asset_id, context=None): new_asset = self.pool.get('account.asset.asset').browse(cr, uid, new_asset_id, context) line_to_update_ids = [] for line in new_asset.depreciation_line_ids: if line.move_id: value_field = line.depreciation_type == 'fiscal' and 'accelerated_value' or 'depreciation_value' gap = line.move_id.amount - sum([getattr(l, value_field) for l in line.move_id.asset_depreciation_line_ids]) rounding = line.asset_id.currency_id.rounding gap = float_round(gap, precision_digits=len(str(rounding).split('.')[-1])) if gap > rounding: # TODO: fix me _logger.warning('Asset Split [new_asset_id=%s,line_id=%s,depreciation_type=%s] - Gap = %s > %s' % (new_asset_id, line.id, line.depreciation_type, gap, rounding)) if gap: line.write({'depreciation_value': line.depreciation_value + gap}) line_to_update_ids.append(line.id) if line_to_update_ids: self.pool.get('account.asset.depreciation.line')._store_set_values(cr, uid, line_to_update_ids, ['accelerated_value'], context)
def required_computation(): """ Compute the average price of each week, store in a csv file, and print the week that had the greatest relative spanself. """ # read with csv reader (handle bom and unicode) try: read_data = list(unicode_csv_dictreader('currency_daily_BTC_USD.csv')) print 'Successfully read {} daily data.'.format(len(read_data)) except (KeyboardInterrupt, SystemExit): raise except: print 'Failed to read data: {}'.format(traceback.format_exc()) read_data = [] # set the initial values start_date = None end_date = None count = 0 sum = 0 max = 0 min = 0 greatest_relative_span = 0 flagged_start_date = None flagged_end_date = None from decimal import Decimal from pandas import DataFrame report = DataFrame() for data in read_data: date_str = str(data['timestamp']) start_date = date_str if end_date is None: end_date = date_str count += 1 price_today = Decimal(data['close (USD)']) sum += price_today if max == 0 or price_today > max: max = price_today if min == 0 or price_today < min: min = price_today if find_weekday(date_str) == 0: average = float_round(sum / count, 8) relative_span = float_round((max - min) / min, 8) to_be_inserted = { 'start_date': [start_date], 'end_date': [end_date], 'average': [average], 'max': [max], 'min': [min], 'relative_span': [relative_span] } report = report.append(DataFrame(to_be_inserted)) if greatest_relative_span == 0 or relative_span > greatest_relative_span: greatest_relative_span = relative_span flagged_start_date = start_date flagged_end_date = end_date end_date = None count = 0 sum = 0 max = 0 min = 0 # make sure the last week is taken into consideration if find_weekday(date_str) != 0: average = float_round(sum / count, 8) relative_span = float_round((max - min) / min, 8) to_be_inserted = { 'start_date': [start_date], 'end_date': [end_date], 'average': [average], 'max': [max], 'min': [min], 'relative_span': [relative_span] } report = report.append(DataFrame(to_be_inserted)) if relative_span > greatest_relative_span: greatest_relative_span = relative_span flagged_start_date = start_date flagged_end_date = end_date os.chdir('/usr/src/app/Reports') report.to_csv('week_report.csv', encoding='utf-8', columns=[ 'start_date', 'end_date', 'average', 'max', 'min', 'relative_span' ]) print( 'The greatest relative span happened in the week between {} and {} ' 'with the span of {}.'.format(flagged_start_date, flagged_end_date, greatest_relative_span))