从json文件创建Python Active Resource对象

时间:2019-06-05 22:21:12

标签: python json

因此,出于测试目的,我试图从json文件创建python ActiveResource对象(我希望该对象具有json文件中的属性)。更具体地说,我正在使用(https://github.com/Shopify/shopify_python_api)中的ShopifyResource,它扩展了ActiveResource对象。

我仔细查看了源代码,发现一些我认为有用的功能: (https://github.com/Shopify/shopify_python_api/blob/master/shopify/base.py

from pyactiveresource.activeresource import ActiveResource
import shopify.mixins as mixins

class ShopifyResource(ActiveResource, mixins.Countable):
    _format = formats.JSONFormat

    def _load_attributes_from_response(self, response):
        if response.body.strip():
            self._update(self.__class__.format.decode(response.body))

_update来自ActiveResource(https://github.com/Shopify/pyactiveresource/blob/master/pyactiveresource/activeresource.py

    def _update(self, attributes):
        """Update the object with the given attributes.

        Args:
            attributes: A dictionary of attributes.
        Returns:
            None
        """
        if not isinstance(attributes, dict):
            return
        for key, value in six.iteritems(attributes):
            if isinstance(value, dict):
                klass = self._find_class_for(key)
                attr = klass(value)
            elif isinstance(value, list):
                klass = None
                attr = []
                for child in value:
                    if isinstance(child, dict):
                        if klass is None:
                            klass = self._find_class_for_collection(key)
                        attr.append(klass(child))
                    else:
                        attr.append(child)
            else:
                attr = value
            # Store the actual value in the attributes dictionary
            self.attributes[key] = attr

因此,我尝试执行以下操作:

order = Order()
with open("file.json")) as json_file:
    x = json.loads(json_file.read())
    order._update(x)

订单在何处扩展ShopifyResource(扩展ActiveResource)。如果没有记错的话,x应该是字典,它是_update()函数的适当参数。

但是我得到以下输出:

raceback (most recent call last):
  File "/home/vineet/Documents/project/tests/test_sync.py", line 137, in testSaveOrder1
    self.getOrder()
  File "/home/vineet/Documents/project/tests/tests/test_sync.py", line 113, in getOrder
    order._update(x)
  File "/home/vineet/Documents/project/venv/lib/python3.6/site-packages/pyactiveresource/activeresource.py", line 962, in _update
    attr.append(klass(child))
  File "/home/vineet/Documents/project/venv/lib/python3.6/site-packages/shopify/base.py", line 126, in __init__
    prefix_options, attributes = self.__class__._split_options(attributes)
  File "/home/vineet/Documents/project/venv/lib/python3.6/site-packages/pyactiveresource/activeresource.py", line 466, in _split_options
    if key in cls._prefix_parameters():
  File "/home/vineet/Documents/project/venv/lib/python3.6/site-packages/pyactiveresource/activeresource.py", line 720, in _prefix_parameters
    for match in template.pattern.finditer(path):
TypeError: cannot use a string pattern on a bytes-like object

我什至尝试了以下方法:

 order._update(order._format.decode(json_file.read()))

但这没用,因为'str'对象没有属性'decode'。

1 个答案:

答案 0 :(得分:0)

您似乎担心x是否具有正确的格式。打印它,然后检查。

顺便说一句:并使用

x = json.load(json_file)

代替

x = json.loads(json_file.read())