Python Forum
parsing logical expression with pyparsing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parsing logical expression with pyparsing
#1
I try to create JSON from logical expression as input from user.


#input from user (for example)
string = "Apple == 5 & (Plum == 7 | Pear == 8)"

string = string.replace('==', ' eq ')
string = string.replace('<>', ' ne ')
string = string.replace('>' , ' gt ')
string = string.replace('>=', ' ge ')
string = string.replace('<' , ' lt ')
string = string.replace('<=', ' le ')
string = string.replace('&' , ' and ')
string = string.replace('|' , ' or ')
string = string.replace('!=', ' not ')

print(string)
# "Apple eq 5 and (Plum eq 7 or Pear eq 8)"

import pyparsing as pp

    operator = pp.Regex(r">=|<=|!=|>|<|=|eq").setName("operator")
    number = pp.Regex(r"[+-]?\d+(:?\.\d*)?(:?[eE][+-]?\d+)?")
    identifier = pp.Word(pp.alphas, pp.alphanums + "_")
    comparison_term = identifier | number 
    condition = pp.Group(comparison_term("Field") + operator("Operator") + comparison_term("Value"))
    expr = pp.operatorPrecedence(condition("Filters"), [
                                ("and", 2, pp.opAssoc.LEFT, ),
                                ("or", 2, pp.opAssoc.LEFT, ),
                                ("not", 1, pp.opAssoc.RIGHT, ),
                                ]).setResultsName("Filter")

pars = expr.parseString(string).asXML()
   
import xmltodict, json
    o = xmltodict.parse(pars)

    with open("C:\\Users\\palo173\\Desktop\\example.json","w") as f:
        json.dump(o,f)
f.close()
My result in JSON after my code above is:

Output:
{ "Filter": { "Filter": { "Filters": [ { "Field": "Apple", "Operator": "eq", "Value": "5" }, { "Filters": [ { "Field": "Plum", "Operator": "eq", "Value": "7" }, { "Field": "Pear", "Operator": "eq", "Value": "8" } ], "ITEM": "or" } ], "ITEM": "and" } } }
But required result in JSON is:

Output:
{ "CategoryId": 0, "FilterRequest": { "Page": 1, "PageSize": 10, "Filter": { "Logic": "and", "Filters": [ { "Logic": "or", "Filters": [ { "Field": "Plum", "Operator": "eq", "Value": "7" }, { "Field": "Pear", "Operator": "eq", "Value": "8" } ] }, { "Field": "Apple", "Operator": "eq", "Value": "5" } ] } } }
I think that I am fault in my "pyparsing part" of code. Could you help me with it? Sad
Reply
#2
check this out, similar application: https://stackoverflow.com/a/32975160
Reply
#3
(May-10-2019, 03:58 PM)Larz60+ Wrote: check this out, similar application: https://stackoverflow.com/a/32975160

I looked at it, but it didn't really help me.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with Logical error processing List of strings dmc8300 3 1,081 Nov-27-2022, 04:10 PM
Last Post: Larz60+
  Greedy algorithms on logical problems Opensourcehacker 0 1,533 Nov-22-2020, 05:12 PM
Last Post: Opensourcehacker
  Unable to bit shift and logical OR bytes and ints? MysticLord 7 6,980 Sep-01-2020, 03:31 PM
Last Post: deanhystad
  Basic logical errors cpopescu 3 2,049 Jun-03-2020, 11:30 AM
Last Post: menator01
  Python logical operator AND rasec70 4 2,521 May-07-2020, 03:40 PM
Last Post: pyzyx3qwerty
  Pass results of expression to another expression cmdr_eggplant 2 2,279 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  pyparsing pproblems standenman 2 5,049 Feb-04-2018, 03:57 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020