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.