Skip to content

Basic "hello world" code broken #13

Closed
rdhyee opened this Issue Jan 13, 2012 · 6 comments

2 participants

@rdhyee
rdhyee commented Jan 13, 2012

I ran the following code (taken from http://pyzotero.readthedocs.org/en/latest/index.html#hello-world):

from zoteroconf import user_id, user_key

# http://pyzotero.readthedocs.org/en/latest/index.html

from pyzotero import zotero
zot = zotero.Zotero(user_id, user_key)
items = zot.items()
for item in items:
    print 'Author: %s | Title: %s' % (item['creators'][0]['lastName'], item['title'])

with pyzotero 0.9.3 and got the following error:

Traceback (most recent call last):
  File "pyzotero_test.py", line 7, in <module>
    items = zot.items()
  File "/home/ubuntu/.virtualenvs/zoterotest/lib/python2.7/site-packages/pyzotero/zotero.py", line 102, in wrapped_f
    return self._process_content(parsed)
  File "/home/ubuntu/.virtualenvs/zoterotest/lib/python2.7/site-packages/pyzotero/zotero.py", line 552, in _process_content
    return self._standard_items(content)
  File "/home/ubuntu/.virtualenvs/zoterotest/lib/python2.7/site-packages/pyzotero/zotero.py", line 569, in _standard_items
    for e in retrieved.entries]
  File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 360, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 378, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

When I run the code with a version of pyzotero that worked for me in November 2011 (0.9.1), I get the following error:

Traceback (most recent call last):
  File "pyzotero_test.py", line 9, in <module>
    print 'Author: %s | Title: %s' % (item['creators'][0]['lastName'], item['title'])
TypeError: string indices must be integers

I'm using Python 2.7.1+ on ubuntu. I get the same errors running Python 2.7 on OS X.

Thanks!

@urschrei
Owner

Could you try installing version 0.9.4 (just pip install pyzotero --upgrade should do it), and retrying your hello world?

@rdhyee
@urschrei
Owner

I've made some changes to the Pyzotero API, but nothing that would break existing code, and I've been keeping up with changes to the Zotero Server API.

However, the issue where the hello world example will fail if the creator field is empty isn't an issue per se; it's not Pyzotero's responsibility to check for empty fields – it simply returns a representation of the data it retrieved from the server. If you attempt to print a sub-item from an empty list without trying to catch the error which might be raised by that action, your code will fail.

Your initial issue seemed to have been caused by Pyzotero incorrectly attempting to Base64 decode JSON content received for attachment items. That error's been fixed by 0.9.4

So, here's what you can try: change your

except Exception e:
    print "print "Error: %s " % (e)

lines to:

except IndexError:
    pass

Which will suppress any errors raised by empty fields.

@rdhyee
rdhyee commented Jan 13, 2012

I understand that it's the responsibility of Pyzotero's caller to check for empty fields. I only bring the issue of empty creator lists because your hello world example at http://pyzotero.readthedocs.org/en/latest/index.html#hello-world) doesn't check for empty creator list -- you might want to help a pyzotero newbie out by doing a check and also change zot.items() to zot.top() (https://github.com/urschrei/pyzotero/blob/master/doc/index.txt#L57).

I'll now look at the code I wrote to see what it's broken. I wrote some code that extends pyzotero.Zotero() to do some automatic paging of the results -- to turn the results into an iterator -- and I might have made some assumptions of the pyzotero internals that are no longer valid.

Thanks,
-Raymond

@rdhyee
rdhyee commented Jan 13, 2012

Hi Steph,

BTW, thanks so much for writing Pyzotero in the first place!! (I don't want to sound ungrateful in my comments.)

-Raymond

@urschrei
Owner

You're welcome! And you have a point about the example, I think. Anyway, do let me know if you run into any difficulty, if anything, my refactoring's made the code a little simpler.

@urschrei urschrei added a commit that closed this issue Jan 17, 2012
@urschrei More widely-applicable "Hello World" example
The new "Hello World" example uses only mandatory components
of a Zotero item (item type and item ID/key), and retrieves only
five top-level items.

Closes #13
1b00cdd
@urschrei urschrei closed this in 1b00cdd Jan 17, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.