0

So I have a problem I'm trying to solve in which I have two fundamental classes CMIX and C, whereby CMIX contains a list of instances of C: [C1, C2, .... Cn] which will be manipulated by a custom grammar (string values operators - "+", "-", "", "(", ")", but in the sense that there is the ability to embed extra logic into operations. For example, a [C1 + C2 + ... + Cn] composition would call C.foo() for each C1,C2,...Cn, and then add those results. Similarly, a composition of grammar terms with the object set, e.g.: [C1 "" "(" C1 "+" C2 "*" "("C2"+"C2")"")"] would call C1.foo(), C2.foo(), and then combine those results according to normal mathematical evaluation. Initially, I just want to deal with basic math operations like * + ( ) but would like to be able to extend it later for custom (non math) terms .

A basic representative sketch of the class functionality is as follows (this is mainly conceptual; especially the CMIX, as I'm unsure of the best way to go about the parsing syntax logic)

class CMIX():
   self.C_list = []  # List of instances of type C
   self.grammar = []
   def my_val(self, val):
      ## call every .foo(val=val) in self.C_list, and apply the grammar rules accordingly.

class C():
   def __init__(self, location):
      self.location= location
   def my_val(self, val):
      return val * 2 
   def check_location(self):
      if self.location == "antarctica":
         return "cold"
      elif self.location == "moon":
         return "bouncy"
      else:
         return "lost"

for example, say we have defined the global grammar operator

+ means if C.check_location() == "lost" then return -100*C.my_val(val) otherwise just return C.m

the grammar said C1 + C2, and C1.location="lost", C2.location="antarctica", then CMIX.my_val(20) would evaluate like: -100*C1.my_val(20) + C2.my_val(20)

Is there an existing easy way to do this in python? Or perhaps a lightweight library that allows one to create such evaluation rules. I have found for instance https://github.com/pydata/numexpr however this applies directly onto datatypes, whereas it should apply onto the object layer, apply custom logic, and then execute the standard math grammar order of operations.

Any direction for trying to do this is greatly appreciated! regards

15
  • 1
    It's not really clear what you're asking although perhaps what you're looking for is operator overloading. Commented Jun 4, 2017 at 1:45
  • Where is this "grammar" coming from, and why do you call it a grammar? It doesn't seem to have much to do with parsing or grammars. It sounds like you could just have the "grammar" be a function. Commented Jun 4, 2017 at 1:47
  • @user2357112 A grammar in the sense that one has a defined set of operations, each of which performs the same task on the same item type. Is this not what a grammar is - a set of rules that determine how certain things compose with one another. In this case that thing is a class, and the structured rule set is mathematical operations on top of another layer of (consistent) logic. A function is merely part of a grammar. Mathematical evaluation has its own grammar. I'm wondering is there an existing solution so I don't re-invent the wheel. Perhaps fdik.org/pyPEG Commented Jun 4, 2017 at 3:05
  • @pvg This is an option, and I may have to use it for now however would prefer something that is not necessarily limited to existing operators Commented Jun 4, 2017 at 3:07
  • 1
    Have you looked at ply? Commented Jun 4, 2017 at 5:44

1 Answer 1

1

Pyparsing makes it pretty easy to define infix notation parsers, and can wrap operators and operands in classes to do standard or customized behavior. See this question and its pyparsing-based solution: pyparsing nestedExpr and nested parentheses

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.