def __init__(self, rows, msh_price=None): # Don't like this, but rows is being modified below, causing side effects rows = deepcopy(rows) if msh_price is not None: # msh_price might be a Decimal, which max() thinks is larger than # any float, so in order to compare properly with floats we need # to convert msh_price to float as well self.max_price = max(self.max_price, float(msh_price)) # print "input data = %s" % rows (self.median_fob_price, self.median_landed_price) = \ utils.get_median_prices(rows) # print "median prices = %s, %s" % (self.median_fob_price, self.median_landed_price) for row in rows: new_max_price = max(self.max_price, row['fob_price'], row['landed_price']) # logging.warning("max of %s, %s and %s is %s" % # (self.max_price, row['fob_price'], row['landed_price'], # new_max_price)) self.max_price = new_max_price self.round_to_set_decimal_places(row, 'fob_price') self.round_to_set_decimal_places(row, 'landed_price') # scale up the value to between 1 and 10, then round up the first # digit to 1, 2 or 5 to make nicer graph scales rounded_up = self.max_price scale_factor = 1 while rounded_up < 1: rounded_up = rounded_up * 10 scale_factor = scale_factor * 10 while rounded_up > 10: rounded_up = rounded_up / 10.0 scale_factor = scale_factor / 10.0 if rounded_up > 5: rounded_up = 10.0 elif rounded_up > 2: rounded_up = 5.0 elif rounded_up > 1: rounded_up = 2.0 # else it's 1, which is fine # scale back down self.max_price = rounded_up / scale_factor # generate the intervals on the graph scale self.scale = [] for k in range(1, 11): self.scale.append(k * (self.max_price / 10)) self.msh_price = msh_price SarpamTable.__init__(self, rows)
def test_none_values_ignored_when_calculating_median_landed_price_of_list(self): price_list = [ {"landed_price": None, "fob_price": None}, {"landed_price": 0.09, "fob_price": None}, {"landed_price": None, "fob_price": None}, {"landed_price": 0.05, "fob_price": None}, {"landed_price": None, "fob_price": None}, {"landed_price": 0.14, "fob_price": None}, ] median = utils.get_median_prices(price_list) self.assertAlmostEquals(0.09, median[1])
def test_none_values_ignored_when_calculating_median_landed_price_of_list( self): price_list = [{ 'landed_price': None, 'fob_price': None }, { 'landed_price': 0.09, 'fob_price': None }, { 'landed_price': None, 'fob_price': None }, { 'landed_price': 0.05, 'fob_price': None }, { 'landed_price': None, 'fob_price': None }, { 'landed_price': 0.14, 'fob_price': None }] median = utils.get_median_prices(price_list) self.assertAlmostEquals(0.09, median[1])