• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python selector.MultiFieldSelector类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: test_mfs_json

    def test_mfs_json(self):
        """MultiFieldSelector can work on JsonRecordList objects"""

        class Thing(JsonRecord):
            flintstone = JsonProperty()
            element = JsonProperty()

        class Things(JsonRecordList):
            itemtype = Thing

        flintstones = ("dino", "bammbamm", "wilma", "fred")
        elements = ("Rb", "At", "Pm", "Fl")
        data = list(
            {"flintstone": x[0], "element": x[1]} for x in
            zip(flintstones, elements)
        )

        all_the_things = Things(data)

        mfs = MultiFieldSelector([None, "flintstone"])
        self.assertEqual(
            mfs.get(all_the_things).json_data(),
            list(dict(flintstone=x) for x in flintstones),
        )

        mfs = MultiFieldSelector([None, "flintstone"], [None, "element"])
        self.assertEqual(mfs.get(all_the_things), all_the_things)
开发者ID:danielcash,项目名称:normalize,代码行数:27,代码来源:test_selector.py


示例2: test_ignore_empty_and_coll

    def test_ignore_empty_and_coll(self):
        person = get_person(6, 0, 3, 4, 5)
        strip_ids_mfs = MultiFieldSelector(
            ["given_name"], ["family_name"], ["description"],
            ["friends", None, "given_name"],
            ["friends", None, "family_name"],
            ["friends", None, "description"],
        )
        filtered_person = strip_ids_mfs.get(person)

        person.description = ""
        person.friends[0].description = ""

        self.assertDifferences(
            person.diff_iter(filtered_person,
                             compare_filter=strip_ids_mfs),
            {
                "REMOVED .description",
                "REMOVED .friends[0]", "ADDED .friends[0]",
            },
        )

        self.assertDifferences(
            person.diff_iter(filtered_person,
                             compare_filter=strip_ids_mfs,
                             ignore_empty_slots=True), {},
        )
开发者ID:newtonry,项目名称:normalize,代码行数:27,代码来源:test_mfs_diff.py


示例3: test_filtered_coll_items_diff

    def test_filtered_coll_items_diff(self):
        strip_ids_mfs = MultiFieldSelector(
            ["given_name"], ["family_name"], ["date_of_birth"],
            ["friends", None, "given_name"],
            ["friends", None, "family_name"],
            ["friends", None, "date_of_birth"],
        )
        person = get_person(0, 2, 5, 6, 3)
        filtered_person = strip_ids_mfs.get(person)

        # not terribly useful!
        self.assertDifferences(
            person.diff_iter(filtered_person), {
                "REMOVED .ssn", "REMOVED .phone_number",
                "REMOVED .friends[0]", "ADDED .friends[0]",
                "REMOVED .friends[1]", "ADDED .friends[1]",
                "REMOVED .friends[2]", "ADDED .friends[2]",
                "REMOVED .friends[3]", "ADDED .friends[3]",
            },
        )

        # however, pass the filter into diff, and it gets it right!
        self.assertDifferences(
            person.diff_iter(filtered_person,
                             compare_filter=strip_ids_mfs), {},
        )

        filtered_person.friends.append(get_person(1))
        del filtered_person.friends.values[0]  # FIXME :)

        self.assertDifferences(
            person.diff_iter(filtered_person,
                             compare_filter=strip_ids_mfs),
            {"ADDED .friends[3]", "REMOVED .friends[0]"},
        )
开发者ID:newtonry,项目名称:normalize,代码行数:35,代码来源:test_mfs_diff.py


示例4: test_filtered_diff

 def test_filtered_diff(self):
     """Test that diff notices when fields are removed"""
     name_mfs = MultiFieldSelector(["given_name"], ["family_name"])
     person = get_person(1)
     filtered_person = name_mfs.get(person)
     self.assertDifferences(
         person.diff_iter(filtered_person),
         {"REMOVED .date_of_birth", "REMOVED .ssn",
          "REMOVED .phone_number"},
     )
开发者ID:newtonry,项目名称:normalize,代码行数:10,代码来源:test_mfs_diff.py


示例5: test_normalize_slot

    def test_normalize_slot(self):
        person = get_person(3, 0, 2, 4, 6)
        strip_ids_mfs = MultiFieldSelector(
            ["given_name"], ["family_name"], ["phone_number"],
            ["friends", None, "given_name"],
            ["friends", None, "family_name"],
            ["friends", None, "phone_number"],
        )
        filtered_person = strip_ids_mfs.get(person)

        class MyDiffOptions(DiffOptions):
            def normalize_slot(self, val, prop):
                if "phone" in prop.name and isinstance(val, basestring):
                    val = normalize_phone(val)
                return super(MyDiffOptions, self).normalize_slot(val, prop)

        person.phone_number = '5309225668'
        person.friends[0].phone_number = '+1 239.978.5912'

        self.assertDifferences(
            person.diff_iter(filtered_person,
                             compare_filter=strip_ids_mfs,
                             ignore_empty_slots=True),
            {
                "MODIFIED .phone_number",
                "REMOVED .friends[0]", "ADDED .friends[0]",
            },
        )

        my_options = MyDiffOptions(
            ignore_empty_slots=True,
            compare_filter=strip_ids_mfs,
        )

        self.assertDifferences(
            person.diff_iter(filtered_person, options=my_options), {},
        )

        friendless = get_person(3)

        self.assertDifferences(
            person.diff_iter(friendless, options=my_options),
            {"REMOVED .friends"},
        )

        ignore_friends = MyDiffOptions(
            ignore_empty_slots=True,
            compare_filter=MultiFieldSelector(
                ["given_name"], ["family_name"], ["phone_number"],
            )
        )

        self.assertDifferences(
            person.diff_iter(friendless, options=ignore_friends), {},
        )
开发者ID:newtonry,项目名称:normalize,代码行数:55,代码来源:test_mfs_diff.py


示例6: test_normalize_slot

    def test_normalize_slot(self):
        person = get_person(3, 0, 2, 4, 6)
        strip_ids_mfs = MultiFieldSelector(
            ["given_name"], ["family_name"], ["phone_number"],
            ["friends", None, "given_name"],
            ["friends", None, "family_name"],
            ["friends", None, "phone_number"],
        )
        filtered_person = strip_ids_mfs.get(person)

        # simplified NANP regex
        phone = re.compile(
            r"^(?:\+?1\s*(?:[.-]\s*)?)?(\d{3})\s*"
            r"(?:[.-]\s*)?(\d{3})\s*(?:[.-]\s*)?"
            r"(\d{4})"
        )

        class MyDiffOptions(DiffOptions):
            def normalize_phone(self, phoney):
                m = re.match(phone, phoney)
                if m:
                    return "(%s) %s-%s" % m.groups()
                else:
                    return phoney

            def normalize_slot(self, val, prop):
                if "phone" in prop.name and isinstance(val, basestring):
                    newval = self.normalize_phone(val)
                    if val != newval:
                        val = newval
                return super(MyDiffOptions, self).normalize_slot(val, prop)

        person.phone_number = '5309225668'
        person.friends[0].phone_number = '+1 239.978.5912'

        self.assertDifferences(
            person.diff_iter(filtered_person,
                             compare_filter=strip_ids_mfs,
                             ignore_empty_slots=True),
            {
                "MODIFIED .phone_number",
                "REMOVED .friends[0]", "ADDED .friends[0]",
            },
        )

        my_options = MyDiffOptions(
            ignore_empty_slots=True,
            compare_filter=strip_ids_mfs,
        )

        self.assertDifferences(
            person.diff_iter(filtered_person, options=my_options), {},
        )
开发者ID:ibiternas,项目名称:normalize,代码行数:53,代码来源:test_mfs_diff.py


示例7: test_filtered_coll_diff

    def test_filtered_coll_diff(self):
        name_and_friends_mfs = MultiFieldSelector(
            ["given_name"], ["family_name"],
            ["friends", 0],
            ["friends", 2],
        )
        person = get_person(0, 2, 5, 6, 3)
        filtered_person = name_and_friends_mfs.get(person)

        self.assertDifferences(
            person.diff_iter(filtered_person),
            {"REMOVED .date_of_birth", "REMOVED .ssn",
             "REMOVED .phone_number",
             "REMOVED .friends[1]", "REMOVED .friends[3]"},
        )
开发者ID:newtonry,项目名称:normalize,代码行数:15,代码来源:test_mfs_diff.py


示例8: test_mfs_apply_ops

    def test_mfs_apply_ops(self):
        from copy import deepcopy
        from testclasses import wall_one
        from normalize.diff import DiffTypes

        selectors = (
            ("owner",),
            ("posts", 0, "comments", 0, "poster"),
            ("posts", 0, "comments", 1, "content"),
        )
        required_fields = (
            ("id",),
            ("posts", 0, "edited"),
            ("posts", 0, "post_id"),
            ("posts", 0, "wall_id"),
            ("posts", 0, "comments", 0, "edited"),
            ("posts", 0, "comments", 0, "id"),
            ("posts", 0, "comments", 1, "edited"),
            ("posts", 0, "comments", 1, "id"),
        )
        deletable_mfs = MultiFieldSelector(*selectors)
        skeleton_mfs = MultiFieldSelector(*(required_fields + selectors))

        scratch_wall = deepcopy(wall_one)
        saved_fields = skeleton_mfs.get(scratch_wall)
        deletable_mfs.delete(scratch_wall)
        removed = set(
            tuple(x.base) for x in wall_one.diff_iter(scratch_wall)
            if x.diff_type == DiffTypes.REMOVED
        )
        self.assertEqual(
            removed, set(selectors),
            "MultiFieldSelector.delete() can delete named attributes",
        )

        deletable_mfs.patch(scratch_wall, saved_fields)
        self.assertFalse(
            scratch_wall.diff(wall_one),
            "MultiFieldSelector.patch() can copy named attributes",
        )

        del saved_fields.owner
        deletable_mfs.patch(scratch_wall, saved_fields)
        self.assertFalse(
            hasattr(scratch_wall, "owner"),
            "MultiFieldSelector.patch() can delete missing attributes",
        )
开发者ID:hearsaycorp,项目名称:normalize,代码行数:47,代码来源:test_selector.py


示例9: test_mfs_marshal

    def test_mfs_marshal(self):
        mfs = MultiFieldSelector(
            ["rakkk", None, "awkkkkkk"],
            ["rakkk", None, "zgruppp"],
            ["cr_r_a_a_ck", "rip"],
            ["cr_r_a_a_ck", "aiieee"],
        )

        path = mfs.path

        new_mfs = MultiFieldSelector.from_path(path)
        for fs in mfs:
            self.assertIn(fs, new_mfs)
            parts = list(fs)
            self.assertIsNotNone(parts[-1])

        for fs in new_mfs:
            self.assertIn(fs, mfs)

        self.assertEqual(len(mfs.path), len(new_mfs.path))

        for path in (".foo", ".foo[*]", ".foo.bar[*]"):
            mfs = MultiFieldSelector.from_path(path)
            self.assertEqual(mfs.path, path)

        for mfs_fs in (
            ((),),
            (("foo",),),
            ((1,), (2,)),
            ((None,)),
            (("foo", "bar", None),),
        ):
            mfs = MultiFieldSelector(*mfs_fs)
            path = mfs.path
            mfs_loop = MultiFieldSelector.from_path(path)
            self.assertEqual(mfs_loop.path, path)
            self.assertEqual(list(fs.path for fs in mfs),
                             list(fs.path for fs in mfs_loop))
开发者ID:hearsaycorp,项目名称:normalize,代码行数:38,代码来源:test_selector.py


示例10: test_mfs_marshal

    def test_mfs_marshal(self):
        mfs = MultiFieldSelector(
            ["rakkk", None, "awkkkkkk"],
            ["rakkk", None, "zgruppp"],
            ["cr_r_a_a_ck", "rip"],
            ["cr_r_a_a_ck", "aiieee"],
        )

        path = mfs.path

        new_mfs = MultiFieldSelector.from_path(path)
        for fs in mfs:
            self.assertIn(fs, new_mfs)

        for fs in new_mfs:
            self.assertIn(fs, mfs)

        self.assertEqual(len(mfs.path), len(new_mfs.path))
开发者ID:rafaelrruiz,项目名称:normalize,代码行数:18,代码来源:test_selector.py


示例11: test_multi_selector

    def test_multi_selector(self):
        selectors = set(
            (
                ("bar", ),
                ("foo", "bar", 0, "boo"),
                ("foo", "bar", 0, "hiss"),
                ("foo", "bar", 1),
            )
        )

        mfs = MultiFieldSelector(*selectors)
        emitted = set(tuple(x.selectors) for x in mfs)
        self.assertEqual(emitted, selectors)
        # match, eg <MultiFieldSelector: (.foo.bar([0](.hiss|.boo)|[1])|.bar)>
        #  but also <MultiFieldSelector: (.bar|.foo.bar([1]|[0](.boo|.hiss)))>
        regexp = re.compile(
            r"""<MultiFieldSelector:\s+\(
                (?:
                  (?: .foo.bar \(
                      (?:
                          (?: \[0\] \(
                              (?:
                                  (?: .hiss | .boo ) \|?
                              ){2} \)
                            | \[1\] ) \|?
                      ){2} \)
                    | .bar ) \|?
                ){2}
            \)>""", re.X,
        )
        self.assertRegexpMatches(str(mfs), regexp)
        mfs_dupe = eval(repr(mfs))
        emitted = set(tuple(x.selectors) for x in mfs_dupe)
        self.assertEqual(emitted, selectors)

        # test various dict-like functions
        self.assertIn("foo", mfs)
        self.assertIn("bar", mfs)
        self.assertNotIn("baz", mfs)
        self.assertIn('bar', mfs['foo'])
        self.assertIn(0, mfs['foo']['bar'])
        self.assertIn('hiss', mfs['foo']['bar'][0])
        self.assertNotIn('miss', mfs['foo']['bar'][0])
        self.assertIn('baz', mfs['bar'])
        self.assertIn('baz', mfs['bar']['frop']['quux']['fred'])

        # if you add a higher level selector, then more specific paths
        # disappear from the MFS
        mfs2 = MultiFieldSelector(mfs, ["foo", "bar"])
        emitted = set(tuple(x.selectors) for x in mfs2)
        self.assertEqual(emitted, set((("bar",), ("foo", "bar"))))

        data = {
            "bar": [1, 2, 3],
            "foo": {
                "bar": [
                    {"boo": "waa", "frop": "quux"},
                    {"waldo": "grault"},
                    {"fubar": "corge"},
                ],
            },
        }
        selected = mfs.get(data)
        self.assertEqual(
            selected, {
                "bar": [1, 2, 3],
                "foo": {
                    "bar": [
                        {"boo": "waa"},
                        {"waldo": "grault"},
                    ],
                },
            }
        )

        class Octothorpe(Record):
            name = Property()
            boo = Property()
            hiss = Property()

        class Caret(Record):
            bar = ListProperty(of=Octothorpe)

        class Pilcrow(Record):
            bar = ListProperty(of=Octothorpe)
            foo = Property(isa=Caret)
            baz = Property()

        full = Pilcrow(
            bar=[dict(name="Heffalump"), dict(name="Uncle Robert")],
            foo=dict(bar=[dict(name="Owl", hiss="Hunny Bee"),
                          dict(name="Piglet")]),
            baz="Wizzle",
        )
        filtered = mfs.get(full)
        expected = Pilcrow(
            bar=[dict(name="Heffalump"), dict(name="Uncle Robert")],
            foo=dict(bar=[dict(hiss="Hunny Bee"),
                          dict(name="Piglet")]),
        )
#.........这里部分代码省略.........
开发者ID:danielcash,项目名称:normalize,代码行数:101,代码来源:test_selector.py


示例12: test_complete_mfs

 def test_complete_mfs(self):
     complete_mfs = MultiFieldSelector.complete_mfs()
     fses = list(complete_mfs)
     self.assertEqual(len(fses), 1)
     self.assertEqual(fses[0].path, "[*]")
开发者ID:hearsaycorp,项目名称:normalize,代码行数:5,代码来源:test_selector.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python tests.RecipeFactory类代码示例发布时间:2022-05-27
下一篇:
Python normalize.FieldSelector类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap