Exemplo n.º 1
0
    def test_fw_to_dict(self):
        """
        Pass in a line and receive dictionary.
        """

        fw_config = deepcopy(SAMPLE_CONFIG)

        fw_obj = FixedWidth(fw_config)

        fw_obj.line = (
            "Michael   Smith                              "
            "032vegetarian             40.7128   -74.0059-100   98.6201701011.000        \r\n"
        )

        values = fw_obj.data
        self.assertEqual(values["first_name"], "Michael")
        self.assertEqual(values["last_name"], "Smith")
        self.assertEqual(values["age"], 32)
        self.assertEqual(values["meal"], "vegetarian")
        self.assertEqual(values["latitude"], Decimal('40.7128'))
        self.assertEqual(values["longitude"], Decimal('-74.0059'))
        self.assertEqual(values["elevation"], -100)
        self.assertEqual(values["temperature"], Decimal('98.6'))
        self.assertEqual(values["decimal_precision"], Decimal('1.000'))
        self.assertEqual(values["date"],
                         datetime.datetime.strptime('20170101', '%Y%m%d'))
        self.assertEqual(values["none_date"], None)
Exemplo n.º 2
0
def main():

    st.header("App for converting csv to fixed-width and other way around")
    st.sidebar.subheader("Upload config file [.csv]")
    st.sidebar.write("Config file should be same for both ways!")
    config_file_upload = st.sidebar.file_uploader("Choose a file")
    if config_file_upload is not None:
        selection = st.sidebar.selectbox(
            "What would you like to convert?",
            ("CSV->FixedWidth", "FixedWidth->CSV"))
        config_file = read_csv_file(config_file_upload)
        CONFIG = create_config(config_file)
        # Reads config for FixedWidth lib
        fw_config = deepcopy(CONFIG)
        fw_obj = FixedWidth(fw_config)

        if selection == 'CSV->FixedWidth':
            st.subheader("Upload CSV file")
        else:
            st.subheader("Upload FixedWidth file")
        data_file = st.file_uploader("Upload a data file")
        if data_file is not None:
            if selection == "CSV->FixedWidth":
                data = read_csv_file(data_file)
                lines = ""
                for i, row in enumerate(data):
                    row = clean_row(row)
                    fw_obj.update(**row)
                    fw_string = fw_obj.line
                    lines += fw_string
                f = open("yourfile.main", "w")
                f.write(lines)
                f.close()
                st.markdown(
                    get_binary_file_downloader_html("yourfile.main",
                                                    "my_file.main"),
                    unsafe_allow_html=True,
                )
            else:
                rows = []
                data = read_fw_file(data_file)
                for i, row in enumerate(data):
                    fw_obj.line = row
                    values = fw_obj.data
                    rows.append(values)
                write_csv(rows)
                st.markdown(
                    get_binary_file_downloader_html("yourfile.csv",
                                                    "my_file.csv"),
                    unsafe_allow_html=True,
                )
Exemplo n.º 3
0
    def test_fw_contains_empty_value(self):
        """
        Pass in a line with empty value and test that default gets set.
        """

        fw_config = deepcopy(SAMPLE_CONFIG)

        fw_obj = FixedWidth(fw_config)
        fw_obj.line = (
            "Michael   Smith                              "
            "032vegetarian             40.7128   -74.0059-100   98.620170101              \r\n"
        )

        self.assertEqual(fw_obj.data["decimal_precision"], Decimal(1))