Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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;
share|improve this question
    
at a first glance, it looks like your code is not properly indented. –  BenjaminGolder Mar 17 at 18:33
    
what do you mean inteded, im not so familiar in python. what should I change? –  Balazs Mar 17 at 18:35
    
indented, this page discusses python indentation: docs.python.org/release/2.5.1/ref/indentation.html, and this discusses indentation generally: en.wikipedia.org/wiki/Indent_style –  BenjaminGolder Mar 17 at 18:37
    
so the return part of the code was missed? –  Balazs Mar 17 at 18:46
    
Nothing to do with PostgreSQL, looks like a pure Python syntax issue. –  Craig Ringer Mar 18 at 0:02
add comment

closed as off-topic by Jason Scheirer, Fezter, Paul, PolyGeo, Simbamangu Mar 18 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.

1 Answer

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.

share|improve this answer
    
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 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 at 1:10
add comment

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