-
-
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." |
!! I didn't notice this comment. Thanks for the feedback! I'm still such a Python newb.
I changed it
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. >_<
Oh.
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!
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
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.
I believe isinstance is recommended over comparing class - probably because it handles subclasses and doesn't require so many underscores :)