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

Python normalize.FieldSelector类代码示例

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

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



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

示例1: test_dict

    def test_dict(self):
        from normalize.coll import dict_of
        from testclasses import Person
        Rolodeck = dict_of(Person)

        deck = Rolodeck({
            "bob": Person(id=123, name="Bob"),
            "peter": Person(id=124, name="Peter"),
            "eve": Person(id=125, name="Steve"),
        })

        fs1 = FieldSelector(["bob", "id"])
        self.assertEqual(fs1.get(deck), 123)
        self.assertRaisesRegexp(
            ValueError, r'id is required', fs1.delete, deck,
        )

        fs2 = FieldSelector(["cuthbert", "id"])
        fs2.post(deck, 923)
        self.assertEqual(deck['cuthbert'].id, 923)
        FieldSelector(["ruth"]).post(deck, {"id": 523, "name": "Ruth"})
        self.assertEqual(deck['ruth'].name, "Ruth")

        FieldSelector(["bob"]).delete(deck)
        self.assertNotIn("bob", deck)

        FieldSelector([None]).delete(deck)
        self.assertEqual(deck, {})
开发者ID:hearsaycorp,项目名称:normalize,代码行数:28,代码来源:test_selector.py


示例2: test_add_full_collection

    def test_add_full_collection(self):
        fs = FieldSelector(["foo", "bar"])
        self.assertEqual(fs.selectors, ["foo", "bar"])

        fs.add_full_collection()
        self.assertEqual(fs.selectors, ["foo", "bar", None])
        self.assertEqual(str(fs), "<FieldSelector: .foo.bar[*]>")
开发者ID:danielcash,项目名称:normalize,代码行数:7,代码来源:test_selector.py


示例3: test_subscripting

 def test_subscripting(self):
     fs = FieldSelector(("somewhere", "over", "the", "rainbow"))
     self.assertEqual(len(fs), 4)
     self.assertEqual(fs[-1], "rainbow")
     shorter = fs[:-1]
     self.assertIsInstance(shorter, FieldSelector)
     self.assertEqual(len(shorter), 3)
     self.assertTrue(fs.startswith(shorter))
开发者ID:danielcash,项目名称:normalize,代码行数:8,代码来源:test_selector.py


示例4: test_add_index

    def test_add_index(self):
        fs = FieldSelector(["foo", "bar"])
        self.assertEqual(fs.selectors, ["foo", "bar"])

        fs.add_index(0)
        self.assertEqual(fs.selectors, ["foo", "bar", 0])

        with self.assertRaisesRegexp(
            ValueError, "index must be an int or a long"
        ):
            fs.add_index(0.0)

        self.assertEqual(fs.selectors, ["foo", "bar", 0])
开发者ID:danielcash,项目名称:normalize,代码行数:13,代码来源:test_selector.py


示例5: test_add_property

    def test_add_property(self):
        fs = FieldSelector(["foo", "bar"])
        self.assertEqual(fs.selectors, ["foo", "bar"])

        fs.add_property("prop_name")
        self.assertEqual(fs.selectors, ["foo", "bar", "prop_name"])

        with self.assertRaisesRegexp(
            ValueError, "properties must be specified by their string name",
        ):
            fs.add_property({})

        self.assertEqual(fs.selectors, ["foo", "bar", "prop_name"])
开发者ID:danielcash,项目名称:normalize,代码行数:13,代码来源:test_selector.py


示例6: test_extend

    def test_extend(self):
        fs1 = FieldSelector(["foo", "bar"])
        fs2 = FieldSelector(["hello", "world"])
        # functional extension
        self.assertEqual(
            (fs1 + fs2).selectors, ["foo", "bar", "hello", "world"],
        )
        self.assertEqual(str(fs1 + "baz"), "<FieldSelector: .foo.bar.baz>")
        self.assertEqual(str(fs1 + ["baz"]), "<FieldSelector: .foo.bar.baz>")
        self.assertEqual(str(fs1 + 7), "<FieldSelector: .foo.bar[7]>")
        fs3 = fs1.extend(fs2)

        # Verify that extend works
        self.assertEqual(fs1.selectors, ["foo", "bar", "hello", "world"])
        # Verify that extend chaining works
        self.assertEqual(fs1, fs3)
开发者ID:danielcash,项目名称:normalize,代码行数:16,代码来源:test_selector.py


示例7: test_put

    def test_put(self):
        record = MockJsonRecord()

        # Test invalid FieldSelector
        fs = FieldSelector(["name"])
        fs.put(record, "pass")
        # self.assertEqual(record.raw_data, {"children": [], 'name': 'pass'})

        record = MockComplexJsonRecord()

        # Test creation of attribute
        fs = FieldSelector(["name"])
        fs.put(record, "Bobby")
        self.assertEqual(record.name, "Bobby")

        # Test creation of collection and sub-Record
        fs = FieldSelector(["children", 0, "name"])
        with self.assertRaises(FieldSelectorException):
            fs.put(record, "Johnny")
开发者ID:danielcash,项目名称:normalize,代码行数:19,代码来源:test_selector.py


示例8: test_path_marshal

 def test_path_marshal(self):
     for path in (
         ("foo", "bar"),
         (None, ),
         (None, "bob", 6, "frop"),
         (),
         ("Bob", "one flew over the cuckoo's nest"),
         ("Leaning", "toothpick", "syndrome", "\\/\"/"),
         (u"\u2620",)
     ):
         fs = FieldSelector(path)
         fs_path = fs.path
         fs_2 = FieldSelector.from_path(fs_path)
         self.assertEqual(fs.path, fs_2.path)
         self.assertEqual(fs.selectors, fs_2.selectors)
开发者ID:rafaelrruiz,项目名称:normalize,代码行数:15,代码来源:test_selector.py


示例9: test_post_required

    def test_post_required(self):
        class FussyRecord(Record):
            id = Property(required=True)

        class FussPot(Record):
            fuss_list = ListProperty(of=FussyRecord)
            fuss_map = DictProperty(of=FussyRecord)
            top_fuss = Property(isa=FussyRecord)

        fp = FussPot()
        fs1 = FieldSelector(("top_fuss", "id"))
        fs2 = FieldSelector(("fuss_map", "phew", "id"))
        fs3 = FieldSelector(("fuss_list", 0, "id"))
        fs1.post(fp, 1)
        fs2.post(fp, 2)
        fs3.post(fp, 3)
        expected = FussPot(
            top_fuss={"id": 1},
            fuss_map={"phew": {"id": 2}},
            fuss_list=[{"id":3}],
        )
开发者ID:RVoigtHearsay,项目名称:normalize,代码行数:21,代码来源:test_selector.py


示例10: test_post

    def test_post(self):
        record = MockComplexJsonRecord()
        fs = FieldSelector(["name"])
        fs.post(record, "Bobby")
        self.assertEqual(record.name, "Bobby")

        fs = FieldSelector(["children", 0, "name"])
        fs.post(record, "Johnny")
        self.assertEqual(record.children[0].name, "Johnny")

        # Test addition to collection
        fs = FieldSelector(["children", 1, "name"])
        fs.post(record, "Susan")
        self.assertEqual(record.children[1].name, "Susan")

        # Test invalid addition to collection
        fs = FieldSelector(["children", 9999, "name"])
        with self.assertRaisesRegexp(
            ValueError, "FieldSelector set out of order"
        ):
            fs.post(record, "Joker")

        # Test create of sub-Record
        fs = FieldSelector(["nested", "name"])
        fs.put(record, "Nested")
        self.assertEqual(record.nested.name, "Nested")
开发者ID:danielcash,项目名称:normalize,代码行数:26,代码来源:test_selector.py


示例11: test_get

    def test_get(self):
        record = MockJsonRecord(
            {
                "age": 5,
                "children": [{"name": "foo"}, {"name": "bar"}],
                "name": "case1",
            }
        )
        # test valid field selectors
        fs = FieldSelector(["name"])
        self.assertEqual(fs.get(record), "case1")
        fs = FieldSelector(["age"])
        self.assertEqual(fs.get(record), 5)
        fs = FieldSelector(["children"])
        self.assertEqual(len(fs.get(record)), 2)
        fs = FieldSelector(["children", 1])
        self.assertEqual(fs.get(record).name, "bar")
        self.assertEqual(fs.get(record), MockChildRecord({"name": "bar"}))
        fs = FieldSelector(["children", 1, "name"])
        self.assertEqual(fs.get(record), "bar")
        fs = FieldSelector(["children", None, "name"])
        self.assertEqual(fs.get(record), ["foo", "bar"])

        # test invalid selectors
        fs = FieldSelector(["bad_name"])  # bad property name
        with self.assertRaisesRegexp(
            FieldSelectorException, "Could not find property specified "
            "by name: bad_name"
        ):
            fs.get(record)
        fs = FieldSelector(["children", 10])  # bad index
        with self.assertRaisesRegexp(
            FieldSelectorException, "Could not find Record specified by "
            "index: 10"
        ):
            fs.get(record)
        # bad nested property name
        fs = FieldSelector(["children", 1, "bad_name"])
        with self.assertRaisesRegexp(
            FieldSelectorException, "Could not find property specified "
            "by name: bad_name"
        ):
            fs.get(record)

        # bad property name
        fs = FieldSelector(["bad_name"])
        with self.assertRaises(AttributeError):
            fs.get(record)
        # bad index
        fs = FieldSelector(["children", 10])
        with self.assertRaises(KeyError):
            fs.get(record)
        # bad nested property name
        fs = FieldSelector(
            ["children", 1, "bad_name"],
        )
        with self.assertRaises(AttributeError):
            fs.get(record)
开发者ID:danielcash,项目名称:normalize,代码行数:58,代码来源:test_selector.py


示例12: test_delete

    def test_delete(self):

        class MyObj(Record):
            foo = Property()
            bar = Property()

        class OtherObj(Record):
            objs = ListProperty(of=MyObj)

        mo = MyObj(foo="bar")
        fs1 = FieldSelector(["foo"])
        self.assertEqual(fs1.get(mo), "bar")
        fs1.delete(mo)
        self.assertRaises(AttributeError, fs1.get, mo)

        oo = OtherObj(objs=[{"foo": "bar"}, {"foo": "frop"}])

        fs2 = FieldSelector(["objs", 0, "foo"])
        self.assertEqual(fs2.get(oo), "bar")
        fs2.delete(oo)
        self.assertRaises(AttributeError, fs2.get, mo)
        fs2.put(oo, "hey")

        fs3 = FieldSelector(["objs", None, "foo"])
        self.assertEqual(fs3.get(oo), ["hey", "frop"])
        fs3.delete(oo)
        self.assertEqual(oo, OtherObj(objs=[{}, {}]))

        oo = OtherObj(objs=[{"foo": "baz"}, {"bar": "quux"}])
        self.assertEqual(fs3.get(oo), ["baz", None])
        fs3.delete(oo)
        self.assertEqual(oo, OtherObj(objs=[{}, {"bar": "quux"}]))

        fs4 = FieldSelector(["objs", None, "bar", "foo"])
        self.assertRaises(FieldSelectorException, fs4.get, oo)

        oo = OtherObj(objs=[{"foo": "baz"}, {"bar": MyObj(foo="bob")}])
        self.assertEqual(fs4.get(oo), [None, "bob"])
        fs4.delete(oo)
        self.assertRaises(FieldSelectorException, fs4.get, oo)

        fs5 = FieldSelector(["objs", 1])
        fs5.delete(oo)
        self.assertEqual(oo, OtherObj(objs=[{"foo": "baz"}]))

        fs6 = FieldSelector(["objs", None])
        fs6.delete(oo)
        self.assertEqual(oo, OtherObj(objs=[]))
开发者ID:hearsaycorp,项目名称:normalize,代码行数:48,代码来源:test_selector.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Python selector.MultiFieldSelector类代码示例发布时间:2022-05-27
下一篇:
Python normalize.normalize函数代码示例发布时间: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