jtasker / python-weather-api

Automatically exported from code.google.com/p/python-weather-api
MIT License
1 stars 1 forks source link

Python3 - 'str' object has no attribute 'decode' #24

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Even using the latest commit from trunk, there are still issues with Python3 
compatibility. For instance:

>>> import pywapi
>>> pywapi.get_loc_id_from_weather_com('new york, ny')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/pywapi.py", line 788, in get_loc_id_from_weather_com
    search_string = unidecode(search_string.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'

I can confirm this with both py3.2 on Debian unstable and py3.3 on Ubuntu 
raring.

Original issue reported on code.google.com by A.Star...@gmail.com on 29 May 2013 at 10:15

GoogleCodeExporter commented 9 years ago
The above was fixed in the last commit to trunk, but now I'm seeing a new py3 
issue:

>>> pywapi.get_woeid_from_yahoo('New York')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/pywapi.py", line 905, in get_woeid_from_yahoo
    yahoo_woeid_result = json.loads(json_response)
  File "/usr/lib/python3.3/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.3/json/decoder.py", line 352, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: can't use a string pattern on a bytes-like object

Original comment by A.Star...@gmail.com on 1 Jun 2013 at 2:20

GoogleCodeExporter commented 9 years ago
This works with both 2.7 and 3.3 on Ubuntu:

--- pywapi-0.3.3~svn144.orig/pywapi.py  2013-05-31 09:39:56.000000000 -0400
+++ pywapi-0.3.3~svn144/pywapi.py   2013-05-31 22:30:54.852303489 -0400
@@ -900,7 +900,7 @@
     if charset.lower() != 'utf-8':
         json_response = handler.read().decode(charset).encode('utf-8')
     else:
-        json_response = handler.read()
+        json_response = handler.read().decode()
     handler.close()
     yahoo_woeid_result = json.loads(json_response)

Original comment by A.Star...@gmail.com on 1 Jun 2013 at 2:33

GoogleCodeExporter commented 9 years ago
Confirmed. That change works from an execution standpoint, but it returns 
incorrect results. Example:

correct results:

Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pywapi
>>> pywapi.get_where_on_earth_ids("Ironia, New Jersey")
{u'2427611': u'Ironia, NJ 07869, United States'}
>>> 

incorrect results:

Python 3.2.3 (default, Oct 19 2012, 19:53:16) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pywapi
>>> pywapi.get_where_on_earth_ids("Ironia, New Jersey")
{'2370405': 'Browns, NJ 07421, United States'}
>>> 

Original comment by jtas...@gmail.com on 2 Jun 2013 at 5:43

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r147.

Original comment by jtas...@gmail.com on 2 Jun 2013 at 6:17