Question

My code:

import yql
y = yql.Public()
query = 'SELECT * FROM yahoo.finance.option_contracts WHERE symbol="SPY"'
y.execute(query)

Result:

yql.YQLError: No definition found for Table yahoo.finance.option_contracts

I know that the table exists because I can test the query at http://developer.yahoo.com/yql/console/ and it works. What am I missing?

Update: I posted the url to the console but not the query I tried in the console. The query is now attached.

http://goo.gl/mNXwC
Était-ce utile?

La solution

Since the yahoo.finance.option_contracts table is a Community Open Data Table you will want to include it as part of the environment for the query. The easiest way to do that is to load up the environment file for all community tables; just like clicking "Show Community Tables" in the YQL console.

One would normally do that by specifying an env=... parameter in the YQL query URL, or (as you have done) with a use clause in the query itself.

The Python library that you are using lets you pass in the environment file as an argument to execute().

import yql
y = yql.Public()
query = 'SELECT * FROM yahoo.finance.option_contracts WHERE symbol="SPY"'
y.execute(query, env="store://datatables.org/alltableswithkeys")

Here's an example of extending yql.Public to be able to define the default environment on instantiation.

class MyYql(yql.Public):

    def __init__(self, api_key=None, shared_secret=None, httplib2_inst=None, env=None):
        super(MyYql, self).__init__(api_key, shared_secret, httplib2_inst)
        self.env = env if env else None

    def execute(self, query, params=None, **kwargs):
        kwargs["env"] = kwargs.get("env", self.env)
        return super(MyYql, self).execute(query, params, **kwargs);

It can be used like:

y = MyYql(env="store://datatables.org/alltableswithkeys")
query = 'SELECT * FROM yahoo.finance.option_contracts WHERE symbol="SPY"'
r = y.execute(query)

You can still override the env in an individual call to y.execute() if you need to.

Autres conseils

Amending query to the following is what works.

query = 'use "http://www.datatables.org/yahoo/finance/yahoo.finance.option_contracts.xml" as foo; SELECT * FROM foo WHERE symbol="SPY"'

More elegant solutions might exist. Please share if such do. Thanks.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top