YAQL Tutorial

Python installation

Python Programming YAQL Queries

How to integrate YAQL query in python program?

  • Steps to integrate YAQL in python code.
    • Create a YAQL enginge
    • Parse a YAQL expression
    • Execute the parsed expression.
  • Engine must be created once and can be reused for all YAQL queries.
  • Python code using YAQL queries to filter and format the JSON data.

    Eample Python Program to integrate YAQL query:

    import yaql                                                                     
    import json                                                                     
                                                                                    
    data_source = json.load(open('input.json', 'r'))                                
                                                                                    
    engine = yaql.factory.YaqlFactory().create()                                    
                                                                                    
    expression = engine('$.cars.groupBy($.transmission_type).toDict($[0],list($[1].name))')
                                                                                    
    data = expression.evaluate(data=data_source)                                    
                                                                                    
    print data 
                    
    JSON data (input.json):
    {                                                                               
     "cars": [{                                                                     
        "name": "Toyota",                                                           
        "cc": 1600,                                                               
        "transmission_type": "manual"                                               
       },                                                                           
       {                                                                            
        "name": "Volkswagen",                                                       
        "cc": 1800,                                                               
        "transmission_type": "automatic"                                            
       },                                                                           
       {                                                                            
        "name": "Audi",                                                             
        "cc": 2600,                                                               
        "transmission_type": "automatic"                                            
       }                                                                            
     ]                                                                              
    }    
    
    Output:
    {u'automatic': [u'Volkswagen', u'Audi'], u'manual': [u'Toyota']}
    
  • Python code using YAQL queries to filter and format the YAML data:
    import yaql                                                                     
    import yaml                                                                     
                                                                                    
    data_source = yaml.load(open('input.yaml', 'r'))                                
                                                                                    
    engine = yaql.factory.YaqlFactory().create()                                    
                                                                                    
    expression = engine('$.cars.where($.transmission_type = "automatic").orderByDescending($.cc)')
    data = expression.evaluate(data=data_source)                                    
                                                                                    
    print data   
    
    YAML data (input.yaml):
    cars:                                                                           
      - name: Toyota                                                                
        cc: 1600                                                                    
        transmission_type: manual                                                   
      - name: Volkswagen                                                            
        cc: 1800                                                                    
        transmission_type: automatic                                                
      - name: Audi                                                                  
        cc: 2600                                                                    
        transmission_type: automatic   
    
    Output:
    [{u'cc': 2600, u'transmission_type': u'automatic', u'name': u'Audi'}, {u'cc': 1800, u'transmission_type': u'automatic', u'name': u'Volkswagen'}]
    
  • How exception will be displayed if embedded YAQL query is error?

    If YAQL query embedded in python is having issue, may get exception like below. We need to correct the YAQL query properly in python code.
    $ python yaml-yaql.py 
    Traceback (most recent call last):
      File "yaml-yaql.py", line 9, in 
        order = expression.evaluate(data=data_source)
      File "/usr/lib/python2.7/site-packages/yaql/language/expressions.py", line 165, in evaluate
        return self(utils.NO_VALUE, context, self.engine)
      File "/usr/lib/python2.7/site-packages/yaql/language/expressions.py", line 156, in __call__
        return super(Statement, self).__call__(receiver, context, engine)
      File "/usr/lib/python2.7/site-packages/yaql/language/expressions.py", line 37, in __call__
        return context(self.name, engine, receiver, context)(*self.args)
      File "/usr/lib/python2.7/site-packages/yaql/language/contexts.py", line 65, in 
        data_context, use_convention, function_filter)
      File "/usr/lib/python2.7/site-packages/yaql/language/runner.py", line 49, in call
        name, all_overloads, engine, receiver, data_context, args, kwargs)
      File "/usr/lib/python2.7/site-packages/yaql/language/runner.py", line 117, in choose_overload
        args = tuple(arg_evaluator(i, arg) for i, arg in enumerate(args))
      File "/usr/lib/python2.7/site-packages/yaql/language/runner.py", line 117, in 
        args = tuple(arg_evaluator(i, arg) for i, arg in enumerate(args))
      File "/usr/lib/python2.7/site-packages/yaql/language/runner.py", line 113, in 
        and not isinstance(arg, expressions.Constant))
      File "/usr/lib/python2.7/site-packages/yaql/language/expressions.py", line 37, in __call__
        return context(self.name, engine, receiver, context)(*self.args)
      File "/usr/lib/python2.7/site-packages/yaql/language/contexts.py", line 65, in 
        data_context, use_convention, function_filter)
      File "/usr/lib/python2.7/site-packages/yaql/language/runner.py", line 51, in call
        result = delegate()
      File "/usr/lib/python2.7/site-packages/yaql/language/runner.py", line 142, in 
        return lambda: delegate()
      File "/usr/lib/python2.7/site-packages/yaql/language/specs.py", line 346, in func
        six.iteritems(keyword_args)))
      File "/usr/lib/python2.7/site-packages/yaql/standard_library/system.py", line 63, in op_dot
        return expr(receiver)
      File "/usr/lib/python2.7/site-packages/yaql/language/yaqltypes.py", line 293, in func
        engine, args, kwargs)
      File "/usr/lib/python2.7/site-packages/yaql/language/yaqltypes.py", line 260, in _call
        result = value(receiver, context, engine)
      File "/usr/lib/python2.7/site-packages/yaql/language/expressions.py", line 37, in __call__
        return context(self.name, engine, receiver, context)(*self.args)
      File "/usr/lib/python2.7/site-packages/yaql/language/contexts.py", line 65, in 
        data_context, use_convention, function_filter)
      File "/usr/lib/python2.7/site-packages/yaql/language/runner.py", line 46, in call
        raise exceptions.NoMethodRegisteredException(name, receiver)
    yaql.language.exceptions.NoMethodRegisteredException: Unknown method "orderDescendingBy" for receiver 
    

Privacy Policy  |  Copyright@2017 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Free online programming tutorials

Email Facebook Google LinkedIn Twitter
^