Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8948439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:59:22+00:00 2026-06-15T12:59:22+00:00

I am working on a simple DSL to transform data extracted from MongoDB. I

  • 0

I am working on a simple DSL to transform data extracted from MongoDB. I am using python and pyparsing and have gotten reasonably far in creating a grammar that works for basic operators like +/-*, starting from the examples provided. I am currently stuck on how to get my program to evaluate functions of the form Rank[dbRef]. I can evaluate and do arithmetic on dbRefs through the simple operators, but something is not working with my recursion in evaluating functions. I cannot figure out how to access the dbRef argument that was passed in the function call.

Here is the grammar and associated setParseActions:

# Define parser, accounting for the fact that some fields contain whitespace
chars = Word(alphanums + "_-/")
expr = Forward()
integer = Word(nums).setParseAction(EvalConstant)
real = Combine(Word(nums) + "." + Word(nums)).setParseAction(EvalConstant)

# Handle database field references that are coming out of Mongo
dbRef = Combine(chars + OneOrMore(":") + chars)

dbRef.setParseAction(EvalDBref)

# Handle function calls
functionCall = (Keyword("Rank") | Keyword("ZS") | Keyword("Ntile")) + "[" + dbRef + "]"
functionCall.setParseAction(EvalFunction)
operand = (real | integer) | functionCall | dbRef 

signop = oneOf('+ -')
multop = oneOf('* /')
plusop = oneOf('+ -')

# Use parse actions to attach Eval constructors to sub-expressions

expr << operatorPrecedence(operand,
    [
     (signop, 1, opAssoc.RIGHT, EvalSignOp),
     (multop, 2, opAssoc.LEFT, EvalMultOp),
     (plusop, 2, opAssoc.LEFT, EvalAddOp),
    ])

formulas = ['Rank[Person:Height]']

for f in formulas:
    ret = expr.parseString(f)[0]
    print p + ": " + line + " --> " + str(ret.eval())

Here is the relevant code for my evaluation class. The class DOES get called by the parser, but how do I access the argument that is passed to the function?

# Executes functions contained in expressions
class EvalFunction(object): 
    def __init__(self, tokens): 
        self.value = tokens[0]
    def eval(self):
        func = self.value
        if func == 'Rank':
            # How to evaluate the token that is arg of Function?
            return 'Rank Found';

I think I just need a nudge in the right direction to get to the next stage ..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:59:23+00:00Added an answer on June 15, 2026 at 12:59 pm

    I sorted this out and wanted to provide an answer. My function evaluation class looks like this:

    # Executes functions contained in expressions
    class EvalFunction(object):
      pop_ = {}
      def __init__(self, tokens):
        self.func_ = tokens.funcname
        self.field_ = tokens.arg
      def eval(self):
          # Get the name of the requested field and source db
          # Functions can only be called on dbRef so this always done
          v = self.field_.value
          fieldRef = v.split(':')
          source = fieldRef[0]
          field = fieldRef[1]
    
          # Evaluate the dbRef (get the value from the db)
          val = self.field_.eval()
    
          if self.func_ == 'Avg':
            rec = db['Stats'].find_one({'_id' : field})   
            return rec['value']['avg']
          elif self.func_ == 'Root':
              return math.sqrt(val)
    

    my function grammar is:

    functionCall = funcNames("funcname") + "[" + dbRef("arg") + "]"
    functionCall.setParseAction(EvalFunction)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple working PHP script to write an HTML table from a
I'm attempting to create a simple DSL to handle reasonably complex dice-rolling instructions using
Good Day, I have a simple working routine in Perl that swaps two words:
I am working on simple login form. I have two text fields as user
Here is a simple working query without bind variables: select * from table1 where
I am working in simple application in which I need to post data to
I have this simple working curl command: curl -k -d X-User=user -d X-Password=password https://12.12.12.21
I have a simple 'Working' form that runs on its own thread to keep
I have been working with simple linq2sql statements for a bit but am new
I'm working with Simple.data, and the answer is not in the technology aforementioned but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.