Created
March 30, 2012 03:01
-
-
Save pckujawa/2246101 to your computer and use it in GitHub Desktop.
xml parsing using different libraries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################## 15 ################################## | |
def hard(): | |
# Example 1 | |
try: | |
import twisted | |
help(twisted) # (this may not be as hard as I think, though) | |
except: | |
pass | |
# Example 2 | |
import xml.dom.minidom | |
document = xml.dom.minidom.parseString( | |
'''<menagerie><cat>Fluffers</cat><cat>Cisco</cat></menagerie>''') | |
menagerie = document.childNodes[0] | |
for node in menagerie.childNodes: | |
if node.childNodes[0].nodeValue== 'Cisco' and node.tagName == 'cat': | |
return node | |
def easy(maybe): | |
# Example 1 | |
try: | |
import gevent | |
help(gevent) | |
except: | |
pass | |
# Example 2 | |
import lxml | |
menagerie = lxml.etree.fromstring( | |
'''<menagerie><cat>Fluffers</cat><cat>Cisco</cat></menagerie>''') | |
for pet in menagerie.find('./cat'): | |
if pet.text == 'Cisco': | |
return pet | |
print "If the implementation is hard to explain, it's a bad idea." | |
print 'If the implementation is easy to explain, it may be a good idea.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken from http://artifex.org/~hblanks/talks/2011/pep20_by_example.html