Ejemplo n.º 1
0
 def get_amount_as(self, new_unit, rounded=True, single_value=False):
     if self.unit == u'literal':
         return
     if isinstance(self.amount, tuple):
         lower  = util.convert_units(self.amount[0], self.unit, new_unit, rounded=rounded)
         higher = util.convert_units(self.amount[1], self.unit, new_unit, rounded=rounded)
         amount = (lower+higher)/2.0 if single_value else (lower, higher)
     else:
         amount = util.convert_units(self.amount, self.unit, new_unit, rounded=rounded)
     return amount
Ejemplo n.º 2
0
    def collect_units(self, string):
        #Assuming the form `Name - <amount> <units>`
        ## i.e. Signature Care Hand Soap Clear Moisturizing - 7.5 Fl. Oz.
        #Also has the form `Name - <quantity>-<amount> <units>`
        ## i.e. SPF 50 - 2-9.1 Oz
        #self.logger.info (f"Working on string - {string}")
        split_off_name = string.split(' - ')
        if len(split_off_name) < 2:
            self.logger.info(f"unable to collect units from {string}")
            return ""
        unit_section = split_off_name[-1]
        #self.logger.info(f"unit-section - {unit_section}")
        quantity = 1
        if unit_section.find('-') != -1:
            # If theirs still a hyphen then we have a quantity to parse off first
            quantity_split = unit_section.split('-')
            if quantity_split[0].isdigit():
                quantity = int(quantity_split[0])
            unit_section = quantity_split[-1]

        #Now we should be in the form X.X Units
        decimal_regex = "([\d]+[.]?[\d]*|[.\d]+)"
        complete_regex = decimal_regex + "(.*)"
        decimal_broken = re.findall(complete_regex, unit_section)
        #self.logger.info(f"collect_units, quantity - {quantity}, decimal - {decimal_broken}")
        if len(decimal_broken) == 0:
            # if its in the Name - EA form
            # Just Use the EA as the units
            decimal_broken = unit_section
        elif type(decimal_broken[0]) is tuple:
            decimal_broken = list(decimal_broken[0])
        unit = convert_units(decimal_broken[-1])

        return unit
Ejemplo n.º 3
0
def _calculated_columns(thing):
    """ Given an object with the required fields,
    calculate and add the other fields
    """
    thing['Size (oz)'] = util.convert_units(thing['Size (mL)'], 'mL', 'oz')
    thing['$/mL'] = thing['Price Paid'] / thing['Size (mL)']
    thing['$/cL'] = thing['Price Paid']*10 / thing['Size (mL)']
    thing['$/oz'] = thing['Price Paid'] / thing['Size (oz)']
Ejemplo n.º 4
0
def _update_computed_fields(row):
    """ Uses clean names
    """
    row.type_ = row.Type.lower()
    try:
        row.Size_oz = util.convert_units(row.Size_mL, 'mL', 'oz')
        row.Cost_per_mL = row.Price_Paid  / row.Size_mL
        row.Cost_per_cL = row.Price_Paid*10  / row.Size_mL
        row.Cost_per_oz = row.Price_Paid  / row.Size_oz
    except ZeroDivisionError:
        log.warning("Ingredient missing size field: {}".format(row))
Ejemplo n.º 5
0
    def __init__(self, type_str, raw_quantity, recipe_unit):
        self.top_with = False
        self.specifier = util.IngredientSpecifier.from_string(type_str)

        # interpret the raw quantity
        if isinstance(raw_quantity, basestring):
            if raw_quantity == u'Top with': # counts at 3 ounces on average
                self.amount = util.convert_units(3.0, u'oz', recipe_unit, rounded=True)
                self.unit = recipe_unit
                self.top_with = True

            elif u'dash' in raw_quantity:
                self.unit = u'ds'
                if raw_quantity == u'dash':
                    self.amount = 1
                elif re.match(r'[0-9]+ dashes', raw_quantity):
                    self.amount = int(raw_quantity.split()[0])
                elif re.match(r'[0-9]+ to [0-9]+ dashes', raw_quantity):
                    self.amount = (int(raw_quantity.split()[0]), int(raw_quantity.split()[2]))
                else:
                    raise RecipeError(u"Unknown format for dash amount: {} {}".format(raw_quantity, type_str))

            elif u'tsp' in raw_quantity:
                try:
                    self.amount = float(raw_quantity.split()[0])
                except ValueError:
                    self.amount = float(Fraction(raw_quantity.split()[0]))
                self.unit = u'tsp'

            elif u'drop' in raw_quantity:
                self.unit = u'drop'
                if raw_quantity == u'drop':
                    self.amount = 1
                elif re.match(r'[0-9]+ drops', raw_quantity):
                    self.amount = int(raw_quantity.split()[0])
                else:
                    raise RecipeError(u"Unknown format for drop amount: {} {}".format(raw_quantity, type_str))

            elif raw_quantity in [u'one', u'two', u'three', u'four', u'five', u'six', u'seven', u'eight']:
                self.amount = raw_quantity
                self.unit = u'literal'
            else:
                # accept other options as literal ingredients
                self.amount = raw_quantity
                self.unit = u'literal'
                #raise RecipeError(u"Unknown ingredient quantity: {} {}".format(raw_quantity, type_str))
        else:
            self.amount = raw_quantity
            self.unit = recipe_unit
Ejemplo n.º 6
0
 def collect_unit(self, string):
     raw_units = re.findall("[A-Za-z/s]+", string)[0]
     unit = convert_units(raw_units)
     return unit