<commit_before>import re, types

from setup import *
from Raba import *
import fields as RabaFields

#Usage:
#
#get all exons of a given transcript
#f = RabaQuery(namespace, Exon)
#f.addFilter(**{'transcript' : myTrans})
#f.addFilter(transcript = myTrans)
#f.addFilter("transcript = myTrans")
#exons = f.run()

class RabaQuery :

	def __init__(self, rabaClass) :
		self.reset(rabaClass)
	
	def reset(self, rabaClass) :
		assert isRabaClass(rabaClass)
		
		self.rabaClass = rabaClass
		self.filters = []
		self.tables = set()
		
		self._raba_namespace = self.rabaClass._raba_namespace
		self.con = RabaConnection(self._raba_namespace)
		
		#self.fctPattern = re.compile("\s*([^\s]+)\s*\(\s*([^\s]+)\s*\)\s*([=><])\s*([^\s]+)\s*")
		self.fieldPattern = re.compile("\s*([^\s\(\)]+)\s*([=><]|([L|l][I|i][K|k][E|e]))\s*(.+)")
		self.operators = set(['LIKE', '=', '<', '>', 'IS'])
		
	def addFilter_bck(self, *lstFilters, **dctFilters) :
		"add a new filter to the query"
		filters = {}
		for v in lstFilters :
			if isinstance(v, types.ListType) or isinstance(v, types.TupleType) :
				for v2 in v :
					res = self._parseInput(v2)
					filters[res[0]] = res[1]
			else :
				self._parseInput(v)
				filters[res[0]] = res[1]
		
		operators = set(['LIKE', '=', '<', '>', 'IS'])
		for k, v in dctFilters.items() :
			if k[-1].upper() not in operators :
				kk = '%s =' % k
			else :
				kk = k
			
			try :
				res = self._parseInput('%s %s' %(kk, v.getJsonEncoding()))
				vv = v.getJsonEncoding()
			except :
				res = self._parseInput('%s %s' %(kk, v))
				vv = v
			
			filters[res[0]] = vv
		self.filters.append(filters)

	def addFilter(self, *lstFilters, **dctFilters) :
		"add a new filter to the query"
		
		for k, v in dctFilters.iteritems() :
			sk = k.split(' ')
			if len(sk) == 2 :
				operator = sk[-1].strip()
				if operator not in self.operators :
					raise ValueError('Unrecognized operator %s' % operator)
				kk = '%s.%s'% (self.rabaClass.__name__, k)
			elif len(sk) == 1 :
				operator = "="
				kk = '%s.%s ='% (self.rabaClass.__name__, k)
			else :
				raise ValueError('Invalid field %s' % k)
			
			if isRabaObject(v) :
				vv = v.getJsonEncoding()
			else :
				vv = v
				<commit_msg>added comments<commit_after>import re, types

from setup import *
from Raba import *
import fields as RabaFields

#Usage:
#########
#Equivalent queries
#########
#get all exons of a given transcript
#f = RabaQuery(namespace, Exon)
#
#f.addFilter(**{'transcript' : myTrans})
#f.addFilter(transcript = myTrans)
#f.addFilter("transcript = myTrans")
#exons = f.run()
#
#of a given chromosome
#query by number
#f.addFilter("transcript->gene->chromosome = 22")
#query by object
#f.addFilter(**{'transcript->gene->chromosome' : myChro})
#exons = f.run()
#
##########
# AND/OR
##########
#f.addFilter(A1, A2, A3)
#f.addFilter(B1, B2)
#
#Means: (A1 AND A2 AND A3) OR (B1 AND B2)

class RabaQuery :

	def __init__(self, rabaClass) :
		self.reset(rabaClass)
	
	def reset(self, rabaClass) :
		assert isRabaClass(rabaClass)
		
		self.rabaClass = rabaClass
		self.filters = []
		self.tables = set()
		
		self._raba_namespace = self.rabaClass._raba_namespace
		self.con = RabaConnection(self._raba_namespace)
		
		#self.fctPattern = re.compile("\s*([^\s]+)\s*\(\s*([^\s]+)\s*\)\s*([=><])\s*([^\s]+)\s*")
		self.fieldPattern = re.compile("\s*([^\s\(\)]+)\s*([=><]|([L|l][I|i][K|k][E|e]))\s*(.+)")
		self.operators = set(['LIKE', '=', '<', '>', 'IS'])
		
	def addFilter_bck(self, *lstFilters, **dctFilters) :
		"add a new filter to the query"
		filters = {}
		for v in lstFilters :
			if isinstance(v, types.ListType) or isinstance(v, types.TupleType) :
				for v2 in v :
					res = self._parseInput(v2)
					filters[res[0]] = res[1]
			else :
				self._parseInput(v)
				filters[res[0]] = res[1]
		
		operators = set(['LIKE', '=', '<', '>', 'IS'])
		for k, v in dctFilters.items() :
			if k[-1].upper() not in operators :
				kk = '%s =' % k
			else :
				kk = k
			
			try :
				res = self._parseInput('%s %s' %(kk, v.getJsonEncoding()))
				vv = v.getJsonEncoding()
			except :
				res = self._parseInput('%s %s' %(kk, v))
				vv = v
			
			filters[res[0]] = vv
		self.filters.append(filters)

	def addFilter(self, *lstFilters, **dctFilters) :
		"add a new filter to the query"
		for k, v in dctFilters.iteritems() :
			sk = k.split(' ')
			if len(sk) == 2 :
				operator = sk[-1].strip()
				if operator not in self.operators :
					raise ValueError('Unrecognized operator %s' % operator)
				kk = '%s.%s'% (self.rabaClass.__name__, k)
			elif len(sk) == 1 :
				operator = "="
				kk = '%s.%s ='% (self.rabaClass.__name__, k)
			else :
				raise ValueError('Invalid field %s' % k)
			
			if isRabaObject(v) :
				vv = v.getJsonEncoding()
			else :
				vv = v
				
{'fraction_non_alphanumeric': 0.19865461425268027, 'fraction_numerical': 0.007147361782636115, 'mean_word_length': 3.2942238267148016, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3766469', 'n_tokens_mistral': 2133, 'n_tokens_neox': 1839, 'n_words': 413}