Repository: DinoTools/python-overpy

Issue:
Can't pickle overpy.Result
<!--- Verify first that your issue/request has not already been reported -->

##### Issue type
 - Bug Report

##### OverPy version
<!--- Paste verbatim output from “python -c 'import overpy; print(overpy.__version__)'” or the hash of the git commit between quotes below. -->
```
0.4
```

##### OS
<!---
Add information about the os you are using OverPy on.
Pick one below and delete the rest:
-->
 - Windows 7 x64 HP

#### Python version
<!-- Pick at least one below and delete the rest -->
 - Python 3.5 x64

##### Summary
Neither pickle nor _pickle Python modules can't dump query result.

##### Steps to reproduce
```python
import overpy
import pickle

op = overpy.Overpass()
sample = op.query("""
    way(50.746,7.154,50.748,7.157) ["highway"];
    (._;>;);
    out body;
    """)

with open('sample.pcl', 'wb') as pcl:
    pickle.dump(sample, pcl)
```

<!--- You can also add links to gist.github.com. -->

##### Expected results
pcl-file full of usefull data =)

##### Actual results
Empty plc-file and exception:
```
     11 with open('sample.pcl', 'wb') as pcl:
---> 12     pickle.dump(sample, pcl)

AttributeError: Can't pickle local object 'Element.__init__.<locals>.<lambda>'
```


Discussion:

Patch:
diff --git a/overpy/__init__.py b/overpy/__init__.py
index 377c28d..6aa887a 100644
--- a/overpy/__init__.py
+++ b/overpy/__init__.py
@@ -19,6 +19,16 @@ PY3 = sys.version_info[0] == 3
 XML_PARSER_DOM = 1
 XML_PARSER_SAX = 2
 
+# Try to convert some common attributes
+# http://wiki.openstreetmap.org/wiki/Elements#Common_attributes
+GLOBAL_ATTRIBUTE_MODIFIERS = {
+    "changeset": int,
+    "timestamp": lambda ts: datetime.strptime(ts, "%Y-%m-%dT%H:%M:%SZ"),
+    "uid": int,
+    "version": int,
+    "visible": lambda v: v.lower() == "true"
+}
+
 if PY2:
     from urllib2 import urlopen
     from urllib2 import HTTPError
@@ -608,17 +618,10 @@ class Element(object):
         """
 
         self._result = result
-        # Try to convert some common attributes
-        # http://wiki.openstreetmap.org/wiki/Elements#Common_attributes
-        self._attribute_modifiers = {
-            "changeset": int,
-            "timestamp": lambda ts: datetime.strptime(ts, "%Y-%m-%dT%H:%M:%SZ"),
-            "uid": int,
-            "version": int,
-            "visible": lambda v: v.lower() == "true"
-        }
         self.attributes = attributes
-        for n, m in self._attribute_modifiers.items():
+        # ToDo: Add option to modify attribute modifiers
+        attribute_modifiers = dict(GLOBAL_ATTRIBUTE_MODIFIERS.items())
+        for n, m in attribute_modifiers.items():
             if n in self.attributes:
                 self.attributes[n] = m(self.attributes[n])
         self.id = None


Test patch:
diff --git a/tests/test_result.py b/tests/test_result.py
index 784208b..d83c0d7 100644
--- a/tests/test_result.py
+++ b/tests/test_result.py
@@ -2,6 +2,7 @@ import pytest
 
 import overpy
 
+from tests.base_class import BaseTestWay
 from tests import read_file, new_server_thread, stop_server_thread, BaseHTTPRequestHandler
 
 
@@ -109,6 +110,23 @@ class TestNode(object):
         stop_server_thread(server)
 
 
+class TestPickle(BaseTestWay):
+    def test_way02(self):
+        """
+        Try to pickle and unpickle the result object
+        """
+        import pickle
+
+        api = overpy.Overpass()
+        result = api.parse_json(read_file("json/way-02.json"))
+        self._test_way02(result)
+        # do pickle and unpickle
+        result_string = pickle.dumps(result)
+        new_result = pickle.loads(result_string)
+        # test new result
+        self._test_way02(new_result)
+
+
 class TestRelation(object):
     def test_missing_unresolvable(self):
         url, server = new_server_thread(HandleResponseJSON02)