Skip to content

Commit aa0fc31

Browse files
authored
Merge pull request #16 from onesteinbv/add_python3_compatibility
Add compatibility with Python 3
2 parents 0bd1a17 + 81f9286 commit aa0fc31

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This library supports only the v2 api.
2020

2121
pyPostcode consists of a single file (pyPostcode.py) that you can put in your python search path or in site-packages (or dist-packages depending on the platform)
2222
You can also simply run it by putting it in the same directory as you main script file or start a python interpreter in the same directory.
23-
pyPostcode works with Python 2.7.x (you're welcome to test other versions)
23+
pyPostcode works with Python 2.7.x and 3.5.x (you're welcome to test other versions)
2424

2525
### API-key
2626

pyPostcode/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
pyPostcode is an api wrapper for http://postcodeapi.nu
88
'''
99

10+
try:
11+
from urllib.request import urlopen, Request # for Python 3
12+
except ImportError:
13+
from urllib2 import urlopen, Request # for Python 2
1014

11-
import urllib2
1215
import json
1316
import logging
1417

1518

16-
__version__ = '0.4'
19+
__version__ = '0.5'
1720

1821

1922
class pyPostcodeException(Exception):
@@ -61,7 +64,7 @@ def request(self, path=None):
6164
"X-Api-Key": self.api_key,
6265
}
6366

64-
result = urllib2.urlopen(urllib2.Request(
67+
result = urlopen(Request(
6568
self.url + path, headers=headers,
6669
))
6770

@@ -70,6 +73,8 @@ def request(self, path=None):
7073

7174
resultdata = result.read()
7275

76+
if isinstance(resultdata, bytes):
77+
resultdata = resultdata.decode("utf-8") # for Python 3
7378
jsondata = json.loads(resultdata)
7479

7580
if self.api_version >= (2, 0, 0):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
setup(
33
name = 'pyPostcode',
44
packages = ['pyPostcode'],
5-
version = '0.3',
5+
version = '0.5',
66
description = 'Request information about Dutch addresses from the PostcodeApi.nu API',
77
author = 'Stefan Jansen',
88
author_email = 'stefan@steffex.net',

0 commit comments

Comments
 (0)