0

I have python 2.7 and I have weather script which returns temperature infos, I would like to implement this script into PostgreSQL. I always got this error: DETAIL: SyntaxError: invalid syntax (<string>, line 10)

code:

    CREATE OR REPLACE FUNCTION GetWeather(lon float, lat float)
    RETURNS float
    AS $$
    import urllib2
    import simplejson as json

    data = urllib2.urlopen(
    "http://api.openweathermap.org/data/2.1/find/station?lat=%s&lon=%s&cnt=1"% (lat, lon))
    js_data = json.load(data)
    if js_data['cod'] == '200': 
    if int(js_data['cnt'])>0: 
    station = js_data['list'][0] 
    print 'Data from weather station %s' %station['name']
    if 'main' in station: 
    if 'temp' in station['main']: 
    temperature = station['main']['temp'] - 273.15 
    else:temperature = None
    else:temperature = None

    return temperature

    $$ LANGUAGE plpythonu;

I tried this version too, it is working in pure python but not in postgresql

CREATE OR REPLACE FUNCTION GetWeather(lon float, lat float)
    RETURNS float
    AS $$
    import urllib2
    import simplejson as json

    def get_temp(lat, lon):
    data = urllib2.urlopen(
    "http://api.openweathermap.org/data/2.1/find/station?lat=%s&lon=%s&cnt=1"% (lat, lon))
    js_data = json.load(data)
    try:
    return js_data['list'][0]['main']['temp']
    except (KeyError, IndexError):
        return None

    $$ LANGUAGE plpythonu;

closed as off-topic by Jason Scheirer, Fezter, Paul, PolyGeo, Simbamangu Mar 18 '14 at 5:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development are off-topic here unless they relate directly to Geographical Information Systems, but they can be asked on Stack Overflow." – Jason Scheirer, Fezter, Paul, PolyGeo, Simbamangu
If this question can be reworded to fit the rules in the help center, please edit the question.

3

Python code must be indented correctly to be valid. For example, taking a wild guess at the intent of your code, it might be something like:

import urllib2
import simplejson as json

data = urllib2.urlopen(
"http://api.openweathermap.org/data/2.1/find/station?lat=%s&lon=%s&cnt=1"% (lat, lon))
js_data = json.load(data)
if js_data['cod'] == '200': 
    if int(js_data['cnt'])>0: 
        station = js_data['list'][0] 
        print 'Data from weather station %s' %station['name']
        if 'main' in station: 
            if 'temp' in station['main']: 
                temperature = station['main']['temp'] - 273.15 
            else:
                temperature = None
            return temperature

Separately, I recommend that you avoid making external network requests like HTTP requests from within the database system. If there are DNS or connectivity problems, issues with the remote end performance, etc, it'll tend to really hurt your database's performance. Instead, do such requests in the application and pass the results into the database for processing.

  • Not running: ERROR: KeyError: 'cnt' CONTEXT: Traceback (most recent call last): PL/Python function "ido", line 10, in <module> if int(js_data['cnt'])>0: – Balazs Mar 26 '14 at 20:56
  • @Balazs I can't fix all your code for you, I can only explain the immediate problem that caused the problem. There's no substitute for actually learning Python. It seems like the data returned from the web service call isn't what your code expects, but I couldn't possibly tell you why, what your code thinks it's doing, etc. – Craig Ringer Mar 27 '14 at 1:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.