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;
5
  • at a first glance, it looks like your code is not properly indented. Commented Mar 17, 2014 at 18:33
  • what do you mean inteded, im not so familiar in python. what should I change? Commented Mar 17, 2014 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 Commented Mar 17, 2014 at 18:37
  • so the return part of the code was missed? Commented Mar 17, 2014 at 18:46
  • Nothing to do with PostgreSQL, looks like a pure Python syntax issue. Commented Mar 18, 2014 at 0:02

1 Answer 1

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.

2
  • 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: Commented Mar 26, 2014 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. Commented Mar 27, 2014 at 1:10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.