YQL Open Table
Table
This class represents the root element of a YQL Table Definition File. You can read about the full documentation here
Definition
Table(name, author, apiKeyURL, documentationURL, sampleQuery=[], description=None, table_attr=None, bindings=[])
>>> from myql.contrib.table import Table
>>> mytable = Table('mytable', 'Josue Kouka', 'http://josuerunel.org/mytable/','http://josuerunel.org/mytable/docs.html',sampleQuery = ['SELECT * FROM mytable', 'SELECT name FROM mytable WHERE id = 77'], description='Just a simple tabe', table_attr={'xmlns':'http://query.yahooapis.com/v1/schema/table.xsd', 'securityLevel':'any', 'https':'false'})
Methods
Table.addBinder(binder_object)
Add a binder to the table
>>> mytable.addBinder(select_binder)
Table.removeBinder(binder_object)
Remove a binder from the table
>>> mytable.removeBinder(select_binder)
Table.save(name=None, path=None)
Save the table as a xml file with Table Object name if name is not provided. If path, saves the xml file to the specified location
>>> mytable.save(name='test',path='/var/www/josuebrunel.org/mytable/')
Inputs
There are 3 kind of inputs as described in the documentation :
- key
- map
- value
Definitions
-
InputKey(id, type, paramType, like='', required=False, default='', private=False, const=False, batchable=False, maxBatchItems=0)
song = InputKey(id='song', type='xs:string', paramType='path', required=True)
-
InputValue(id, type, paramType, like='', required=False, default='', private=False, const=False, batchable=False, maxBatchItems=0)
song = InputValue(id='song', type='xs:string', paramType='path', required=True, const='12' )
-
InputMap(id, type, paramType, like='', required=False, default='', private=False, const=False, batchable=False, maxBatchItems=0)
All of those classes are based on BaseInput.
like is a replacement for as which is a python keyword. In the xml file, as will be displayed.
Methods
No methods defined
Paging
This class describe a paging element. A paging is deifined by one of the 3 classes below :
- PagingPage
- PagingUrl
- PagingOffset
Check out the full documentation
Definitions
-
PagingPage(start={}, pageSize={}, total={})
>>> mypage = PagingPage({'id': 'ItemPage', 'default': '1'}, {'id':'Count' ,'max':'25'},{'default': '10'})
-
PagingUrl(nextpage)
>>> mypage = PagingUrl("ysearchresponse.nextpage")
-
PagingOffset(matrix, start={}, pageSize={}, total={})
>>> mypage = PagingOffset({'id': 'ItemPage', 'default': '1'}, {'id':'Count' ,'max':'25'},{'default': '10'})
All these classes above subclass BasePaging .
Methods
No methods defined
Binder
This class represents an element under
- select
- insert
- update
- delete
You can read about the full documentation here
Definition
Binder(name, itemPath, produces, pollingFrequencySeconds=0, urls=[], inputs=[], paging=None)
>>> select = Binder('select', 'products.product', 'xml')
Methods
Binder.addInput(input_object) :
Add an input object to the binder
>>> song = InputKey(id='song', type='xs:string', paramType='path', required=True)
>>> select.addBinder(song)
Binder.removeInput(input_id, input_type)
Remove an input object from the binder. input_type may be key, value or map
>>> select.removeBinder('song','select')
Binder.addUrl(url)
Add an url to the binder
>>> select.addUrl('http://lol.com/{song}')
Binder.removeUrl(url)
Remove an url from the binder
>>> select.removeUrl('http://lol.com/{song}')
Binder.addPaging(paging_instance)
Add a paging to the binder
>>> mypage = PagingUrl("ysearchresponse.nextpage")
>>> select.addPaging(mypage)
Binder.removePaging()
Remove a paging from the binder
>>> select.removePaging()
BinderFunction
This class represents a stored function. Read the full documentation here
Definition
BinderFunction(func_name, func_code='', func_file=None, inputs=[])
- func_name : function name
- func_code : function code passed as string
- func_file : file containing the function
- inputs : list of inputs
>>> myfunc = BinderFunction('concat', func_code="console.log('Hello Josh!!!')")
Methods
As Binder, BinderFunction is a subclass of BaseBinder. They both share the same methods
Using MetaClasses to define a Table
BinderModel and TableModel are the only classes to keep in mind here. They're respectively subclasses of BinderMeta and TableMeta. Those last two help providing a powerful API to define YQL Table.
They say "A picture is worth a thousand of words" and I say "A code snippet is worth ..." . You got it (^_^).
Copy and paste the code snippet below in a example.py
from myql.contrib.table import BinderModel, InputKey, PagingPage, PagingUrl, InputValue, BinderFunction
from myql.contrib.table import TableModel, BinderFrom
class SelectBinder(BinderModel):
name = 'select'
itemPath = 'products.product'
produces = 'xml'
pollingFrequencySeconds = 30
urls = ['http://lol.com/services?artist={artis}','http://lol.com/services/song={song}']
paging = PagingPage({'id': 'ItemPage', 'default': '1'}, {'id':'Count' ,'max':'25'},{'default': '10'})
artist = InputKey(id='artist', type='xs:string', paramType='path')
song = InputKey(id='song', type='xs:string', paramType='path', required=True)
class InsertBinder(BinderModel):
name = 'insert'
itemPath = 'products.product'
produces = 'xml'
pollingFrequencySeconds = 30
urls = ['http://lol.com/services?artist={artis}','http://lol.com/services/song={song}']
paging = PagingUrl(nextpage={'path':'yqlsearch.nextpage'})
artist = InputKey(id='artist', type='xs:string', paramType='path')
song = InputValue(id='song', type='xs:string', paramType='path', required=True)
class TestTable(TableModel):
name = 'Test'
author = 'Josue Kouka'
apiKeyURL = 'http://josuebrunel.org/api'
documentationURL = 'http://josuebrunel.org/doc.html'
description = "Just a test table"
sampleQuery = ['SELECT * FROM mytable','SELECT name FROM mytable WHERE id=4656', "SELECT * FROM mytable WHERE name='Josh'"]
select = BinderFrom(SelectBinder)
insert = BinderFrom(InsertBinder)
func1 = BinderFunction('concat', func_code="console.log('Hello Josh!!!')")
TestTable.table.save(name='Example')
Run
$ python example.py
$ cat Example.xml
<?xml version="1.0" ?>
<table https="false" securityLevel="any" xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
<meta>
<apiKey>http://josuebrunel.org/api</apiKey>
<author>Josue Kouka</author>
<description>Just a test table</description>
<documentationURL>http://josuebrunel.org/doc.html</documentationURL>
<sampleQuery>SELECT * FROM mytable</sampleQuery>
<sampleQuery>SELECT name FROM mytable WHERE id=4656</sampleQuery>
<sampleQuery>SELECT * FROM mytable WHERE name='Josh'</sampleQuery>
</meta>
<bindings>
<function name="concat">
<execute>
![CDATA]console.log('Hello Josh!!!')]]
</execute>
</function>
<insert itemPath="products.product" produces="xml">
<urls>
<url>http://lol.com/services?artist={artis}</url>
<url>http://lol.com/services/song={song}</url>
</urls>
<inputs>
<value id="song" required="true" type="xs:string"/>
<key id="artist" type="xs:string"/>
</inputs>
<paging model="url">
<nextpage path="yqlsearch.nextpage"/>
</paging>
</insert>
<select itemPath="products.product" produces="xml">
<urls>
<url>http://lol.com/services?artist={artis}</url>
<url>http://lol.com/services/song={song}</url>
</urls>
<inputs>
<key id="song" required="true" type="xs:string"/>
<key id="artist" type="xs:string"/>
</inputs>
<paging model="page">
<start default="1" id="ItemPage"/>
<total default="10"/>
<pageSize id="Count" max="25"/>
</paging>
</select>
</bindings>
</table>
Voila, i think we're done here