FAQ

Kieran Kelk edited this page Sep 1, 2014 · 7 revisions

Before checking here, I'd suggest updating to the latest version of the library, either through a git pull or downloading a ZIP or tarball and extracting it over the existing files - bugs are being fixed all the time.

  • I'm getting an ImportError on simplejson. Why?
    This is because you are running a version of Python below 2.6 (i.e., you don't have the in-built JSON module.) and don't have the simplejson library installed. (Or you're just weird and like messing about with your Python install. Like me.) The solution is generally to install simplejson from PyPI (easy_install simplejson).

    If you're using Django, you already have simplejson there, it's just under the django.utils namespace. To use it in Py-StackExchange, edit the stackweb.py file and change the line:

    import simplejson as json

    to:

    from django.utils import simplejson as json

    Thanks to Edan Maor on StackApps for this tip.

  • Is there an easy way to get up votes - down votes on a question or answer?

    Yes! Use the score property.

  • Why do I get an empty list on user.answers or user.questions or badge.recipients etc.?

    To reduce the number of API calls the library makes, collections which would require another request to populate require explicit 'fetching' of the data, with, for example user.answers.fetch(). This will return the list with the new items, and also update the property on the original object.

  • Why do answer.body or question.body raise an AttributeError?

    Question and answer bodies must be explicitly fetched (through the specification of a parameter) to save bandwidth. (This is a restriction 'imposed' by the API.) To do this, you can either use the body=True keyword argument to your request:

    so = stackexchange.Site(stackexchange.StackOverflow)
    q = so.question(4, body=True)
    

    Or, to have this affect the entire site object (note: this must be used if you need to access bodies from post collections, such as user.answers), call so.be_inclusive() before making any requests.

  • Why do I get a ValueError: No JSON object could be decoded message?

    This is probably the result of some proxy/router mangling with request headers. It could be that your router/proxy adds headers requesting gzip data, but doesn't decompress it, and that you are running a slightly old version of the code which does not deal with gzip compression. In this case, just update to the latest version of the library. Otherwise, please send me a stack trace and any other details you have through StackApps - just submit an answer.

  • What's the Py-StackExchange equivalent of the URL users/41981/questions?

    Just use site.{questions|answers|etc}(user_id=41981).

  • Why do I get a 'not a gzip file' or similar error?

    Something could be disposing of, or decompressing, the gzip headers or data respectively. In this case, before making your first request, use site.use_gzip = False. If this doesn't solve the problem, send me a bug report through StackApps (see previous question).

  • fetch_next() returns an empty tuple where it shouldn't. Sometimes it works if the code is moved somewhere else.

    I'm not exactly sure what's going on here, but try rearranging your code if your problem matches this description. Otherwise, send me a bug report through StackApps or Github.

  • How do I format dates like they do on the site -- like '3 seconds ago'?

    If the last part isn't an absolute requirement, you can use the stack{exchange|auth}.format_relative_date(date) function. Just pass in the date from the post object, and it'll give you back a string.

  • Can I be defensive against request throttling by the API?

    Yes. Just add the so.impose_throttling = True property to your Site object. This will (in theory) raise an error when you make more than 30 requests in 5 seconds.

    If you'd prefer, you can instead have the library wait until it's safe before making the request by setting so.throttle_stop = False. This might not always work as expected, however. For more information on API throttling, see: http://stackapps.com/questions/1143/request-throttling-limits.

  • Can I format reputation scores like they are on the SE site homepages?
    Of course! If you couldn't, I wouldn't have put the question in here. Thanks to George Edison and code poet on StackApps for posting their simple algorithms on the dev-tips questions, which were easily ported to Python and incorporated here.

    Just use post.owner.reputation.format(), or if you need to do so with an arbitrary number, FormattedReputation(num).format(). This is implemented under the hood with a subclass of int, so reputation should still behave as it should as far as things like arithmetic are concerned.

  • Why are post.owner, or user.answers returning None?

    I had a particularly stupid bug which affected the creation of all "partial models." These are objects in the API which have been made from a portion of the values they'd have when requested directly, because they are generated only from information attached to another response.

    I've fixed this in the latest revision. If you still encounter problems, please send me a bug report through StackApps.

  • What is this mysterious Area 51 error?

    With StackAuth, if the user whose associations you are looking up has an associated account on Area51, it will appear in the association list. However, you can't interact with Area 51 through the API, so I created a stub class which at least allows the list of associated accounts to be constructed.

    If you'd like not to include Area 51 in the results, prefix the method with api_ - so, my_stack_auth.associated() becomes my_stack_auth.api_associated(). You can also check explicitly if the has_endpoint field is True on the association entry.

  • The in-built caching is a bit too aggressive. Can I reduce the expiration delay, or disable caching altogether?

    Yes and yes. When constructing your Site object, use Site(cache=Timeout). The timeout is in seconds; if 0 is specified, the cache will be disabled. By default, objects are kept in the cache for 1800 seconds (thirty minutes).

  • How do I enable API request debug output?

    You can set the .debug attribute on the stackexchange.web.WebRequestManager class:

    import stackexchange
    so = stackexchange.Site(stackexchange.StackOverflow)
    stackexchange.web.WebRequestManager.debug = True
    ... do some stackexchange things here...

    – though this will operate globally on all API requests from that point forward.