Package gavo :: Package grammars :: Module dictlistgrammar
[frames] | no frames]

Source Code for Module gavo.grammars.dictlistgrammar

 1  """ 
 2  A (quite trivial) grammar that iterates over lists of dicts. 
 3  """ 
 4   
 5  #c Copyright 2008-2019, the GAVO project 
 6  #c 
 7  #c This program is free software, covered by the GNU GPL.  See the 
 8  #c COPYING file in the source distribution. 
 9   
10   
11  from gavo import base 
12  from gavo.grammars.common import Grammar, RowIterator 
13   
14   
15 -class ListIterator(RowIterator):
16 - def __init__(self, *args, **kwargs):
17 RowIterator.__init__(self, *args, **kwargs) 18 self.recNo = 0 19 if self.grammar.asPars: 20 self.sourceRow = self.sourceToken[0]
21
22 - def _iterRows(self):
23 if self.grammar.asPars: 24 return 25 self.recNo = 1 26 for rec in self.sourceToken: 27 res = rec.copy() 28 yield res 29 self.recNo += 1
30
31 - def getLocator(self):
32 return "List, index=%d"%self.recNo
33 34
35 -class DictlistGrammar(Grammar):
36 """A grammar that "parses" from lists of dicts. 37 38 Actually, it will just return the dicts as they are passed. This is 39 mostly useful internally, though it might come in handy in custom code. 40 """ 41 name_ = "dictlistGrammar" 42 rowIterator = ListIterator 43 44 _asPars = base.BooleanAttribute("asPars", default=False, description= 45 "Just return the first item of the list as parameters row and exit?")
46