Skip to content

Commit 1794033

Browse files
authored
git - Merge pull request #119 from DinoTools/docs
Fix isse and add more documentation
2 parents 9e16d51 + 6f7224f commit 1794033

File tree

2 files changed

+94
-61
lines changed

2 files changed

+94
-61
lines changed

docs/source/example.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,14 @@ Line 22-25:
260260
The resolved nodes have been added to the result set and are available to be used again later.
261261

262262
Serialization
263-
----
263+
-------------
264264

265265
Result objects can be converted to a dictionary, in the same format as the
266266
Overpass API ``json`` output format.
267267

268268
.. code-block:: pycon
269+
:linenos:
270+
269271
>>> import overpy, simplejson
270272
>>> api = overpy.Overpass()
271273
>>> result = api.query("[out:xml];node(50.745,7.17,50.75,7.18);out;")
@@ -280,6 +282,8 @@ numbers, and then parsing with ``Overpass.parse_json()``. The third-party
280282
package ``simplejson`` works for this application:
281283

282284
.. code-block:: pycon
285+
:linenos:
286+
283287
>>> result_str = simplejson.dumps(result.to_json())
284288
>>> new_result = api.parse_json(result_str)
285289
>>> assert len(result.nodes) == len(new_result.nodes)

overpy/__init__.py

Lines changed: 89 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,23 @@ class Overpass:
6060
"""
6161
Class to access the Overpass API
6262
63-
:cvar default_max_retry_count: Global max number of retries (Default: 0)
64-
:cvar default_read_chunk_size: Max size of each chunk read from the server response
65-
:cvar default_retry_timeout: Global time to wait between tries (Default: 1.0s)
66-
:cvar default_url: Default URL of the Overpass server
63+
:param read_chunk_size: Max size of each chunk read from the server response
64+
:param url: Optional URL of the Overpass server. Defaults to http://overpass-api.de/api/interpreter
65+
:param xml_parser: The xml parser to use
66+
:param max_retry_count: Max number of retries (Default: default_max_retry_count)
67+
:param retry_timeout: Time to wait between tries (Default: default_retry_timeout)
6768
"""
69+
70+
#: Global max number of retries (Default: 0)
6871
default_max_retry_count: ClassVar[int] = 0
72+
73+
#: Max size of each chunk read from the server response
6974
default_read_chunk_size: ClassVar[int] = 4096
75+
76+
#: Global time to wait between tries (Default: 1.0s)
7077
default_retry_timeout: ClassVar[float] = 1.0
78+
79+
#: Default URL of the Overpass server
7180
default_url: ClassVar[str] = "http://overpass-api.de/api/interpreter"
7281

7382
def __init__(
@@ -77,13 +86,8 @@ def __init__(
7786
xml_parser: int = XML_PARSER_SAX,
7887
max_retry_count: int = None,
7988
retry_timeout: float = None):
80-
"""
81-
:param read_chunk_size: Max size of each chunk read from the server response
82-
:param url: Optional URL of the Overpass server. Defaults to http://overpass-api.de/api/interpreter
83-
:param xml_parser: The xml parser to use
84-
:param max_retry_count: Max number of retries (Default: default_max_retry_count)
85-
:param retry_timeout: Time to wait between tries (Default: default_retry_timeout)
86-
"""
89+
90+
#: URL to use for this instance
8791
self.url = self.default_url
8892
if url is not None:
8993
self.url = url
@@ -92,16 +96,23 @@ def __init__(
9296
self._regex_remove_tag = re.compile(b"<[^>]*?>")
9397
if read_chunk_size is None:
9498
read_chunk_size = self.default_read_chunk_size
99+
100+
#: The chunk size for this instance
95101
self.read_chunk_size = read_chunk_size
96102

97103
if max_retry_count is None:
98104
max_retry_count = self.default_max_retry_count
105+
106+
#: Max retry count for this instance
99107
self.max_retry_count = max_retry_count
100108

101109
if retry_timeout is None:
102110
retry_timeout = self.default_retry_timeout
111+
112+
#: The retry timeout for this instance
103113
self.retry_timeout = retry_timeout
104114

115+
#: The XML parser to use for this instance
105116
self.xml_parser = xml_parser
106117

107118
@staticmethod
@@ -176,7 +187,11 @@ def query(self, query: Union[bytes, str]) -> "Result":
176187
retry_exceptions.append(current_exception)
177188
raise exception.MaxRetriesReached(retry_count=run + 1, exceptions=retry_exceptions)
178189

179-
def parse_json(self, data: Union[bytes, str], encoding: str = "utf-8") -> "Result":
190+
def parse_json(
191+
self,
192+
data: Union[bytes, str],
193+
encoding: str = "utf-8"
194+
) -> "Result":
180195
"""
181196
Parse raw response from Overpass service.
182197
@@ -191,7 +206,12 @@ def parse_json(self, data: Union[bytes, str], encoding: str = "utf-8") -> "Resul
191206
self._handle_remark_msg(msg=data_parsed.get("remark"))
192207
return Result.from_json(data_parsed, api=self)
193208

194-
def parse_xml(self, data: Union[bytes, str], encoding: str = "utf-8", parser: Optional[int] = None):
209+
def parse_xml(
210+
self,
211+
data: Union[bytes, str],
212+
encoding: str = "utf-8",
213+
parser: Optional[int] = None
214+
) -> "Result":
195215
"""
196216
197217
:param data: Raw XML Data
@@ -215,17 +235,16 @@ def parse_xml(self, data: Union[bytes, str], encoding: str = "utf-8", parser: Op
215235
class Result:
216236
"""
217237
Class to handle the result.
238+
239+
:param elements: List of elements to initialize the result with
240+
:param api: The API object to load additional resources and elements
218241
"""
219242

220243
def __init__(
221244
self,
222245
elements: Optional[List[Union["Area", "Node", "Relation", "Way"]]] = None,
223246
api: Optional[Overpass] = None):
224-
"""
225247

226-
:param elements: List of elements to initialize the result with
227-
:param api: The API object to load additional resources and elements
228-
"""
229248
if elements is None:
230249
elements = []
231250
self._areas: Dict[int, Union["Area", "Node", "Relation", "Way"]] = {
@@ -246,11 +265,12 @@ def __init__(
246265
Relation: self._relations,
247266
Area: self._areas
248267
}
249-
self.api = api
268+
#: The API to use if we need to resolve additional information
269+
self.api: Optional[Overpass] = api
250270

251271
def expand(self, other: "Result"):
252272
"""
253-
Add all elements from an other result to the list of elements of this result object.
273+
Add all elements from another result to the list of elements of this result object.
254274
255275
It is used by the auto resolve feature.
256276
@@ -581,26 +601,28 @@ def get_ways(self, way_id: Optional[int] = None) -> List["Way"]:
581601
class Element:
582602
"""
583603
Base element
604+
605+
:param attributes: Additional attributes
606+
:param result: The result object this element belongs to
607+
:param tags: List of tags
584608
"""
585609

586610
_type_value: str
587611

588612
def __init__(self, attributes: Optional[dict] = None, result: Optional[Result] = None, tags: Optional[Dict] = None):
589-
"""
590-
:param attributes: Additional attributes
591-
:param result: The result object this element belongs to
592-
:param tags: List of tags
593-
"""
594-
595613
self._result = result
596614
self.attributes = attributes
597615
# ToDo: Add option to modify attribute modifiers
598616
attribute_modifiers: Dict[str, Callable] = dict(GLOBAL_ATTRIBUTE_MODIFIERS.items())
599617
for n, m in attribute_modifiers.items():
600618
if n in self.attributes:
601619
self.attributes[n] = m(self.attributes[n])
620+
621+
#: The ID of the element form the OSM data
602622
self.id: int
603-
self.tags = tags
623+
624+
#: The tags of the element
625+
self.tags: Optional[Dict] = tags
604626

605627
@classmethod
606628
def get_center_from_json(cls, data: dict) -> Tuple[Decimal, Decimal]:
@@ -661,16 +683,14 @@ def from_xml(
661683
class Area(Element):
662684
"""
663685
Class to represent an element of type area
686+
687+
:param area_id: Id of the area element
688+
:param kwargs: Additional arguments are passed directly to the parent class
664689
"""
665690

666691
_type_value = "area"
667692

668693
def __init__(self, area_id: Optional[int] = None, **kwargs):
669-
"""
670-
:param area_id: Id of the area element
671-
:param kwargs: Additional arguments are passed directly to the parent class
672-
"""
673-
674694
Element.__init__(self, **kwargs)
675695
#: The id of the way
676696
self.id = area_id
@@ -753,6 +773,11 @@ def from_xml(cls, child: xml.etree.ElementTree.Element, result: Optional[Result]
753773
class Node(Element):
754774
"""
755775
Class to represent an element of type node
776+
777+
:param lat: Latitude
778+
:param lon: Longitude
779+
:param node_id: Id of the node element
780+
:param kwargs: Additional arguments are passed directly to the parent class
756781
"""
757782

758783
_type_value = "node"
@@ -763,16 +788,16 @@ def __init__(
763788
lat: Optional[Union[Decimal, float]] = None,
764789
lon: Optional[Union[Decimal, float]] = None,
765790
**kwargs):
766-
"""
767-
:param lat: Latitude
768-
:param lon: Longitude
769-
:param node_id: Id of the node element
770-
:param kwargs: Additional arguments are passed directly to the parent class
771-
"""
772791

773792
Element.__init__(self, **kwargs)
793+
794+
#: The node ID from the OSM
774795
self.id = node_id
796+
797+
#: Latitude of the node
775798
self.lat = lat
799+
800+
#: Longitude of the node
776801
self.lon = lon
777802

778803
def __repr__(self) -> str:
@@ -870,6 +895,12 @@ def from_xml(cls, child: xml.etree.ElementTree.Element, result: Optional[Result]
870895
class Way(Element):
871896
"""
872897
Class to represent an element of type way
898+
899+
:param center_lat: The latitude of the center of the way (optional depending on query)
900+
:param center_lon: The longitude of the center of the way (optional depending on query)
901+
:param node_ids: List of node IDs
902+
:param way_id: Id of the way element
903+
:param kwargs: Additional arguments are passed directly to the parent class
873904
"""
874905

875906
_type_value = "way"
@@ -881,11 +912,6 @@ def __init__(
881912
center_lon: Optional[Union[Decimal, float]] = None,
882913
node_ids: Optional[Union[List[int], Tuple[int]]] = None,
883914
**kwargs):
884-
"""
885-
:param node_ids: List of node IDs
886-
:param way_id: Id of the way element
887-
:param kwargs: Additional arguments are passed directly to the parent class
888-
"""
889915

890916
Element.__init__(self, **kwargs)
891917
#: The id of the way
@@ -894,8 +920,10 @@ def __init__(
894920
#: List of Ids of the associated nodes
895921
self._node_ids = node_ids
896922

897-
#: The lat/lon of the center of the way (optional depending on query)
923+
#: The latitude of the center of the way (optional depending on query)
898924
self.center_lat = center_lat
925+
926+
#: The longitude of the center of the way (optional depending on query)
899927
self.center_lon = center_lon
900928

901929
def __repr__(self):
@@ -1063,6 +1091,13 @@ def from_xml(cls, child: xml.etree.ElementTree.Element, result: Optional[Result]
10631091
class Relation(Element):
10641092
"""
10651093
Class to represent an element of type relation
1094+
1095+
:param center_lat: The latitude of the center of the relation (optional depending on query)
1096+
:param center_lon: The longitude of the center of the relation (optional depending on query)
1097+
:param members:
1098+
:param rel_id: Id of the relation element
1099+
:param kwargs:
1100+
:return:
10661101
"""
10671102

10681103
_type_value = "relation"
@@ -1074,19 +1109,15 @@ def __init__(
10741109
center_lon: Optional[Union[Decimal, float]] = None,
10751110
members: Optional[List["RelationMember"]] = None,
10761111
**kwargs):
1077-
"""
1078-
:param members:
1079-
:param rel_id: Id of the relation element
1080-
:param kwargs:
1081-
:return:
1082-
"""
10831112

10841113
Element.__init__(self, **kwargs)
10851114
self.id = rel_id
10861115
self.members = members
10871116

1088-
#: The lat/lon of the center of the way (optional depending on query)
1117+
#: The latitude of the center of the relation (optional depending on query)
10891118
self.center_lat = center_lat
1119+
1120+
#: The longitude of the center of the relation (optional depending on query)
10901121
self.center_lon = center_lon
10911122

10921123
def __repr__(self):
@@ -1221,6 +1252,11 @@ def from_xml(cls, child: xml.etree.ElementTree.Element, result: Optional[Result]
12211252
class RelationMember:
12221253
"""
12231254
Base class to represent a member of a relation.
1255+
1256+
:param attributes:
1257+
:param ref: Reference Id
1258+
:param role: The role of the relation member
1259+
:param result:
12241260
"""
12251261
_type_value: Optional[str] = None
12261262

@@ -1231,13 +1267,7 @@ def __init__(
12311267
ref: Optional[int] = None,
12321268
role: Optional[str] = None,
12331269
result: Optional[Result] = None):
1234-
"""
1235-
:param ref: Reference Id
1236-
:type ref: Integer
1237-
:param role: The role of the relation member
1238-
:type role: String
1239-
:param result:
1240-
"""
1270+
12411271
self.ref = ref
12421272
self._result = result
12431273
self.role = role
@@ -1405,16 +1435,15 @@ def __repr__(self):
14051435
class OSMSAXHandler(handler.ContentHandler):
14061436
"""
14071437
SAX parser for Overpass XML response.
1438+
1439+
:param result: Append results to this result set.
14081440
"""
14091441
#: Tuple of opening elements to ignore
14101442
ignore_start: ClassVar = ('osm', 'meta', 'note', 'bounds', 'remark')
14111443
#: Tuple of closing elements to ignore
14121444
ignore_end: ClassVar = ('osm', 'meta', 'note', 'bounds', 'remark', 'tag', 'nd', 'center')
14131445

14141446
def __init__(self, result: Result):
1415-
"""
1416-
:param result: Append results to this result set.
1417-
"""
14181447
handler.ContentHandler.__init__(self)
14191448
self._result = result
14201449
self._curr: Dict[str, Any] = {}

0 commit comments

Comments
 (0)