forked from simonw/geocoders
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
54 lines (48 loc) · 1.52 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Yuck! http://code.activestate.com/recipes/475126/
try:
import xml.etree.ElementTree as ET # in python >=2.5
except ImportError:
try:
import cElementTree as ET # effbot's C module
except ImportError:
try:
import elementtree.ElementTree as ET # effbot's pure Python module
except ImportError:
import lxml.etree as ET # ElementTree API using libxml2
try:
import json as simplejson
except ImportError:
import simplejson
# Methods for helping with namespace handling in ElementTree
def make_nsfind(nsdict=None):
nsdict = nsdict or {}
def find(et, xpath):
xpath = fix_ns(xpath, nsdict)
return et.find(xpath)
return find
def make_nsfindall(nsdict=None):
nsdict = nsdict or {}
def find(et, xpath):
return et.findall(fix_ns(xpath, nsdict))
return find
def fix_ns(xpath, nsdict=None):
nsdict = nsdict or {}
for ns, url in nsdict.items():
xpath = xpath.replace('%s:' % ns, '{%s}' % url)
return xpath
def partial2(fn):
def inner1(arg2):
def inner2(arg1):
return fn(arg1, arg2)
return inner2
return inner1
# Borrowed from
# http://geopy.googlecode.com/svn/branches/reverse-geocode/geopy/util.py
# Allows for original tuple unpacking when the caller doesn't care about
# extra data
class RichResult(tuple):
def __new__(cls, *args, **kwargs):
obj = tuple.__new__(cls, *args)
for name, value in kwargs.items():
setattr(obj, name, value)
return obj