Skip to content
This repository

The object explorer is one of the demos included in the Py-StackExchange source code. To run it, simply execute python demos/ from your terminal.

It allows you to interactively traverse the object trees generated by the API wrapper. It’s good if you like to poke around, and is also a nicer documentation tool than reading source. It’s a bit more suited to the task than the plain old Python interpreter, as you’ll see.

.h3 Transcript

Here’s an example session:

lucas@ubuntu:~/projects/py-stackexchange$ python demo/ 
Select a site (0 exits):
1) Stack Overflow
2) Server Fault
3) Super User
4) Meta Stack Overflow
5) Web Apps
6) Web Apps Meta
7) Gaming
8) Gaming Meta
9) Webmasters
10) Webmasters Meta
11) Cooking
12) Cooking Meta
13) Game Developers
14) Game Developers Meta
15) Electronic Gadgets
16) Electronic Gadgets Meta
17) Photography
18) Photography Meta
19) Statistical Analysis
20) Statistical Analysis Meta
21) Mathematics
22) Mathematics Meta
23) Home Improvement
24) Home Improvement Meta
25) Meta Super User
26) Meta Server Fault
27) GIS
28) GIS Meta
29) TeX - LaTeX
30) TeX - LaTeX Meta
31) Ubuntu
32) Ubuntu Meta
33) Personal Finance and Money
34) Personal Finance and Money Meta
35) English Language and Usage
36) English Language and Usage Meta
37) Stack Apps
38) User Interface
39) User Interface Meta
40) Unix and Linux
41) Unix and Linux Meta
42) WordPress
43) WordPress Meta

On startup, you are presented with a list of all current StackExchange sites, retrieved through StackAuth. Select one – you can always change later.

Site ID: 1
Use function names you would when using the Site, etc. objects.
return:	Move back up an object.
exit:	Quits.
dir:		Shows meaningful methods and properties on the current object.
dir*:	Same as dir, but includes *all* methods and properties.
code:	Show the code you'd need to get to where you are now.
! before a non-function means "explore anyway."
a prompt ending in []> means the current item is a list.

You are presented with a short help screen the first time you choose a site. The commands are detailed above. Any other input will be interpreted, depending on the circumstances, as either a function name or a list or dictionary index.

Stack Overflow> user
nid: 1
<stackexchange.User object at 0xa24b12c>

I typed user, which is the name of a function on the Site object. The tool then prompts you for each parameter which must be supplied. The resulting object is repr’d and displayed. The interpreter now switches contexts: now, functions and fields are resolved against the new user object, rather than the site object.

user> dir
['about_me', 'age', 'answers', 'association_id', 'badge_counts', 'badge_counts_t', 'badge_total', 'badges', 'bronze_badges', 'comments', 'creation_date', 'display_name', 'down_vote_count', 'email_hash', 'favorites', 'fetch', 'gold_badges', 'id', 'json_ob', 'last_access_date', 'location', 'mentioned', 'partial', 'questions', 'reputation', 'reputation_detail', 'silver_badges', 'site', 'tags', 'timeline', 'transfer', 'up_vote_count', 'url', 'user_type', 'view_count', 'vote_counts', 'website_url']

The prompt has changed to signify that the context has changed. The dir command shows all the ‘meaningful’ attributes: those whose first character is lowercase (if it is a letter), and is not an underscore. I can now query information on the user object.

user> display_name
u'Jeff Atwood'
user> age   
user> about_me
u'<p><img src="" width="250"></p>\n\n<p><a href="" rel="nofollow">Stack Overflow Valued Associate #00001</a></p>\n\n<p>Wondering how our software development process works? <a href="" rel="nofollow">Take a look!</a></p>\n'
user> reputation

There we go. If you ever wonder where you are, just type ‘code’:

user> code
import stackexchange
site = stackexchange.Site("")
user = site.user(1)

This will show you the Python code you’d use to get to the object you are exploring now. You can do this at any level.

The tool handles lists very well, even ones which need more data to be fetched from the API:

user> answers
Fetched. You'll need to remember to call .fetch() in your code.
(<stackexchange.Answer object at 0x8f67cec>, <stackexchange.Answer object at 0x8fb302c>, <stackexchange.Answer object at 0x8fb32ac>, <stackexchange.Answer object at 0x8fb308c>, <stackexchange.Answer object at 0x8fb31ac>, <stackexchange.Answer object at 0x8fb370c>, <stackexchange.Answer object at 0xb788b46c>, <stackexchange.Answer object at 0x8fae0ec>, <stackexchange.Answer object at 0x8f7ddcc>, <stackexchange.Answer object at 0x8f7de2c>, <stackexchange.Answer object at 0x8f7dc2c>, <stackexchange.Answer object at 0x8f7dc6c>, <stackexchange.Answer object at 0x8f7dcec>, <stackexchange.Answer object at 0x8f7dd6c>, <stackexchange.Answer object at 0x8f7ddac>, <stackexchange.Answer object at 0x8f7dbcc>, <stackexchange.Answer object at 0x8f7db4c>, <stackexchange.Answer object at 0x8f7dbac>, <stackexchange.Answer object at 0x8f7dd0c>, <stackexchange.Answer object at 0x8f7dacc>, <stackexchange.Answer object at 0x8f5fbcc>, <stackexchange.Answer object at 0x8f5fd0c>, <stackexchange.Answer object at 0x8fb894c>, <stackexchange.Answer object at 0x8fb896c>, <stackexchange.Answer object at 0x8fb890c>, <stackexchange.Answer object at 0x8fb89ac>, <stackexchange.Answer object at 0x8fb8dac>, <stackexchange.Answer object at 0x8fb860c>, <stackexchange.Answer object at 0x8fb8ccc>, <stackexchange.Answer object at 0x8fb80cc>)

You can see the prompt has changed to include an []. This means that the program has entered list mode. The dir command now works differently, as does the stuff you type:

answers[]> dir
0) Answer 3420640 [sql statistics io scan count explanation]
1) Answer 2934211 [COMET (server push to client) on iPhone]
2) Answer 1250987 [How do I remove eTag headers from IIS7?]
3) Answer 2797461 [C# How do I catch an exception and check if it contains a string?]
4) Answer 2787505 [<noscript> stuff </noscript> doesn't work on google chrome]
5) Answer 2696588 [Two versions of same app using same server as stateserver - bad?]
6) Answer 2682395 [Account confirmation email sent as SPAM]
7) Answer 8474 [Practical non-image based CAPTCHA approaches?]
8) Answer 961504 [Unicode Regex; Invalid XML characters]
9) Answer 177539 [Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?]
10) Answer 2234236 [C#: Generating .BLT Files for OpenSTV Elections]
11) Answer 2205417 [What is the gold standard for website APIs?  Twitter, Flickr, Facebook, etc.]
12) Answer 25486 [How do you include a webpage title as part of a webpage URL?]
13) Answer 72045 [How to implement a Digg-like algorithm?]
14) Answer 2055881 [WMD Markdown from MySQL to HTML server site without showing the textarea]
15) Answer 2055442 [There is an if-else, is there a Neither Nor statement?]
16) Answer 2048073 [Is this a valid XSS attack]
17) Answer 2032670 [How do I get an HTML Text Area with monospaced text using CSS?]
18) Answer 2026609 [Function to Calculate Median in Sql Server]
19) Answer 211461 [Punishment for breaking the build]
20) Answer 1853253 [Quicksort superiority over Heap Sort]
21) Answer 1837450 [Hands-free mice for programmers?]
22) Answer 1775017 [Can I test if a regex is valid in C# without throwing exception]
23) Answer 1741342 [RegEx: Link Twitter-Name Mentions to Twitter in HTML]
24) Answer 1639022 [Firefox and SSL: sec_error_unknown_issuer]
25) Answer 1574165 [How to configure dotNetOpenId in an session less load balancing environment]
26) Answer 1562821 [Should I pay out of pocket to attend a conference?]
27) Answer 1546441 [Why does MVC think multiple different addresses to the same page are SEO friendly?]
28) Answer 1536342 [Rewriting URLs from https:// to http:// in IIS7]
29) Answer 1304839 [IIS7 Cache-Control ]

dir now shows you the contents of the list. To explore an element, just type it’s number.

answers[]> 1
answers_item> dir
['accepted', 'comments', 'community_owned', 'creation_date', 'down_vote_count', 'fetch', 'id', 'json_ob', 'last_activity_date', 'owner', 'owner_id', 'owner_info', 'partial', 'question', 'question_id', 'score', 'site', 'title', 'transfer', 'up_vote_count', 'url', 'view_count', 'votes']

Things work the same as before. Notice that code generation still holds up impeccably:

answers_item> code
import stackexchange
site = stackexchange.Site("")
user = site.user(1)
answers = user.answers.fetch()
answers_item = answers[1]

Now, I think we’re bored of Jeff’s stuff now. Let’s go back a few levels.

answers_item> return
answers[]> return
user> return
Stack Overflow> code
import stackexchange
site = stackexchange.Site("")
Stack Overflow> 

I think we want to see a nicer user’s profile.

Stack Overflow> user
nid: 41981
<stackexchange.User object at 0x9b65f2c>
user> reputation

Now, we can also, if we need to, explore values which ordinarily wouldn’t be explored, like this int. Now, it’s not really an int, as you’ll soon find out.

user> !reputation
reputation> dir
['conjugate', 'denominator', 'format', 'imag', 'numerator', 'real']

You do that simply by putting an exclamation mark before the name. Now, that little ‘format’ method:

reputation> format
format> code
import stackexchange
site = stackexchange.Site("")
user = site.user(41981)
reputation = user.reputation
format = reputation.format()

Amazing. You can exit at any time by typing exit.

format> exit
Something went wrong with that request. Please try again.