Skip to content

Instantly share code, notes, and snippets.

@benui-dev
Created January 16, 2012 08:52
Show Gist options
  • Select an option

  • Save benui-dev/1619791 to your computer and use it in GitHub Desktop.

Select an option

Save benui-dev/1619791 to your computer and use it in GitHub Desktop.
from mwlib import wiki
from mwlib.parser import nodes
env = wiki.makewiki('/home/ben/wiki/simple/wikiconf.txt') # wiki Environment object
a = env.wiki.getParsedArticle('cheese')
for child in a.allchildren():
if isinstance(child, nodes.Section): # Yay global function
print "Section title: " + child.children[0].asText()
if isinstance(child, nodes.LangLink):
if child.interwiki == "ja":
print "Linked Japanese article: " + child.target
elif isinstance(child, nodes.ArticleLink):
print "Inline link: " + child.target
if child.ns != 0:
print "The link is in another castle. A non-English castle."
@veryhappythings
Copy link
Copy Markdown

I believe isinstance is recommended over comparing class - probably because it handles subclasses and doesn't require so many underscores :)

@benui-dev
Copy link
Copy Markdown
Author

!! I didn't notice this comment. Thanks for the feedback! I'm still such a Python newb.
I changed it

@veryhappythings
Copy link
Copy Markdown

It's a sucky language because there are loads of unexpected side-effects like this. It should be isinstance(child, nodes.Section) http://docs.python.org/library/functions.html#isinstance - why is it a global function? Because it just is. Like most stuff in python, there is no real sense to it - it just is. >_<

@benui-dev
Copy link
Copy Markdown
Author

Oh.

@veryhappythings
Copy link
Copy Markdown

That page has all the globals on there, including the thoroughly upsetting enumerate and sorted. Is the language in the past or the present? Who knows! Who cares!

@benui-dev
Copy link
Copy Markdown
Author

The insane capitalisation in the language as well as the libraries I've tried using also drives me nuts. isinstance(), raw_input(), a lot of libraries use camelCase, some use underscores.
It's so PHP.
As you say, maybe we shouldn't care, but the 5th time I get a function name wrong because it doesn't follow the (non-existent) convention, I start caring :S

@veryhappythings
Copy link
Copy Markdown

No, we absolutely should care - I was mimicking the internal voice of the python designers :) Even python's standard library is all over the place. It's a really dirty language, but it is quite solid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment