Skip to content
lucjon edited this page Sep 14, 2010 · 28 revisions

Welcome to the Py-StackExchange wiki!

New API Version
A new API version (1.0) has been released. As far as I’m aware, there aren’t really any changes, but if you encounter any difficulties, just pull from the 0.9 branch instead.

Things to do

  • Fix/investigate this bug
  • Implement search and post revisions
  • Convert keyword arguments to correct strings (i.e., ('hello', 'world') becomes 'hello;world', 10 becomes '10'). done.
  • Look at adding support for gzip-compressed responses done.

FAQ

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.
  • 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 does 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.
  • How do I format dates like they do on the site — like ‘3 seconds ago’ (or for Jon Skeet, ‘-∞ 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.
If you do need the Jon Skeet bit, try:

if post.owner_id == 22656:
    return u'-∞ seconds ago'
else:
    return stackexchange.format_relative_date(post.creation_date)
Note that the Jon Skeet UID is only for StackOverflow, not [M]SFU or Moms4Mom.
  • 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 put it here. (By the way, thanks to code poet on StackApps for all these dev-tips questions, and the people that give simple answers. :))
Just use post.owner.reputation.format(), or if you need to do so with an arbitrary number, FormattedReputation.format(num). This is implemented under the hood with a subclass of int, so reputation should still behave as it should.
Clone this wiki locally