Пример #1
0
 def test_required_is_none(self):
     """
     Pass in a None value and raise exception.
     """
     fw_config = deepcopy(SAMPLE_CONFIG)
     fw_obj = FixedWidth(fw_config)
     fw_obj.update(
         last_name="Smith", first_name="Michael",
         age=32, meal="vegetarian", latitude=Decimal('40.7128'),
         longitude=Decimal('-74.0059'), elevation=None,
     )
     self.assertRaises(Exception, fw_obj.validate)
Пример #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,
                )
Пример #3
0
    def test_optional_is_none(self):
        """
        Pass in a None value and raise exception.
        """
        fw_config = deepcopy(SAMPLE_CONFIG)
        fw_obj = FixedWidth(fw_config)
        fw_obj.update(
            last_name="Smith", first_name="Michael",
            age=32, meal="vegetarian", latitude=Decimal('40.7128'),
            longitude=Decimal('-74.0059'), elevation=-100, decimal_precision=None,
        )

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

        self.assertEqual(fw_obj.line, good)
Пример #4
0
    def test_update(self):
        """
        Test FixedWidth.update()
        """

        fw_config = deepcopy(SAMPLE_CONFIG)
        fw_obj = FixedWidth(fw_config)

        fw_obj.update(
            last_name="Smith", first_name="Michael",
            age=32, meal="vegetarian", latitude=Decimal('40.7128'),
            longitude=Decimal('-74.0059'), elevation=-100, decimal_precision=1,
        )

        #change a value
        fw_obj.update(meal="Paleo")
        self.assertEqual(fw_obj.data["meal"], "Paleo")

        #nothing else should have changed
        self.assertEqual(fw_obj.data["first_name"], "Michael")
Пример #5
0
    def test_basic(self):
        """
        Test a simple, valid example.
        """

        fw_config = deepcopy(SAMPLE_CONFIG)
        fw_obj = FixedWidth(fw_config)
        fw_obj.update(
            last_name="Smith", first_name="Michael",
            age=32, meal="vegetarian", latitude=Decimal('40.7128'),
            longitude=Decimal('-74.0059'), elevation=-100, decimal_precision=Decimal('1.0001'),
        )

        fw_string = fw_obj.line

        good = (
            "Michael   Smith                              "
            "032vegetarian             40.7128   -74.0059-100   98.6201701011.001        \r\n"
        )

        self.assertEqual(fw_string, good)