Exemplo n.º 1
0
def main():
    """
    Concatenate csv files together in no particular order.
    """
    import pathlib

    input_dir = pathlib.Path("test_data/things_kinds")

    file_path_names = i_walk_dir_for_filepaths_names(str(input_dir))

    csv_file_path_names = ifilter(karld.io.is_file_csv, file_path_names)

    out_prefix = ""
    out_dir = pathlib.Path("out_data/things_kinds")
    out_filename = "combined_things.csv"

    csv_files_to_file(chain.from_iterable, out_prefix, str(out_dir), out_filename, csv_file_path_names)
Exemplo n.º 2
0
    def test_sort_merge_csv_files_to_file(self):
        """
        Ensure csv_files_to_file will read multiple
        csv files and write one csv file
        with the contents as yielded from the
        given combiner function.

        Ensure i_walk_dir_for_filepaths_names produces
        the paths and basenames of the files in the
        test_data directory.
        """
        from karld.run_together import csv_files_to_file

        out_dir = os.path.join(tempfile.gettempdir(),
                               "karld_test_sort_merge")

        prefix = str(datetime.now())

        out_filename = "things_combined.csv"
        input_dir = os.path.join(os.path.dirname(__file__),
                                 "test_data",
                                 "things_kinds")

        file_path_names = i_walk_dir_for_filepaths_names(input_dir)

        expected_file = os.path.join(out_dir,
                                     "{}{}".format(prefix, out_filename))

        if os.path.exists(expected_file):
            os.remove(expected_file)

        csv_file_path_names = ifilter(
            is_file_csv,
            file_path_names)

        csv_files_to_file(
            combine_things,
            prefix,
            out_dir,
            out_filename,
            csv_file_path_names)

        self.assertTrue(os.path.exists(expected_file))

        with open(expected_file) as result_file:
            contents = result_file.read()
            expected_lines = ['cat,animal',
                              'cheese,dairy',
                              'apple,fruit',
                              'orange,fruit',
                              'peach,fruit',
                              'pear,fruit',
                              'tomato,fruit',
                              'mushroom,fungus',
                              'iron,metal',
                              'titanium,metal',
                              'ruby,mineral',
                              'topaz,mineral',
                              'WĄŻ,utf-8 sample',
                              'dróżką,utf-8 sample',
                              'celery,vegetable']

            lines = contents.splitlines()
            self.assertEqual(expected_lines, lines)

        if os.path.exists(expected_file):
            os.remove(expected_file)