当前位置: 首页>>代码示例>>Python>>正文


Python smartcsv.reader函数代码示例

本文整理汇总了Python中smartcsv.reader函数的典型用法代码示例。如果您正苦于以下问题:Python reader函数的具体用法?Python reader怎么用?Python reader使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了reader函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_required_column_with_data_is_transformed

    def test_required_column_with_data_is_transformed(self):
        """Should apply the transformation to the value"""
        iphone_data = {"title": "iPhone 5C", "price": "799"}
        ipad_data = {"title": "iPad mini", "price": "699"}
        csv_data = """
title,price
{iphone_row}
{ipad_row}
""".format(
            iphone_row="{title},{price}".format(**iphone_data), ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(
            StringIO(csv_data),
            columns=[
                {"name": "title", "required": True},
                {"name": "price", "required": True, "transform": lambda x: Decimal(x)},
            ],
        )

        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, {"title": "iPhone 5C", "price": Decimal("799")})
        self.assertModelsEquals(ipad, {"title": "iPad mini", "price": Decimal("699")})
开发者ID:jangeador,项目名称:smartcsv,代码行数:28,代码来源:test_column_value_transformation.py

示例2: test_error_exception_is_reported_without_fail_fast

    def test_error_exception_is_reported_without_fail_fast(self):
        """reader.errors should contain the exception that happenend with the value transformation"""
        iphone_data = {"title": "iPhone 5C", "price": "INVALID"}
        ipad_data = {"title": "iPad mini", "price": "699"}
        iphone_row = "{title},{price}".format(**iphone_data)

        csv_data = """
title,price
{iphone_row}
{ipad_row}
""".format(
            iphone_row=iphone_row, ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(
            StringIO(csv_data),
            columns=[
                {"name": "title", "required": True},
                {"name": "price", "required": True, "transform": lambda x: Decimal(x)},
            ],
            fail_fast=False,
        )

        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))
        self.assertTrue(isinstance(ipad, dict))
        self.assertModelsEquals(ipad, {"title": "iPad mini", "price": Decimal("699")})

        self.assertTrue(reader.errors is not None)
        self.assertTrue("rows" in reader.errors)
        self.assertEqual(len(reader.errors["rows"]), 1)  # 1 row failing

        self.assertRowError(reader.errors, iphone_row, 0, "transform")
开发者ID:jangeador,项目名称:smartcsv,代码行数:33,代码来源:test_column_value_transformation.py

示例3: test_not_required_value_is_provided

    def test_not_required_value_is_provided(self):
        """Shouldn't fail and should have an the correct value"""
        iphone_data = {
            'title': 'iPhone 5C',
            'price': '799'
        }
        ipad_data = {
            'title': 'iPad mini',
            'price': '699'
        }
        csv_data = """
title,price
{iphone_row}
{ipad_row}
        """.format(
            iphone_row="{title},{price}".format(**iphone_data),
            ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': False},
            {'name': 'price', 'required': False},
        ])

        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, iphone_data)
        self.assertModelsEquals(ipad, ipad_data)
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:32,代码来源:test_required_column.py

示例4: test_required_value_is_missing_and_dont_fail_fast

    def test_required_value_is_missing_and_dont_fail_fast(self):
        """Shouldn't fail fast, instead report errors on reader.errors"""
        iphone_data = {
            'title': 'iPhone 5C'
        }
        ipad_data = {
            'title': 'iPad mini',
            'price': '699'
        }
        iphone_row = "{title},".format(**iphone_data)
        csv_data = """
title,price
{iphone_row}
{ipad_row}
        """.format(
            iphone_row=iphone_row,
            ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': True},
            {'name': 'price', 'required': True},
        ], fail_fast=False)

        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))
        self.assertTrue(isinstance(ipad, dict))
        self.assertModelsEquals(ipad, ipad_data)

        self.assertTrue(reader.errors is not None)
        self.assertTrue('rows' in reader.errors)
        self.assertEqual(len(reader.errors['rows']), 1)  # 1 row failing

        self.assertRowError(
            reader.errors, iphone_row, 0, 'price')
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:35,代码来源:test_required_column.py

示例5: test_default_value_is_not_transformed

    def test_default_value_is_not_transformed(self):
        """Shouldn't apply no transformation if the value is missing and the default value is being used"""
        iphone_data = {"title": "iPhone 5C", "price": ""}
        ipad_data = {"title": "iPad mini", "price": ""}
        csv_data = """
title,price
{iphone_row}
{ipad_row}
""".format(
            iphone_row="{title},{price}".format(**iphone_data), ipad_row="{title},{price}".format(**ipad_data)
        )
        mocked_validator = mock.MagicMock(return_value=True)
        reader = smartcsv.reader(
            StringIO(csv_data),
            columns=[
                {"name": "title", "required": True},
                {"name": "price", "required": False, "transform": mocked_validator, "default": 899},
            ],
        )

        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, {"title": "iPhone 5C", "price": 899})
        self.assertModelsEquals(ipad, {"title": "iPad mini", "price": 899})

        self.assertEqual(mocked_validator.call_count, 0)
开发者ID:jangeador,项目名称:smartcsv,代码行数:31,代码来源:test_column_value_transformation.py

示例6: test_column_required_and_choice_missing_with_fail_fast

    def test_column_required_and_choice_missing_with_fail_fast(self):
        """Should fail because the field is required"""
        iphone_data = {
            'title': 'iPhone 5C'
        }
        ipad_data = {
            'title': 'iPad mini',
            'currency': 'ARS'
        }
        csv_data = """
title,currency
{iphone_row}
{ipad_row}""".format(
            iphone_row="{title},".format(**iphone_data),
            ipad_row="{title},{currency}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': True},
            {
                'name': 'currency',
                'required': True,
                'choices': CURRENCY_CHOICES
            }
        ])

        try:
            next(reader)
        except InvalidCSVException as e:
            self.assertTrue(e.errors is not None)
            self.assertTrue('currency' in e.errors)
        else:
            assert False, "Shouldn't reach this state"
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:32,代码来源:test_column_choices.py

示例7: test_invalid_value_is_passed_and_exception_is_raised

    def test_invalid_value_is_passed_and_exception_is_raised(self):
        """Should not validate and raise a exception (fail_fast=True)"""
        iphone_data = {
            'title': 'iPhone 5C',
            'price': 'INVALID'
        }
        ipad_data = {
            'title': 'iPad mini',
            'price': '699'
        }
        csv_data = """
title,price
{iphone_row}
{ipad_row}""".format(
            iphone_row="{title},{price}".format(**iphone_data),
            ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': True},
            {
                'name': 'price',
                'required': True,
                'validator': is_number
            },
        ])

        try:
            next(reader)
        except InvalidCSVException as e:
            self.assertTrue(e.errors is not None)
            self.assertTrue('price' in e.errors)
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:31,代码来源:test_column_validation.py

示例8: test_column_doesnt_have_a_name

 def test_column_doesnt_have_a_name(self):
     """Should fail if a column doesn't have a name"""
     columns = [{"required": True}, {"name": "price", "required": True}]
     self.assertRaises(
         InvalidCSVColumnDefinition,
         lambda: smartcsv.reader(StringIO(CSV_DATA), columns=columns, header_included=False),
     )
开发者ID:jangeador,项目名称:smartcsv,代码行数:7,代码来源:test_model_definitions.py

示例9: test_required_column_empty_fails_even_with_default

    def test_required_column_empty_fails_even_with_default(self):
        """Should fail if the column is required and the value is empty."""
        iphone_data = {
            'title': 'iPhone 5C',
            'price': ''
        }
        ipad_data = {
            'title': 'iPad mini',
            'price': '799'
        }
        csv_data = """
title,price
{iphone_row}
{ipad_row}
""".format(
            iphone_row="{title},{price}".format(**iphone_data),
            ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': True},
            {'name': 'price', 'required': True, 'default': 999},
        ])

        try:
            next(reader)
        except InvalidCSVException as e:
            self.assertTrue(e.errors is not None)
            self.assertTrue('price' in e.errors)
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:28,代码来源:test_column_default.py

示例10: test_not_required_column_empty_returns_default_value

    def test_not_required_column_empty_returns_default_value(self):
        """Should return the default value if no other is provided"""
        iphone_data = {
            'title': 'iPhone 5C',
            'price': ''
        }
        ipad_data = {
            'title': 'iPad mini',
            'price': '799'
        }
        csv_data = """
title,price
{iphone_row}
{ipad_row}
""".format(
            iphone_row="{title},{price}".format(**iphone_data),
            ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': True},
            {'name': 'price', 'required': False, 'default': 999},
        ])

        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, {
            'title': 'iPhone 5C',
            'price': 999
        })
        self.assertModelsEquals(ipad, ipad_data)
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:35,代码来源:test_column_default.py

示例11: test_valid_and_value_transformed_with_only_required_data

    def test_valid_and_value_transformed_with_only_required_data(self):
        """Should transform all values with only required data present"""
        csv_data = """
title,currency,price,in_stock
iPhone 5c blue,USD,799,
iPad mini,USD,699,
"""
        reader = smartcsv.reader(
            StringIO(csv_data), columns=COLUMNS_WITH_VALUE_TRANSFORMATIONS)
        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(
            isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, {
            'title': 'iPhone 5c blue',
            'currency': 'USD',
            'price': Decimal('799'),
            'in_stock': ''
        })
        self.assertModelsEquals(ipad, {
            'title': 'iPad mini',
            'currency': 'USD',
            'price': Decimal('699'),
            'in_stock': ''
        })
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:29,代码来源:test_value_transformations.py

示例12: test_valid_data_and_skip_lines_with_header

    def test_valid_data_and_skip_lines_with_header(self):
        """Should skip the first N lines and parse data ok with header"""
        csv_data = """
Generated by Autobot 2000 - V0.1.2
----------
This next is intentionally left blank

-- Beginning of content
title,category,subcategory,currency,price,url,image_url
{iphone_data}
{ipad_data}
        """.format(
            iphone_data=VALID_TEMPLATE_STR.format(**IPHONE_DATA),
            ipad_data=VALID_TEMPLATE_STR.format(**IPAD_DATA),
        )
        reader = smartcsv.reader(
            StringIO(csv_data), columns=COLUMNS_1, skip_lines=6)

        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(
            isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, IPHONE_DATA)
        self.assertModelsEquals(ipad, IPAD_DATA)
开发者ID:ilmesi,项目名称:smartcsv,代码行数:28,代码来源:test_csv_skip_lines.py

示例13: test_default_value_is_empty

 def test_default_value_is_empty(self):
     """Should fail a default value is empty"""
     columns = [{"name": "title", "required": True}, {"name": "price", "required": True, "default": ""}]
     self.assertRaises(
         InvalidCSVColumnDefinition,
         lambda: smartcsv.reader(StringIO(CSV_DATA), columns=columns, header_included=False),
     )
开发者ID:jangeador,项目名称:smartcsv,代码行数:7,代码来源:test_model_definitions.py

示例14: handle

    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError('Usage: importproducts ' + self.args)

        filepath = args[0]

        print 'Importing persons from {0}...'.format(filepath)

        stats = {'imported': 0, 'skipped': 0}
        with open(filepath) as f:
            reader = smartcsv.reader(f, columns=COLUMNS, fail_fast=False)

            for person in reader:
                try:
                    Profile.objects.create(
                        email=person['email'],
                        lg_full_name=person['name'],
                        lg_timezone=person['timezone']
                    )
                except IntegrityError:
                    stats['skipped'] += 1
                else:
                    stats['imported'] += 1
        print ('Finished with stats: '
               'imported {imported}, skipped {skipped}').format(**stats)
开发者ID:adrianmoisey,项目名称:rmotr-sis,代码行数:25,代码来源:importpersons.py

示例15: test_valid_choice_is_provided

    def test_valid_choice_is_provided(self):
        """Should not fail and have the value of the selected choice"""
        iphone_data = {
            'title': 'iPhone 5C',
            'currency': 'USD'
        }
        ipad_data = {
            'title': 'iPad mini',
            'currency': 'ARS'
        }
        csv_data = """
title,currency
{iphone_row}
{ipad_row}""".format(
            iphone_row="{title},{currency}".format(**iphone_data),
            ipad_row="{title},{currency}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': True},
            {
                'name': 'currency',
                'required': True,
                'choices': CURRENCY_CHOICES
            }
        ])

        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, iphone_data)
        self.assertModelsEquals(ipad, ipad_data)
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:35,代码来源:test_column_choices.py


注:本文中的smartcsv.reader函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。