Switch core module to Python3
The core module (the relational algebra, not the interface) is now using Python3 Tests are passing, so it should be ok.
This commit is contained in:
@@ -29,11 +29,12 @@
|
||||
# The class used is defined in optimizer module.
|
||||
# A function will have to return the number of changes performed on the tree.
|
||||
|
||||
import parser
|
||||
|
||||
from cStringIO import StringIO
|
||||
from io import StringIO
|
||||
from tokenize import generate_tokens
|
||||
|
||||
|
||||
from relational import parser
|
||||
|
||||
sel_op = (
|
||||
'//=', '**=', 'and', 'not', 'in', '//', '**', '<<', '>>', '==', '!=', '>=', '<=', '+=', '-=',
|
||||
'*=', '/=', '%=', 'or', '+', '-', '*', '/', '&', '|', '^', '~', '<', '>', '%', '=', '(', ')', ',', '[', ']')
|
||||
@@ -620,4 +621,4 @@ general_optimizations = [
|
||||
specific_optimizations = [selection_and_product]
|
||||
|
||||
if __name__ == "__main__":
|
||||
print tokenize_select("skill == 'C' and id % 2 == 0")
|
||||
print (tokenize_select("skill == 'C' and id % 2 == 0"))
|
||||
|
@@ -25,8 +25,8 @@
|
||||
# The functions will always return a string with the optimized query, but if a parse tree was provided,
|
||||
# the parse tree itself will be modified accordingly.
|
||||
|
||||
import optimizations
|
||||
import parser
|
||||
from relational import optimizations
|
||||
from relational import parser
|
||||
|
||||
|
||||
# Stuff that was here before, keeping it for compatibility
|
||||
@@ -56,12 +56,12 @@ def optimize_all(expression, rels, specific=True, general=True, debug=None):
|
||||
steps.
|
||||
|
||||
Return value: this will return an optimized version of the expression'''
|
||||
if isinstance(expression, unicode):
|
||||
if isinstance(expression, str):
|
||||
n = tree(expression) # Gets the tree
|
||||
elif isinstance(expression, node):
|
||||
n = expression
|
||||
else:
|
||||
raise (TypeError("expression must be a unicode string or a node"))
|
||||
raise (TypeError("expression must be a string or a node"))
|
||||
|
||||
if isinstance(debug, list):
|
||||
dbg = True
|
||||
@@ -128,8 +128,8 @@ if __name__ == "__main__":
|
||||
print rels'''
|
||||
n = tree(u"π indice,qq,name (ρ age➡qq,id➡indice (P1-P2))")
|
||||
# n=tree("σ id==3 and indice==2 and name==5 or name<2(P1 * S1)")
|
||||
print n
|
||||
print n.toPython()
|
||||
print (n)
|
||||
print (n.toPython())
|
||||
|
||||
# print optimizations.selection_and_product(n,rels)
|
||||
|
||||
|
@@ -41,7 +41,8 @@
|
||||
# Language definition here:
|
||||
# https://github.com/ltworf/relational/wiki/Grammar-and-language
|
||||
import re
|
||||
import rtypes
|
||||
|
||||
from relational import rtypes
|
||||
|
||||
RELATION = 0
|
||||
UNARY = 1
|
||||
@@ -104,10 +105,10 @@ class node (object):
|
||||
# If the list contains only a list, it will consider the lower level list.
|
||||
# This will allow things like ((((((a))))) to work
|
||||
while len(expression) == 1 and isinstance(expression[0], list):
|
||||
expression = expression[0]
|
||||
expression = expression[0]
|
||||
|
||||
# The list contains only 1 string. Means it is the name of a relation
|
||||
if len(expression) == 1 and isinstance(expression[0], unicode):
|
||||
if len(expression) == 1:
|
||||
self.kind = RELATION
|
||||
self.name = expression[0]
|
||||
if not rtypes.is_valid_relation_name(self.name):
|
||||
@@ -125,7 +126,7 @@ class node (object):
|
||||
Since it searches for strings, and expressions into parenthesis are
|
||||
within sub-lists, they won't be found here, ensuring that they will
|
||||
have highest priority.'''
|
||||
for i in xrange(len(expression) - 1, -1, -1):
|
||||
for i in range(len(expression) - 1, -1, -1):
|
||||
if expression[i] in b_operators: # Binary operator
|
||||
self.kind = BINARY
|
||||
self.name = expression[i]
|
||||
@@ -142,7 +143,7 @@ class node (object):
|
||||
self.right = node(expression[i + 1:])
|
||||
return
|
||||
'''Searches for unary operators, parsing from right to left'''
|
||||
for i in xrange(len(expression) - 1, -1, -1):
|
||||
for i in range(len(expression) - 1, -1, -1):
|
||||
if expression[i] in u_operators: # Unary operator
|
||||
self.kind = UNARY
|
||||
self.name = expression[i]
|
||||
@@ -293,9 +294,6 @@ def tokenize(expression):
|
||||
'''This function converts an expression into a list where
|
||||
every token of the expression is an item of a list. Expressions into
|
||||
parenthesis will be converted into sublists.'''
|
||||
if not isinstance(expression, unicode):
|
||||
raise TokenizerException('expected unicode')
|
||||
|
||||
items = [] # List for the tokens
|
||||
|
||||
'''This is a state machine. Initial status is determined by the starting of the
|
||||
@@ -334,7 +332,7 @@ def tokenize(expression):
|
||||
|
||||
elif expression.startswith((u"σ", u"π", u"ρ")): # Unary 2 bytes
|
||||
items.append(expression[0:1])
|
||||
#Adding operator in the top of the list
|
||||
# Adding operator in the top of the list
|
||||
expression = expression[
|
||||
1:].strip() # Removing operator from the expression
|
||||
|
||||
@@ -345,7 +343,7 @@ def tokenize(expression):
|
||||
par = expression.find('(')
|
||||
|
||||
items.append(expression[:par].strip())
|
||||
#Inserting parameter of the operator
|
||||
# Inserting parameter of the operator
|
||||
expression = expression[
|
||||
par:].strip() # Removing parameter from the expression
|
||||
elif expression.startswith((u"÷", u"ᑎ", u"ᑌ", u"*", u"-")):
|
||||
@@ -417,8 +415,8 @@ def parse(expr):
|
||||
|
||||
if __name__ == "__main__":
|
||||
while True:
|
||||
e = unicode(raw_input("Expression: "), 'utf-8')
|
||||
print parse(e)
|
||||
e = str(raw_input("Expression: "))
|
||||
print (parse(e))
|
||||
|
||||
# b=u"σ age>1 and skill=='C' (peopleᐅᐊskills)"
|
||||
# print b[0]
|
||||
|
@@ -19,7 +19,7 @@
|
||||
#
|
||||
# This module provides a classes to represent queries
|
||||
|
||||
import parser
|
||||
from relational import
|
||||
|
||||
|
||||
class TypeException(Exception):
|
||||
@@ -29,10 +29,6 @@ class TypeException(Exception):
|
||||
class Query(object):
|
||||
|
||||
def __init__(self, query):
|
||||
|
||||
if not isinstance(query, unicode):
|
||||
raise TypeException('Expected unicode')
|
||||
|
||||
self.query = query
|
||||
self.tree = parser.tree(query)
|
||||
# TODO self.query_code = parser
|
||||
|
@@ -20,9 +20,10 @@
|
||||
# This module provides a classes to represent relations and to perform
|
||||
# relational operations on them.
|
||||
|
||||
from rtypes import *
|
||||
import csv
|
||||
|
||||
from relational.rtypes import *
|
||||
|
||||
|
||||
class relation (object):
|
||||
|
||||
@@ -46,13 +47,13 @@ class relation (object):
|
||||
self.header = header([])
|
||||
return
|
||||
# Opening file
|
||||
fp = file(filename)
|
||||
fp = open(filename)
|
||||
|
||||
reader = csv.reader(fp) # Creating a csv reader
|
||||
self.header = header(reader.next()) # read 1st line
|
||||
self.header = header(next(reader)) # read 1st line
|
||||
self.content = set()
|
||||
|
||||
for i in reader.__iter__(): # Iterating rows
|
||||
for i in reader: # Iterating rows
|
||||
self.content.add(tuple(i))
|
||||
|
||||
# Closing file
|
||||
@@ -123,7 +124,7 @@ class relation (object):
|
||||
try:
|
||||
if eval(expr, attributes):
|
||||
newt.content.add(i)
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
raise Exception(
|
||||
"Failed to evaluate %s\n%s" % (expr, e.__str__()))
|
||||
return newt
|
||||
@@ -190,7 +191,7 @@ class relation (object):
|
||||
newt = relation()
|
||||
newt.header = header(list(self.header.attributes))
|
||||
|
||||
for old, new in params.iteritems():
|
||||
for old, new in params.items():
|
||||
if (newt.header.rename(old, new)) == False:
|
||||
raise Exception('Unable to find attribute: %s' % old)
|
||||
|
||||
|
Reference in New Issue
Block a user