简体   繁体   中英

Creating Python Active Resource object from json file

So for testing purposes, I am trying to create a python ActiveResource object from a json file (I want the object to have attributes from the json file). More specifically I am using the ShopifyResource from ( https://github.com/Shopify/shopify_python_api ), which extends the ActiveResource object.

I looked through the source code and found some functions that I thought would be of some use: ( 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))

where _update is from 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

So then I tried to do the following:

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

Where Order extends ShopifyResource (which extends ActiveResource). If am not mistaken x should be a dictionary, which is an approriate parameter for the _update() function.

Yet I get the following output:

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

I even tried the following:

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

But that didn't work since 'str' object has no attribute 'decode'.

It seems You are worried if x has correct format. Print it, and check.

Btw: And use

x = json.load(json_file)

instead of

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM