예제 #1
0
 def test_one_file(self):
     """Test reading of one input file with two columns and one row"""
     input_file = StringIO("Flour, Sugar\n1g,2g")
     ingredients, columns = read_files([input_file])
     self.assertEquals((FLOUR, SUGAR), ingredients)
     self.assertEquals(len(columns[0]), 2)
     self.assertEquals(len(columns), 1)
예제 #2
0
 def test_two_files(self):
     """Test reading of two (identical) input files"""
     input_file_1 = StringIO("Flour, Sugar\n1g,2g")
     input_file_2 = StringIO("Flour, Sugar\n1g,2g")
     ingredients, columns = read_files([input_file_1, input_file_2])
     self.assertEquals((FLOUR, SUGAR), ingredients)
     self.assertEquals(len(columns), 2)
     self.assertEquals(len(columns[0]), 2)
예제 #3
0
 def test_non_matching_headers(self):
     """Test error condition where two input files are read with differing
        ingredients"""
     input_file_1 = StringIO("Flour, Sugar\n1g,2g")
     input_file_2 = StringIO("Flour, Salt\n1g,2g")
     try:
         _ingredients, _columns = read_files([input_file_1, input_file_2])
         self.fail("Expected error")
     except InvalidInputException, error:
         self.assertEquals(str(error),
                           "All input files must have the same header.")
예제 #4
0
def get_ratio_and_stats(filenames, distinct, merge, zero_columns=None):
    """Parse input files to produce mean recipe ratio and related statistics
    """
    files = [open(filename) for filename in filenames]
    ingredients, proportions = read_files(files)
    proportions = to_grams(ingredients, proportions)
    if distinct:
        proportions = set(proportions)
    ingredients, proportions = merge_columns(ingredients, proportions,
                                                 merge)
    statistics = calculate_statistics(proportions, ingredients, zero_columns)
    ratio = Ratio(ingredients, statistics.bakers_percentage())
    return ingredients, ratio, statistics, len(proportions)