Skip to content

Commit db8f80e

Browse files
authored
git - Merge pull request #65 from DinoTools/pickle
Pickle Result() object
2 parents daa6a72 + e4f15af commit db8f80e

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

overpy/__init__.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
XML_PARSER_DOM = 1
2020
XML_PARSER_SAX = 2
2121

22+
# Try to convert some common attributes
23+
# http://wiki.openstreetmap.org/wiki/Elements#Common_attributes
24+
GLOBAL_ATTRIBUTE_MODIFIERS = {
25+
"changeset": int,
26+
"timestamp": lambda ts: datetime.strptime(ts, "%Y-%m-%dT%H:%M:%SZ"),
27+
"uid": int,
28+
"version": int,
29+
"visible": lambda v: v.lower() == "true"
30+
}
31+
2232
if PY2:
2333
from urllib2 import urlopen
2434
from urllib2 import HTTPError
@@ -608,17 +618,10 @@ def __init__(self, attributes=None, result=None, tags=None):
608618
"""
609619

610620
self._result = result
611-
# Try to convert some common attributes
612-
# http://wiki.openstreetmap.org/wiki/Elements#Common_attributes
613-
self._attribute_modifiers = {
614-
"changeset": int,
615-
"timestamp": lambda ts: datetime.strptime(ts, "%Y-%m-%dT%H:%M:%SZ"),
616-
"uid": int,
617-
"version": int,
618-
"visible": lambda v: v.lower() == "true"
619-
}
620621
self.attributes = attributes
621-
for n, m in self._attribute_modifiers.items():
622+
# ToDo: Add option to modify attribute modifiers
623+
attribute_modifiers = dict(GLOBAL_ATTRIBUTE_MODIFIERS.items())
624+
for n, m in attribute_modifiers.items():
622625
if n in self.attributes:
623626
self.attributes[n] = m(self.attributes[n])
624627
self.id = None

tests/test_result.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import overpy
44

5+
from tests.base_class import BaseTestWay
56
from tests import read_file, new_server_thread, stop_server_thread, BaseHTTPRequestHandler
67

78

@@ -109,6 +110,23 @@ def test_missing_resolvable(self):
109110
stop_server_thread(server)
110111

111112

113+
class TestPickle(BaseTestWay):
114+
def test_way02(self):
115+
"""
116+
Try to pickle and unpickle the result object
117+
"""
118+
import pickle
119+
120+
api = overpy.Overpass()
121+
result = api.parse_json(read_file("json/way-02.json"))
122+
self._test_way02(result)
123+
# do pickle and unpickle
124+
result_string = pickle.dumps(result)
125+
new_result = pickle.loads(result_string)
126+
# test new result
127+
self._test_way02(new_result)
128+
129+
112130
class TestRelation(object):
113131
def test_missing_unresolvable(self):
114132
url, server = new_server_thread(HandleResponseJSON02)

0 commit comments

Comments
 (0)