Skip to content

Commit

Permalink
Precompiled to array
Browse files Browse the repository at this point in the history
  • Loading branch information
nickelkr committed Feb 2, 2016
1 parent e17a350 commit 3d0c9c6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
27 changes: 17 additions & 10 deletions tests/test_yql.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,38 @@ def setUp(self):

def test_single_select(self):
self.yql.select('*')
self.assertEqual(self.yql.compiled(), 'select * from yahoo.finance.quotes')
self.assertIn('select * from yahoo.finance.quotes', self.yql.parts())

def test_multi_select(self):
pass

def test_select_all(self):
pass

def test_single_where(self):
self.yql.where('symbol')
self.assertEqual(self.yql.compiled(), 'where symbol')
self.assertIn('where symbol', self.yql.parts())

def test_single__in(self):
self.yql._in('TSLA')
self.assertEqual(self.yql.compiled(), 'in ("TSLA")')
self.assertIn('in ("TSLA")', self.yql.parts())

def test_chaining(self):
compiled = 'select * from yahoo.finance.quotes where symbol in ("TSLA")'
self.yql.select('*').where('symbol')._in('TSLA')
self.assertEqual(self.yql.compiled(), compiled)
self.assertIn('select * from yahoo.finance.quotes', self.yql.parts())
self.assertIn('where symbol', self.yql.parts())
self.assertIn('in ("TSLA")', self.yql.parts())

def test_sequence(self):
compiled = 'select * from yahoo.finance.quotes where symbol in ("TSLA")'
self.yql.select('*')
self.yql.where('symbol')
self.yql._in('TSLA')
self.assertEqual(self.yql.compiled(), compiled)
self.assertIn('select * from yahoo.finance.quotes', self.yql.parts())
self.assertIn('where symbol', self.yql.parts())
self.assertIn('in ("TSLA")', self.yql.parts())

def test_url_safe(self):
urlable = 'select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22TSLA%22)'
self.yql.select('*').where('symbol')._in('TSLA')
def test_symbol(self):
pass



Expand Down
29 changes: 19 additions & 10 deletions yfi/yql.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,45 @@
class Yql:

def __init__(self):
self.compiled_str = ""
self.terms = []
self.compiled_str = None
self.conn = http.client.HTTPSConnection("query.yahooapis.com")
self.endpoint = "/v1/public/yql?q="
self.store = "store:https://datatables.org/alltableswithkeys"
self.format = "json"

def parts(self):
return self.terms

def select(self, *itms):
self.compile("select %s from yahoo.finance.quotes" % itms)
self.terms.append("select %s from yahoo.finance.quotes" % itms)
return self

def where(self, *whrs):
self.compile("where %s" % whrs)
self.terms.append(" where %s" % whrs)
return self

def _in(self, *lst):
self.compile('in ("%s")' % lst)
self.terms.append(' in ("%s")' % lst)
return self

def compile(self, part):
if self.compiled_str:
part = " " + part
self.compiled_str += part
def compile(self):
cs = ""
for term in self.terms:
cs += term
self.compiled_str = urllib.parse.quote(cs)
return self

def compiled(self):
return self.compiled_str

def exec(self):
s = urllib.parse.quote(self.compiled_str)
if self.compiled_str is None:
self.compile()

s = "%s&format=%s" % (self.compiled_str, self.format)
s = "%s%s&env=%s" % (self.endpoint, s, urllib.parse.quote("store:https://datatables.org/alltableswithkeys"))
print(s)

self.conn.request("GET", s)
r = self.conn.getresponse()
print(r.read())
Expand Down

0 comments on commit 3d0c9c6

Please sign in to comment.