How to use the agate.type_tester.TypeTester function in agate

To help you get started, we’ve selected a few agate examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github etalab / csvapi / csvapi / type_tester.py View on Github external
def agate_tester():
    # Override the original list of type checkers present in agate
    # to detect types.
    #
    # Original list here:
    # https://github.com/wireservice/agate/blob/e3078dca8b3566e8408e65981f79918c2f36f9fe/agate/type_tester.py#L64-L71
    return TypeTester(
        types=[
            Boolean(),
            SirenSiret(),
            Number(),
            Time(),
            TimeDelta(),
            Date(),
            DateTime(),
            Text(),
        ]
github wireservice / agate / tests / test_table / test_denormalize.py View on Github external
def test_denormalize_column_type_tester(self):
        table = Table(self.rows, self.column_names, self.column_types)

        normalized_table = table.denormalize(None, 'property', 'value', column_types=TypeTester(force={'gender': Text()}))

        # NB: value has been overwritten
        normal_rows = (
            ('male', 24),
        )

        self.assertRows(normalized_table, normal_rows)
        self.assertColumnNames(normalized_table, ['gender', 'age'])
        self.assertColumnTypes(normalized_table, [Text, Number])
github wireservice / agate / tests / test_table / __init__.py View on Github external
def test_from_json_no_type_tester(self):
        tester = TypeTester(limit=0)

        table = Table.from_json('examples/test.json', column_types=tester)

        self.assertColumnTypes(table, [Text, Text, Text, Text, Text, Text])
github wireservice / agate / tests / test_type_tester.py View on Github external
def test_types_number_locale(self):
        rows = [
            ('1,7',),
            ('200.000.000',),
            ('',)
        ]

        tester = TypeTester(types=[Number(locale='de_DE'), Text()])
        inferred = tester.run(rows, ['one'])

        self.assertIsInstance(inferred[0], Number)
        self.assertEqual(str(inferred[0].locale), 'de_DE')
github wireservice / agate / tests / test_type_tester.py View on Github external
def setUp(self):
        self.tester = TypeTester()
github wireservice / agate / tests / test_type_tester.py View on Github external
def test_force_type(self):
        rows = [
            ('1.7',),
            ('200000000',),
            ('',)
        ]

        tester = TypeTester(force={
            'one': Text()
        })

        inferred = tester.run(rows, ['one'])

        self.assertIsInstance(inferred[0], Text)
github wireservice / agate / agate / table / denormalize.py View on Github external
if len(k) == 1:
            row_names.append(k[0])
        else:
            row_names.append(k)

        for f in field_names:
            if f in v:
                row.append(v[f])
            else:
                row.append(default_value)

        new_rows.append(Row(row, new_column_names))

    key_column_types = [self.column_types[self.column_names.index(name)] for name in key]

    if column_types is None or isinstance(column_types, TypeTester):
        tester = TypeTester() if column_types is None else column_types
        force_update = dict(zip(key, key_column_types))
        force_update.update(tester._force)
        tester._force = force_update

        new_column_types = tester.run(new_rows, new_column_names)
    else:
        new_column_types = key_column_types + list(column_types)

    return Table(new_rows, new_column_names, new_column_types, row_names=row_names)
github wireservice / agate / agate / table / __init__.py View on Github external
self._column_names = tuple(utils.letter_name(i) for i in range(len(data[0])))
            warnings.warn('Column names not specified. "%s" will be used as names.' % str(self._column_names), RuntimeWarning, stacklevel=2)
        else:
            self._column_names = []

        len_column_names = len(self._column_names)

        # Validate column_types
        if column_types is None:
            column_types = TypeTester()
        elif isinstance(column_types, dict):
            for v in column_types.values():
                if not isinstance(v, DataType):
                    raise ValueError('Column types must be instances of DataType.')

            column_types = TypeTester(force=column_types)
        elif not isinstance(column_types, TypeTester):
            for column_type in column_types:
                if not isinstance(column_type, DataType):
                    raise ValueError('Column types must be instances of DataType.')

        if isinstance(column_types, TypeTester):
            self._column_types = column_types.run(data, self._column_names)
        else:
            self._column_types = tuple(column_types)

        if len_column_names != len(self._column_types):
            raise ValueError('column_names and column_types must be the same length.')

        if row_names:
            computed_row_names = []
github wireservice / agate / agate / table / denormalize.py View on Github external
row_names.append(k[0])
        else:
            row_names.append(k)

        for f in field_names:
            if f in v:
                row.append(v[f])
            else:
                row.append(default_value)

        new_rows.append(Row(row, new_column_names))

    key_column_types = [self.column_types[self.column_names.index(name)] for name in key]

    if column_types is None or isinstance(column_types, TypeTester):
        tester = TypeTester() if column_types is None else column_types
        force_update = dict(zip(key, key_column_types))
        force_update.update(tester._force)
        tester._force = force_update

        new_column_types = tester.run(new_rows, new_column_names)
    else:
        new_column_types = key_column_types + list(column_types)

    return Table(new_rows, new_column_names, new_column_types, row_names=row_names)