Skip to content

Commit 553f90b

Browse files
authored
git - Merge pull request #93 from DinoTools/code_improvements
Code improvements
2 parents fad49ac + 1450a0c commit 553f90b

File tree

7 files changed

+38
-42
lines changed

7 files changed

+38
-42
lines changed

docs/source/conf.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@
55
# serve to show the default.
66

77
import sys
8-
import os
8+
from pathlib import Path
9+
10+
from overpy import __about__ as overpy_about
911

1012
# If extensions (or modules to document with autodoc) are in another directory,
1113
# add these directories to sys.path here. If the directory is relative to the
1214
# documentation root, use os.path.abspath to make it absolute, like shown here.
1315
#sys.path.insert(0, os.path.abspath('.'))
14-
sys.path.insert(
15-
0,
16-
os.path.abspath(
17-
os.path.join(
18-
os.path.dirname(__file__),
19-
'../../'
20-
)
21-
)
22-
)
16+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))
2317

2418
# -- General configuration ------------------------------------------------
2519

@@ -57,7 +51,6 @@
5751
# built documents.
5852
#
5953
# The short X.Y version.
60-
from overpy import __about__ as overpy_about
6154
version = overpy_about.__version__
6255
# The full version, including alpha/beta/rc tags.
6356
release = overpy_about.__version__

overpy/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from collections import OrderedDict
22
from datetime import datetime
33
from decimal import Decimal
4+
from urllib.request import urlopen
5+
from urllib.error import HTTPError
46
from xml.sax import handler, make_parser
57
import json
68
import re
@@ -25,9 +27,6 @@
2527
"visible": lambda v: v.lower() == "true"
2628
}
2729

28-
from urllib.request import urlopen
29-
from urllib.error import HTTPError
30-
3130

3231
def is_valid_type(element, cls):
3332
"""
@@ -86,7 +85,8 @@ def __init__(self, read_chunk_size=None, url=None, xml_parser=XML_PARSER_SAX, ma
8685

8786
self.xml_parser = xml_parser
8887

89-
def _handle_remark_msg(self, msg):
88+
@staticmethod
89+
def _handle_remark_msg(msg):
9090
"""
9191
Try to parse the message provided with the remark tag or element.
9292
@@ -628,7 +628,7 @@ def get_center_from_json(cls, data):
628628
raise ValueError("Unable to get lat or lon of way center.")
629629
center_lat = Decimal(center_lat)
630630
center_lon = Decimal(center_lon)
631-
return (center_lat, center_lon)
631+
return center_lat, center_lon
632632

633633
@classmethod
634634
def get_center_from_xml_dom(cls, sub_child):

setup.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
#!/usr/bin/env python
22
import os
33
import sys
4+
from pathlib import Path
45
from setuptools import setup, find_packages
56

6-
base_dir = os.path.dirname(__file__)
7+
HERE = Path(__file__).resolve().parent
78

89
about = {}
9-
with open(os.path.join(base_dir, "overpy", "__about__.py")) as f:
10-
exec(f.read(), about)
10+
exec((HERE / "overpy" / "__about__.py").read_text(), about)
1111

12-
filename_readme = os.path.join(base_dir, "README.rst")
13-
if sys.version_info[0] == 2:
14-
import io
15-
fp = open(filename_readme, encoding="utf-8")
16-
else:
17-
fp = open(filename_readme, encoding="utf-8")
18-
long_description = fp.read()
12+
long_description = (HERE / "README.rst").read_text()
1913

2014
setup(
2115
name=about["__title__"],

tests/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import os
21
import sys
32
import threading
3+
from pathlib import Path
44
from threading import Lock
55

66
from socketserver import BaseRequestHandler, TCPServer, ThreadingMixIn
@@ -21,13 +21,12 @@ def handle(self):
2121
self.request.send(data)
2222

2323
@staticmethod
24-
def get_response(self):
24+
def get_response():
2525
yield b""
2626

2727

2828
def read_file(filename, mode="r"):
29-
filename = os.path.join(os.path.dirname(__file__), filename)
30-
return open(filename, mode).read()
29+
return (Path(__file__).resolve().parent / filename).open(mode).read()
3130

3231

3332
def new_server_thread(handle_cls, port=None):

tests/base_class.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111

1212
class BaseTestAreas:
13-
def _test_area01(self, result):
13+
@staticmethod
14+
def _test_area01(result):
1415
assert len(result.areas) == 4
1516
assert len(result.nodes) == 0
1617
assert len(result.relations) == 0
@@ -77,7 +78,8 @@ def _test_area01(self, result):
7778

7879

7980
class BaseTestNodes:
80-
def _test_node01(self, result):
81+
@staticmethod
82+
def _test_node01(result):
8183
assert len(result.nodes) == 3
8284
assert len(result.relations) == 0
8385
assert len(result.ways) == 0
@@ -151,7 +153,8 @@ def _test_node01(self, result):
151153

152154

153155
class BaseTestRelation:
154-
def _test_relation01(self, result):
156+
@staticmethod
157+
def _test_relation01(result):
155158
assert len(result.nodes) == 0
156159
assert len(result.relations) == 1
157160
assert len(result.ways) == 0
@@ -187,7 +190,8 @@ def _test_relation01(self, result):
187190
assert isinstance(relation.members[3], overpy.RelationNode)
188191
assert isinstance(relation.members[4], overpy.RelationWay)
189192

190-
def _test_relation02(self, result):
193+
@staticmethod
194+
def _test_relation02(result):
191195
assert len(result.nodes) == 3
192196
assert len(result.relations) == 1
193197
assert len(result.ways) == 1
@@ -240,7 +244,8 @@ def _test_relation02(self, result):
240244
assert way.id == 317146078
241245
assert member.ref == way.id
242246

243-
def _test_relation03(self, result):
247+
@staticmethod
248+
def _test_relation03(result):
244249
assert len(result.nodes) == 0
245250
assert len(result.relations) == 1
246251
assert len(result.ways) == 0
@@ -259,7 +264,8 @@ def _test_relation03(self, result):
259264
assert relation.center_lat == Decimal("50.8176646")
260265
assert relation.center_lon == Decimal("7.0208539")
261266

262-
def _test_relation04(self, result):
267+
@staticmethod
268+
def _test_relation04(result):
263269
assert len(result.nodes) == 0
264270
assert len(result.relations) == 1
265271
assert len(result.ways) == 0
@@ -289,7 +295,8 @@ def _test_relation04(self, result):
289295

290296

291297
class BaseTestWay:
292-
def _test_way01(self, result):
298+
@staticmethod
299+
def _test_way01(result):
293300
assert len(result.nodes) == 0
294301
assert len(result.relations) == 0
295302
assert len(result.ways) == 2
@@ -345,7 +352,8 @@ def _test_way01(self, result):
345352
assert way_ids[0] == 317146077
346353
assert way_ids[1] == 317146078
347354

348-
def _test_way02(self, result):
355+
@staticmethod
356+
def _test_way02(result):
349357
assert len(result.nodes) == 6
350358
assert len(result.relations) == 0
351359
assert len(result.ways) == 1
@@ -405,7 +413,8 @@ def _test_way02(self, result):
405413
assert len(way_ids) == 1
406414
assert way_ids[0] == 317146077
407415

408-
def _test_way03(self, result):
416+
@staticmethod
417+
def _test_way03(result):
409418
assert len(result.nodes) == 4
410419
assert len(result.relations) == 0
411420
assert len(result.ways) == 1

tests/test_exception.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class TestExceptions:
55
def test_element_data_wrong_type(self):
66
e = overpy.exception.ElementDataWrongType("from1")
77
assert e.type_expected == "from1"
8-
assert e.type_provided == None
8+
assert e.type_provided is None
99
assert isinstance(str(e), str)
1010

1111
e = overpy.exception.ElementDataWrongType("from2", "to2")
@@ -28,7 +28,7 @@ def test_overpass_bad_request(self):
2828

2929
def test_overpass_unknown_content_type(self):
3030
e = overpy.exception.OverpassUnknownContentType(None)
31-
assert e.content_type == None
31+
assert e.content_type is None
3232
assert str(e).startswith("No content")
3333

3434
e = overpy.exception.OverpassUnknownContentType("content")

tests/test_xml.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ def test_way04(self):
106106

107107

108108
class TestDataError:
109-
def _get_element_wrong_type(self):
109+
@staticmethod
110+
def _get_element_wrong_type():
110111
data = "<foo></foo>"
111112
import xml.etree.ElementTree as ET
112113
return ET.fromstring(data)

0 commit comments

Comments
 (0)