예제 #1
0
    def check(self, name, skip_if_win=False):
        """
            Check if the output array from csv_files/<name>.csv
            (which is of unkown format)
            is the same as the array in csv_files/<name>.py
        """
        if skip_if_win and sys.platform.startswith('win'):
            raise nose.SkipTest

        # Note: The files needed for the test are currently accessed directly.
        #       This assumes that the files are present, and not in a zipped
        #       egg.  traits.util.resource as supposed to always work, but
        #       when it ran the striped the tests in the EPD installer tests,
        #       it was broken.  Since we define zip_safe = False in setup.py
        #       it is safe to assume the files are always present.

        s = Sniff(
            os.path.join(os.path.dirname(__file__), 'csv_files',
                         name + '.csv'))

        f_py = os.path.join(os.path.dirname(__file__), 'csv_files',
                            name + '.py')

        if not sys.platform.startswith('win'):
            nan = float('nan')  # must be in namespace for some .py files

        d = eval(open(f_py).read())

        self.assertEqual(d['kwds'], s.kwds())
        self.assertNamedClose(d['array'], s.loadtxt())
예제 #2
0
    def check(self, name, skip_if_win=False):
        """
            Check if the output array from csv_files/<name>.csv
            (which is of unkown format)
            is the same as the array in csv_files/<name>.py
        """
        if skip_if_win and sys.platform.startswith("win"):
            raise nose.SkipTest

        # Note: The files needed for the test are currently accessed directly.
        #       This assumes that the files are present, and not in a zipped
        #       egg.  traits.util.resource as supposed to always work, but
        #       when it ran the striped the tests in the EPD installer tests,
        #       it was broken.  Since we define zip_safe = False in setup.py
        #       it is safe to assume the files are always present.

        s = Sniff(os.path.join(os.path.dirname(__file__), "csv_files", name + ".csv"))

        f_py = os.path.join(os.path.dirname(__file__), "csv_files", name + ".py")

        if not sys.platform.startswith("win"):
            nan = float("nan")  # must be in namespace for some .py files

        d = eval(open(f_py).read())

        self.assertEqual(d["kwds"], s.kwds())
        self.assertNamedClose(d["array"], s.loadtxt())
예제 #3
0
    def test_tabs(self):
        fo = tempfile.mktemp()
        with open(fo, 'w') as f:
            f.write('''54\t87\n21\t32''')

        s = Sniff(fo)
        self.assertEqual(s.delimiter(), None)
        self.assertEqual(s.skiprows(), 0)
예제 #4
0
    def test_tabs(self):
        fo = open(TESTFN, 'wb')
        fo.write('''54\t87\n21\t32''')
        fo.close()

        s = Sniff(TESTFN)
        self.assertEqual(s.delimiter(), None)
        self.assertEqual(s.skiprows(), 0)
예제 #5
0
    def test_tabs(self):
        fo = open(TESTFN, 'wb')
        fo.write('''54\t87\n21\t32''')
        fo.close()

        s = Sniff(TESTFN)
        self.assertEqual(s.delimiter(), None)
        self.assertEqual(s.skiprows(), 0)
예제 #6
0
    def test_tabs(self):
        fo = tempfile.mktemp()
        with open(fo, "w") as f:
            f.write("""54\t87\n21\t32""")

        s = Sniff(fo)
        self.assertEqual(s.delimiter(), None)
        self.assertEqual(s.skiprows(), 0)
예제 #7
0
    def test_nohead(self):
        fo = open(TESTFN, 'wb')
        fo.write('''Hello;54;87\nWorld;42;86.5''')
        fo.close()

        s = Sniff(TESTFN)
        self.assertEqual(s.kwds(),
          {'comments': '#',
           'delimiter': ';',
           'skiprows': 0,
           'dtype': {'names': ('Column 1', 'Column 2', 'Column 3'),
                     'formats': ('S5', float, float)}})
예제 #8
0
    def test_nohead(self):
        fo = tempfile.mktemp()
        with open(fo, 'w') as f:
            f.write('''Hello;54;87\nWorld;42;86.5''')

        s = Sniff(fo)
        self.assertEqual(s.kwds(),
          {'comments': '#',
           'delimiter': ';',
           'skiprows': 0,
           'dtype': {'names': ('Column 1', 'Column 2', 'Column 3'),
                     'formats': ('S5', float, float)}})
    def test_nohead(self):
        fo = tempfile.mktemp()
        with open(fo, 'w', encoding="utf-8") as f:
            f.write('''Hello;54;87\nWorld;42;86.5''')

        s = Sniff(fo)
        self.assertEqual(s.kwds(),
          {'comments': '#',
           'delimiter': ';',
           'skiprows': 0,
           'dtype': {'names': ('Column 1', 'Column 2', 'Column 3'),
                     'formats': ('S5', float, float)}})
예제 #10
0
    def test_API(self):
        fo = open(TESTFN, 'wb')
        fo.write(''' "A", "B", "C"
                     1, 2, 3.2
                     7, 4, 1.87''')
        fo.close()

        s = Sniff(TESTFN)
        self.assertEqual(s.comments(), '#')
        self.assertEqual(s.delimiter(), ',')
        self.assertEqual(s.skiprows(), 1)
        self.assertEqual(s.dtype(), {
            'names': ('A', 'B', 'C'),
            'formats': (float, float, float)
        })
        x = s.loadtxt()
        y = array([(1.0, 2.0, 3.20), (7.0, 4.0, 1.87)],
                  dtype=[('A', float), ('B', float), ('C', float)])
        self.assertNamedClose(x, y)

        y = loadtxt(TESTFN, **s.kwds())
        self.assertNamedClose(x, y)

        y = loadtxt_unknown(TESTFN)
        self.assertNamedClose(x, y)

        d = array2dict(y)
        self.assertEqual(type(d), type({}))
        self.assertAllClose(x['A'], [1, 7])
        self.assertAllClose(x['B'], [2, 4])
        self.assertAllClose(x['C'], [3.2, 1.87])
예제 #11
0
    def test_comment(self):
        fo = open(TESTFN, 'wb')
        fo.write('''
        % "A"  "B"  "C"
           1    2   4.2   % comment''')
        fo.close()

        s = Sniff(TESTFN)
        self.assertEqual(s.kwds(),
          {'dtype': {'names': ('A', 'B', 'C'),
                     'formats': (float, float, float)},
           'delimiter': None,
           'skiprows': 0,   # FIXME
           'comments': '%'})
    def test_comment(self):
        fo = tempfile.mktemp()
        with open(fo, 'w', encoding="utf-8") as f:
            f.write('''
            % "A"  "B"  "C"
               1    2   4.2   % comment''')

        s = Sniff(fo)
        self.assertEqual(s.kwds(),
          {'dtype': {'names': ('A', 'B', 'C'),
                     'formats': (float, float, float)},
           'delimiter': None,
           'skiprows': 0,   # FIXME
           'comments': '%'})
    def test_API(self):
        fo = tempfile.mktemp()
        with open(fo, 'w', encoding="utf-8") as f:
            f.write(''' "A", "B", "C"
                         1, 2, 3.2
                         7, 4, 1.87''')

        s = Sniff(fo)
        self.assertEqual(s.comments(), '#')
        self.assertEqual(s.delimiter(), ',')
        self.assertEqual(s.skiprows(), 1)
        self.assertEqual(s.dtype(), {'names': ('A', 'B', 'C'),
                                     'formats': (float, float, float)})
        x = s.loadtxt()
        y = array([(1.0, 2.0, 3.20),
                   (7.0, 4.0, 1.87)],
                  dtype=[('A', float), ('B', float), ('C', float)])
        self.assertNamedClose(x, y)

        with open(fo, 'r', encoding="utf-8") as fh:
            y = loadtxt(fh, **s.kwds())
        self.assertNamedClose(x, y)

        y = loadtxt_unknown(fo)
        self.assertNamedClose(x, y)

        d = array2dict(y)
        self.assertEqual(type(d), type({}))
        self.assertAllClose(x['A'], [1, 7])
        self.assertAllClose(x['B'], [2, 4])
        self.assertAllClose(x['C'], [3.2, 1.87])
예제 #14
0
    def test_comment(self):
        fo = tempfile.mktemp()
        with open(fo, 'w') as f:
            f.write('''
            % "A"  "B"  "C"
               1    2   4.2   % comment''')

        s = Sniff(fo)
        self.assertEqual(s.kwds(),
          {'dtype': {'names': ('A', 'B', 'C'),
                     'formats': (float, float, float)},
           'delimiter': None,
           'skiprows': 0,   # FIXME
           'comments': '%'})
예제 #15
0
    def test_nohead(self):
        fo = tempfile.mktemp()
        with open(fo, "w") as f:
            f.write("""Hello;54;87\nWorld;42;86.5""")

        s = Sniff(fo)
        self.assertEqual(
            s.kwds(),
            {
                "comments": "#",
                "delimiter": ";",
                "skiprows": 0,
                "dtype": {"names": ("Column 1", "Column 2", "Column 3"), "formats": ("S5", float, float)},
            },
        )
예제 #16
0
    def test_nohead(self):
        fo = open(TESTFN, 'wb')
        fo.write('''Hello;54;87\nWorld;42;86.5''')
        fo.close()

        s = Sniff(TESTFN)
        self.assertEqual(
            s.kwds(), {
                'comments': '#',
                'delimiter': ';',
                'skiprows': 0,
                'dtype': {
                    'names': ('Column 1', 'Column 2', 'Column 3'),
                    'formats': ('S5', float, float)
                }
            })
예제 #17
0
    def guess_defaults(self):
        try:
            kwds = Sniff(self.filename).kwds()
        except:
            kwds = {
                'comments': '#',
                'delimiter': ',',
                'dtype': float,
                'skiprows': 0
            }

        if kwds['delimiter']:
            self.delimiter = kwds['delimiter']
        else:
            self.delimiter = ' '
        self.comments = kwds['comments']
        self.skiprows = kwds['skiprows']
        self.names = list(kwds['dtype']['names'])
        self.formats = list(kwds['dtype']['formats'])

        self.columns = [
            ListItem(name='Column %i:' % (i + 1),
                     parent=self,
                     column_number=i,
                     my_name=val) for i, val in enumerate(self.names)
        ]
        self.load_data()
예제 #18
0
    def test_API(self):
        fo = open(TESTFN, 'wb')
        fo.write(''' "A", "B", "C"
                     1, 2, 3.2
                     7, 4, 1.87''')
        fo.close()

        s = Sniff(TESTFN)
        self.assertEqual(s.comments(), '#')
        self.assertEqual(s.delimiter(), ',')
        self.assertEqual(s.skiprows(), 1)
        self.assertEqual(s.dtype(), {'names': ('A', 'B', 'C'),
                                     'formats': (float, float, float)})
        x = s.loadtxt()
        y = array([(1.0, 2.0, 3.20),
                   (7.0, 4.0, 1.87)],
                  dtype=[('A', float), ('B', float), ('C', float)])
        self.assertNamedClose(x, y)

        y = loadtxt(TESTFN, **s.kwds())
        self.assertNamedClose(x, y)

        y = loadtxt_unknown(TESTFN)
        self.assertNamedClose(x, y)

        d = array2dict(y)
        self.assertEqual(type(d), type({}))
        self.assertAllClose(x['A'], [1, 7])
        self.assertAllClose(x['B'], [2, 4])
        self.assertAllClose(x['C'], [3.2, 1.87])
예제 #19
0
    def test_API(self):
        fo = tempfile.mktemp()
        with open(fo, "w") as f:
            f.write(
                """ "A", "B", "C"
                         1, 2, 3.2
                         7, 4, 1.87"""
            )

        s = Sniff(fo)
        self.assertEqual(s.comments(), "#")
        self.assertEqual(s.delimiter(), ",")
        self.assertEqual(s.skiprows(), 1)
        self.assertEqual(s.dtype(), {"names": ("A", "B", "C"), "formats": (float, float, float)})
        x = s.loadtxt()
        y = array([(1.0, 2.0, 3.20), (7.0, 4.0, 1.87)], dtype=[("A", float), ("B", float), ("C", float)])
        self.assertNamedClose(x, y)

        y = loadtxt(fo, **s.kwds())
        self.assertNamedClose(x, y)

        y = loadtxt_unknown(fo)
        self.assertNamedClose(x, y)

        d = array2dict(y)
        self.assertEqual(type(d), type({}))
        self.assertAllClose(x["A"], [1, 7])
        self.assertAllClose(x["B"], [2, 4])
        self.assertAllClose(x["C"], [3.2, 1.87])
예제 #20
0
    def test_comment(self):
        fo = open(TESTFN, 'wb')
        fo.write('''
        % "A"  "B"  "C"
           1    2   4.2   % comment''')
        fo.close()

        s = Sniff(TESTFN)
        self.assertEqual(
            s.kwds(),
            {
                'dtype': {
                    'names': ('A', 'B', 'C'),
                    'formats': (float, float, float)
                },
                'delimiter': None,
                'skiprows': 0,  # FIXME
                'comments': '%'
            })
예제 #21
0
    def test_comment(self):
        fo = tempfile.mktemp()
        with open(fo, "w") as f:
            f.write(
                """
            % "A"  "B"  "C"
               1    2   4.2   % comment"""
            )

        s = Sniff(fo)
        self.assertEqual(
            s.kwds(),
            {
                "dtype": {"names": ("A", "B", "C"), "formats": (float, float, float)},
                "delimiter": None,
                "skiprows": 0,  # FIXME
                "comments": "%",
            },
        )