RegEx Parse Error by Parsley Python

2024/10/12 18:15:38

I have made a simple parser for simple queries, to fetch data from a datastore. The operands I have used are <,<=,>,>=,==,!= The Parser works fine for every operand except for < I am a bit surprised with the behavior since the regex for each operand are almost identical. I am unable to find what i might have done wrong.

Code:

import parsleyfrom FieldSet import FieldSet
from Field import Fieldclass QueryParser(object):def __init__(self,getter):self.__defineParser()self.getter = getterdef __defineParser(self):self.parseField = parsley.makeGrammar("""neq = <letterOrDigit*>:field ws '!=' ws <letterOrDigit*>:value ->Field(field,value,'!=')eq = <letterOrDigit*>:field  ws '==' ws <letterOrDigit*>:value ->Field(field,value,'==')lte = <letterOrDigit*>:field ws '<=' ws <digit*'.'?digit*>:value ->Field(field,value,'<=')gte = <letterOrDigit*>:field ws '>=' ws <digit*'.'?digit*>:value ->Field(field,value,'>=')lt = <letterOrDigit*>:field ws '<' ws <digit*'.'?digit*>:value ->Field(field,value,'<')gt = <letterOrDigit*>:field ws '>' ws <digit*'.'?digit*>:value ->Field(field,value,'>')fieldCondition = ws (neq | eq | lte | lt | gte |gt):evalTuple ws -> evalTuple""",{'Field':Field}) self.parse = parsley.makeGrammar("""neq = <letterOrDigit* ws '!=' ws letterOrDigit*>:s ->str(s)eq = <letterOrDigit* ws '==' ws letterOrDigit*>:s ->str(s)lte = <letterOrDigit* ws '<=' ws digit*'.'?digit*>:s->str(s)gte = <letterOrDigit* ws '>=' ws digit*'.'?digit*>:s ->str(s)lt = <letterOrDigit* ws '<' ws digit*'.'?digit*>:s->str(s)gt = <letterOrDigit* ws '>' ws digit*'.'?digit*>:s ->str(s)parens = ws '(' ws expr:e ws ')' ws -> evalue = ws parens | neq | eq | lte | lt | gte |gt wsws = ' '*and = 'AND' ws expr3:n -> ('AND', n)or = 'OR' ws expr3:n -> ('OR', n)not = 'NOT' ws value:n -> ('NOT', n)checknot = ws (value|not)andor = ws (and | or)expr = expr3:left andor*:right -> performOperations(left, right)expr3 = ws checknot:right -> getVal(right)""", {"performOperations": self.performOperations,'getVal':self.getVal})def processQuery(self,field):if type(field) is FieldSet:return fieldelif type(field) is Field:elements = FieldSet(field,self.getter)return elementselse:raise Exception("Invalid Input")def performOperations(self,start, pairs):result = startif type(result) is Field:result = self.processQuery(start)for op, value in pairs:if op == 'AND':secondField = self.processQuery(value)result.union(secondField)elif op == 'OR':secondField = self.processQuery(value)result.intersection(secondField)print type(result)print result.getSet() return result'''This functions will be returning sets'''def getVal(self,field):if type(field) is tuple:_op,value = fieldresult = self.parseField(value).fieldCondition()result.negate()elif type(field) is FieldSet:result = fieldelse:result = self.parseField(field).fieldCondition()print "res",resultreturn result    def getResults(self,query):return self.parse(query).expr().getSet()
if __name__=="__main__":pae = QueryParser("POP")print pae.getResults("lame>10")

For Every other Operand the output is something like this

res lame<10
set(['-&-lame<10'])
set(['-&-lame<10'])

But for '>' the output/error is as:

Traceback (most recent call last):File "D:\Nother\engine\parser\QueryParser.py", line 107, in <module>print pae.getResults("lame>10")File "D:\Nother\engine\parser\QueryParser.py", line 104, in getResultsreturn self.parse(query).expr().getSet()File "D:\Nother\lookup_env\lib\site-packages\parsley.py", line 98, in invokeRuleraise err
ometa.runtime.EOFError: 
lame>10
^
Parse error at line 2, column 0: end of input. trail: [digit]

I suppose its trying to find some digit and its not able to.But similar regex has been written for other operands and it does not result in an error, which seems to be weird. Would appreciate if anyone could look into this and tell me where I am wrong.

Answer

The problem with this regEx was how I handled the value regEx.

value = ws parens | neq | eq | lte | lt | gte |gt ws

Here I have added whitespace before and after all the possible options, thus instead of whitespace being optional it was being enforced. So rather than using ws here at value, I used ws where I was using the value token for searching , and changed value to

value = parens | neq | eq | lte | lt | gte |gt

This Solved my problem.

https://en.xdnf.cn/q/118168.html

Related Q&A

Accessing Bangla (UTF-8) string by index in Python

I have a string in Bangla and Im trying to access characters by index.# -*- coding: utf-8 -*- bstr = "তরদজ" print bstr # This line is working fine for i in bstr:print i, # question marks …

Computing KL divergence for many distributions

I have a matrix of test probability distributions:qs = np.array([[0.1, 0.6], [0.9, 0.4] ])(sums up to 1 in each column) and "true" distribution:p = np.array([0.5, 0.5])I would like to calcula…

Expanding mean over multiple series in pandas

I have a groupby object I apply expanding mean to. However I want that calculation over another series/group at the same time. Here is my code:d = { home : [A, B, B, A, B, A, A], away : [B, A,A, B, A, …

Moving window sum on a boollean array, with steps.

Im struggling with creating a moving window sum function that calculates the number of True values in a given numpy Boolean array my_array, with a window size of n and in jumping steps of s.For example…

Python - take the time difference from the first date in a column

Given the date column, I want to create another column diff that count how many days apart from the first date.date diff 2011-01-01 00:00:10 0 2011-01-01 00:00:11 0.000011 …

(Django) Limited ForeignKey choices by Current User

Update Thanks to Michael I was able to get this to work perfectly in my CreateView, but not in the UpdateView. When I try to set a form_class it spits out an improperly configured error. How can I go a…

Parse a custom text file in Python

I have a text to be parsed, this is a concise form of the text.apple {type=fruitvarieties {color=redorigin=usa} }the output should be as shown belowapple.type=fruit apple.varieties.color=red apple.vari…

Logs Dont Overwrite

Im using Pythons logging.config module to configure and use a logging tool in my project.I want my log files to overwrite each time (not append), so I set my YAML configuration file like this:# logging…

How to upload local files to Firebase storage from Jupyter Notebook using Python

Since I guess importing google.cloud.storage might be a very first step to set API connecting the firebase storage, what I did first is to install google-cloud on Ubuntu like this:$ pip install --upgra…

How can scrapy crawl more urls?

as we see:def parse(self, response):hxs = HtmlXPathSelector(response)sites = hxs.select(//ul/li)items = []for site in sites:item = Website()item[name] = site.select(a/text()).extract()item[url] = site.…