从json-fi创建Python活动资源对象

2024-04-16 07:19:23 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,出于测试目的,我尝试从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))

其中,更新来自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)

其中Order扩展了shopifysource(它扩展了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”属性。你知道吗


Tags: inpyselfjsonhomeifvalueupdate
1条回答
网友
1楼 · 发布于 2024-04-16 07:19:23

你似乎担心x的格式是否正确。打印出来,检查一下。你知道吗

顺便说一句:和使用

x = json.load(json_file)

而不是

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

相关问题 更多 >