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


Python ElementFactory.from_json方法代码示例

本文整理汇总了Python中gatherer.element.ElementFactory.from_json方法的典型用法代码示例。如果您正苦于以下问题:Python ElementFactory.from_json方法的具体用法?Python ElementFactory.from_json怎么用?Python ElementFactory.from_json使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gatherer.element.ElementFactory的用法示例。


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

示例1: test_all_child_single_optional_fail

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_all_child_single_optional_fail(self):
        """
        When an Element is optional, its rules and all descendent Element's
        rules must be considered optional
        """
        all_copy = copy.deepcopy(ALL_ELEMENT)
        single_copy = copy.deepcopy(SINGLE_ELEMENT)
        single_copy["optional"] = True
        all_copy["children"].append(single_copy)
        ele = ElementFactory.from_json(all_copy)
        flattened = flatten_element(ele)

        optional_outputs = [
            {
                "items": [
                    {
                        "title": "Foundation",
                        "url": "http://www.isaacasimov.com/foundation"
                    }
                ]
            },
            {},
        ]
        for out in optional_outputs:
            self.assertIsNotNone(differences(out, flattened))
开发者ID:pshrmn,项目名称:gatherer,代码行数:27,代码来源:test_differences.py

示例2: test_all_child_all_optional_fail

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_all_child_all_optional_fail(self):
        all_copy = copy.deepcopy(ALL_ELEMENT)
        alt_all_copy = copy.deepcopy(ALT_ALL_ELEMENT)
        alt_all_copy["optional"] = True
        all_copy["children"].append(alt_all_copy)
        ele = ElementFactory.from_json(all_copy)
        flattened = flatten_element(ele)

        bad_outputs = [
            {
                "items": [
                    {
                        "paragraphs": [
                            {
                                "description": "foo"
                            },
                            {
                                "description": "bar"
                            }
                        ]
                    }
                ]
            },
            {}
        ]

        for o in bad_outputs:
            self.assertIsNotNone(differences(o, flattened))
开发者ID:pshrmn,项目名称:gatherer,代码行数:30,代码来源:test_differences.py

示例3: test_all_child_single

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_all_child_single(self):
        """
        When a SingleElement is nested within an AllELement
        (or RangeElement), all of its rules will be added to the
        dict of the AllElement's rules.
        """
        all_copy = copy.deepcopy(ALL_ELEMENT)
        single_copy = copy.deepcopy(SINGLE_ELEMENT)
        all_copy["children"].append(single_copy)
        ele = ElementFactory.from_json(all_copy)
        flattened = flatten_element(ele)

        output = {
            "items": [
                {
                    "count": 7,
                    "title": "Nightfall",
                    "url": "http://www.isaacasimov.com/nightfall"
                },
                {
                    "count": 12,
                    "title": "Foundation",
                    "url": "http://www.isaacasimov.com/foundation"
                }
            ]
        }

        self.assertIsNone(differences(output, flattened))
开发者ID:pshrmn,项目名称:gatherer,代码行数:30,代码来源:test_differences.py

示例4: test_single_from_json

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
 def test_single_from_json(self):
     e = ElementFactory.from_json(SINGLE_JSON)
     self.assertIsNotNone(e)
     self.assertIsInstance(e, SingleElement)
     # test type of child
     first_child = e.children[0]
     self.assertIsInstance(first_child, SingleElement)
开发者ID:pshrmn,项目名称:gatherer,代码行数:9,代码来源:test_element.py

示例5: test_single_child_all

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_single_child_all(self):
        """
        when an AllElement (or RangeElement) is nested within
        a SingleElement, a key/value pair with the AllElement's
        spec name and a dict containing it (and its children's)
        rules will be created
        """
        all_copy = copy.deepcopy(ALL_ELEMENT)
        single_copy = copy.deepcopy(SINGLE_ELEMENT)
        single_copy["children"].append(all_copy)
        ele = ElementFactory.from_json(single_copy)
        flattened = flatten_element(ele)

        output = {
            "title": "Nightfall",
            "url": "http://www.isaacasimov.com/nightfall",
            "items": [
                {
                    "count": 7,
                },
                {
                    "count": 12,
                }
            ]
        }

        self.assertIsNone(differences(output, flattened))
开发者ID:pshrmn,项目名称:gatherer,代码行数:29,代码来源:test_differences.py

示例6: test_single_child_all_optional_fail

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_single_child_all_optional_fail(self):
        all_copy = copy.deepcopy(ALL_ELEMENT)
        single_copy = copy.deepcopy(SINGLE_ELEMENT)
        single_copy["children"].append(all_copy)
        all_copy["optional"] = True
        ele = ElementFactory.from_json(single_copy)
        flattened = flatten_element(ele)

        bad_outputs = [
            {
                "url": "http://www.isaacasimov.com/nightfall",
                "items": [
                    {
                        "count": 7,
                    },
                    {
                        "count": 12,
                    }
                ]
            },
            {
                "title": "Nightfall",
                "items": [
                    {
                        "count": 7,
                    },
                    {
                        "count": 12,
                    }
                ]
            }
        ]

        for o in bad_outputs:
            self.assertIsNotNone(differences(o, flattened))
开发者ID:pshrmn,项目名称:gatherer,代码行数:37,代码来源:test_differences.py

示例7: test_range_optional

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_range_optional(self):
        """
        the RangeElement flattens the same as an AllElement
        """
        range_copy = copy.deepcopy(RANGE_ELEMENT)
        range_copy["optional"] = True
        ele = ElementFactory.from_json(range_copy)
        flattened = flatten_element(ele)

        # "items", dict, True
        items_tuple = flattened.get("items")
        items, items_optional = items_tuple
        self.assertIsInstance(items, dict)
        self.assertEqual(items_optional, True)

        expected_item_keys = [
            ("count", int, True)
        ]

        for item in expected_item_keys:
            name, expected_type, expected_optional = item
            rule = items.get(name)
            self.assertIsNotNone(rule)

            _type, optional = rule
            self.assertIs(_type, expected_type)
            self.assertEqual(optional, expected_optional)
开发者ID:pshrmn,项目名称:gatherer,代码行数:29,代码来源:test_flatten.py

示例8: test_all_child_single_optional

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_all_child_single_optional(self):
        all_copy = copy.deepcopy(ALL_ELEMENT)
        single_copy = copy.deepcopy(SINGLE_ELEMENT)
        all_copy["children"].append(single_copy)
        single_copy["optional"] = True
        ele = ElementFactory.from_json(all_copy)
        flattened = flatten_element(ele)

        # "items", dict, False
        items_tuple = flattened.get("items")
        items, items_optional = items_tuple
        self.assertIsInstance(items, dict)
        self.assertEqual(items_optional, False)

        expected_item_keys = [
            ("count", int, False),
            ("title", str, True),
            ("url", str, True)
        ]

        for item in expected_item_keys:
            name, expected_type, expected_optional = item
            rule = items.get(name)
            self.assertIsNotNone(rule)

            _type, optional = rule
            self.assertIs(_type, expected_type)
            self.assertEqual(optional, expected_optional)
开发者ID:pshrmn,项目名称:gatherer,代码行数:30,代码来源:test_flatten.py

示例9: test_all_optional

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_all_optional(self):
        """
        for an optional AllElement, the dict for it
        will be optional as will all rules from it and
        its children
        """
        all_copy = copy.deepcopy(ALL_ELEMENT)
        all_copy["optional"] = True
        ele = ElementFactory.from_json(all_copy)
        flattened = flatten_element(ele)

        # "items", dict, True
        items_tuple = flattened.get("items")
        items, items_optional = items_tuple
        self.assertIsInstance(items, dict)
        self.assertEqual(items_optional, True)

        expected_item_keys = [
            ("count", int, True)
        ]

        for item in expected_item_keys:
            name, expected_type, expected_optional = item
            rule = items.get(name)
            self.assertIsNotNone(rule)

            _type, optional = rule
            self.assertIs(_type, expected_type)
            self.assertEqual(optional, expected_optional)
开发者ID:pshrmn,项目名称:gatherer,代码行数:31,代码来源:test_flatten.py

示例10: test_single_element

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_single_element(self):
        single_copy = copy.deepcopy(SINGLE_ELEMENT)
        ele = ElementFactory.from_json(single_copy)
        flattened = flatten_element(ele)

        output = {
            "title": "Nightfall",
            "url": "http://www.isaacasimov.com/nightfall"
        }
        self.assertIsNone(differences(output, flattened))
开发者ID:pshrmn,项目名称:gatherer,代码行数:12,代码来源:test_differences.py

示例11: test_all_element

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_all_element(self):
        all_copy = copy.deepcopy(ALL_ELEMENT)
        ele = ElementFactory.from_json(all_copy)
        flattened = flatten_element(ele)

        output = {
            "items": [
                {"count": 7},
                {"count": 12}
            ]
        }
        self.assertIsNone(differences(output, flattened))
开发者ID:pshrmn,项目名称:gatherer,代码行数:14,代码来源:test_differences.py

示例12: test_range_element_fail

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_range_element_fail(self):
        range_copy = copy.deepcopy(RANGE_ELEMENT)
        ele = ElementFactory.from_json(range_copy)
        flattened = flatten_element(ele)

        output = {
            "items": [
                {"count": 7},
                {}
            ]
        }
        self.assertIsNotNone(differences(output, flattened))
开发者ID:pshrmn,项目名称:gatherer,代码行数:14,代码来源:test_differences.py

示例13: test_optional_range_element

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_optional_range_element(self):
        """
        for an optional RangeElement, the dict for it
        will be optional as will all rules from it and
        its children
        """
        range_copy = copy.deepcopy(RANGE_ELEMENT)
        range_copy["optional"] = True
        ele = ElementFactory.from_json(range_copy)
        flattened = flatten_element(ele)

        output = {}
        self.assertIsNone(differences(output, flattened))
开发者ID:pshrmn,项目名称:gatherer,代码行数:15,代码来源:test_differences.py

示例14: test_single_child_single

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_single_child_single(self):
        single_copy = copy.deepcopy(SINGLE_ELEMENT)
        alt_single_copy = copy.deepcopy(ALT_SINGLE_ELEMENT)
        single_copy["children"].append(alt_single_copy)
        ele = ElementFactory.from_json(single_copy)
        flattened = flatten_element(ele)

        output = {
            "title": "The Dark Forest",
            "url": "http://www.liucixin.com/the_dark_forest",
            "text": "The Second Novel"
        }

        self.assertIsNone(differences(output, flattened))
开发者ID:pshrmn,项目名称:gatherer,代码行数:16,代码来源:test_differences.py

示例15: test_single_child_single_optional_fail

# 需要导入模块: from gatherer.element import ElementFactory [as 别名]
# 或者: from gatherer.element.ElementFactory import from_json [as 别名]
    def test_single_child_single_optional_fail(self):
        single_copy = copy.deepcopy(SINGLE_ELEMENT)
        alt_single_copy = copy.deepcopy(ALT_SINGLE_ELEMENT)
        single_copy["children"].append(alt_single_copy)
        alt_single_copy["optional"] = True
        ele = ElementFactory.from_json(single_copy)
        flattened = flatten_element(ele)

        outputs = [
            {
                "title": "The Dark Forest",
                "text": "The Second Novel"
            }
        ]

        for o in outputs:
            self.assertIsNotNone(differences(o, flattened))
开发者ID:pshrmn,项目名称:gatherer,代码行数:19,代码来源:test_differences.py


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