[Solved] Got stuck in python code which made me confused


How about something like this?

import re

class context:
    grammar =  r'and|or|#|\$|:|@|\w+'
    subs = [
        (r'\$(\w+)', "context('\\1')"),
        (r'!#(\w+)', "intents('\\1').isNot_()"),
        (r'#(\w+)', "intents('\\1').is_()"),
        (r'@(\w+):(\w+)', "entities('\\1').is_('\\2')"),
        (r'@(\w+)', "entities('\\1').any()")
    ]

    def __init__(self, val):
       self.val = val

    def parse(self):
        parsed = self.val
        for sub in self.subs:
            parsed = re.sub(*sub, parsed)
        return parsed
>>> print(context('$foo\n#foo\n!#foo\n@foo\n@foo:bar').parse())
context('foo')
intents('foo').is_()
intents('foo').isNot_()
entities('foo').any()
entities('foo').is_('bar')

1

solved Got stuck in python code which made me confused