<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from collections import namedtuple <EOL> import csv <EOL> import itertools <EOL> from itertools import product <EOL> from operator import attrgetter as aget , itemgetter as iget <EOL> import operator <EOL> import sys <EOL> from pandas . compat import map , u , callable , Counter <EOL> import pandas . compat as compat <EOL> ex1 = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:id>' : '<STR_LIT:hello>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : dict ( url = '<STR_LIT>' ) } , <EOL> ] <EOL> } <EOL> ex2 = { u ( '<STR_LIT>' ) : { u ( '<STR_LIT>' ) : [ { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT:value>' ) : <NUM_LIT:7> } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : False } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : False } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : False } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : False } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : u ( '<STR_LIT>' ) } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : <NUM_LIT> } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : True } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : False } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : <NUM_LIT:1> } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : True } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : False } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT:value>' ) : True } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : True } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT:value>' ) : True } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : u ( '<STR_LIT>' ) } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : False } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : <NUM_LIT:0> } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : True } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : u ( '<STR_LIT>' ) } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : True } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : False } , <EOL> { u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT:value>' ) : True } ] , <EOL> u ( '<STR_LIT>' ) : [ { u ( '<STR_LIT:id>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT>' ) : True } , <EOL> { u ( '<STR_LIT:id>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT>' ) : False } , <EOL> { u ( '<STR_LIT:id>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT>' ) : True } , <EOL> { u ( '<STR_LIT:id>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT>' ) : True } , <EOL> { u ( '<STR_LIT:id>' ) : u ( '<STR_LIT>' ) , u ( '<STR_LIT>' ) : True } ] , <EOL> u ( '<STR_LIT>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT:location>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT>' ) : u ( '<STR_LIT>' ) } , <EOL> u ( '<STR_LIT>' ) : { <EOL> u ( '<STR_LIT>' ) : [ { u ( '<STR_LIT>' ) : False , <EOL> u ( '<STR_LIT:id>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) } , <EOL> { u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT:id>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT>' ) : False , <EOL> u ( '<STR_LIT>' ) : False , <EOL> u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) } , <EOL> { u ( '<STR_LIT>' ) : False , <EOL> u ( '<STR_LIT:id>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) } , <EOL> { u ( '<STR_LIT>' ) : False , <EOL> u ( '<STR_LIT:id>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) } , <EOL> { u ( '<STR_LIT>' ) : False , <EOL> u ( '<STR_LIT:id>' ) : u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT>' ) : True , <EOL> u ( '<STR_LIT:name>' ) : u ( '<STR_LIT>' ) } ] , <EOL> u ( '<STR_LIT>' ) : <NUM_LIT:1> } } <EOL> def denorm ( queries , iterable_of_things , default = None ) : <EOL> """<STR_LIT>""" <EOL> def _denorm ( queries , thing ) : <EOL> fields = [ ] <EOL> results = [ ] <EOL> for q in queries : <EOL> r = Ql ( q , thing ) <EOL> if not r : <EOL> r = [ default ] <EOL> if isinstance ( r [ <NUM_LIT:0> ] , type ( { } ) ) : <EOL> fields . append ( sorted ( r [ <NUM_LIT:0> ] . keys ( ) ) ) <EOL> else : <EOL> fields . append ( [ q ] ) <EOL> results . append ( r ) <EOL> flist = list ( flatten ( * map ( iter , fields ) ) ) <EOL> prod = itertools . product ( * results ) <EOL> for p in prod : <EOL> U = dict ( ) <EOL> for ( ii , thing ) in enumerate ( p ) : <EOL> if isinstance ( thing , type ( { } ) ) : <EOL> U . update ( thing ) <EOL> else : <EOL> U [ fields [ ii ] [ <NUM_LIT:0> ] ] = thing <EOL> yield U <EOL> return list ( flatten ( * [ _denorm ( queries , thing ) for thing in iterable_of_things ] ) ) <EOL> def default_iget ( fields , default = None , ) : <EOL> """<STR_LIT>""" <EOL> myiget = operator . itemgetter ( * fields ) <EOL> L = len ( fields ) <EOL> def f ( thing ) : <EOL> try : <EOL> ans = list ( myiget ( thing ) ) <EOL> if L < <NUM_LIT:2> : <EOL> ans = [ ans , ] <EOL> return ans <EOL> except KeyError : <EOL> return [ thing . get ( x , default ) for x in fields ] <EOL> f . __doc__ = "<STR_LIT>" % ( default , fields ) <EOL> f . __name__ = "<STR_LIT>" <EOL> return f <EOL> def flatten ( * stack ) : <EOL> """<STR_LIT>""" <EOL> stack = list ( stack ) <EOL> while stack : <EOL> try : x = next ( stack [ <NUM_LIT:0> ] ) <EOL> except StopIteration : <EOL> stack . pop ( <NUM_LIT:0> ) <EOL> continue <EOL> if hasattr ( x , '<STR_LIT>' ) and callable ( getattr ( x , '<STR_LIT>' ) ) : <EOL> stack . insert ( <NUM_LIT:0> , x ) <EOL> else : yield x <EOL> def _Q ( filter_ , thing ) : <EOL> """<STR_LIT>""" <EOL> T = type ( thing ) <EOL> if isinstance ( { } , T ) : <EOL> for k , v in compat . iteritems ( thing ) : <EOL> if filter_ == k : <EOL> if isinstance ( v , type ( [ ] ) ) : <EOL> yield iter ( v ) <EOL> else : <EOL> yield v <EOL> if type ( v ) in ( type ( { } ) , type ( [ ] ) ) : <EOL> yield Q ( filter_ , v ) <EOL> elif isinstance ( [ ] , T ) : <EOL> for k in thing : <EOL> yield Q ( filter_ , k ) <EOL> else : <EOL> pass <EOL> def Q ( filter_ , thing ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( filter_ , type ( [ ] ) ) : <EOL> return flatten ( * [ _Q ( x , thing ) for x in filter_ ] ) <EOL> elif isinstance ( filter_ , type ( { } ) ) : <EOL> d = dict . fromkeys ( list ( filter_ . keys ( ) ) ) <EOL> for k in d : <EOL> d [ k ] = Q ( k , thing ) <EOL> return d <EOL> else : <EOL> if "<STR_LIT:U+0020>" in filter_ : <EOL> parts = filter_ . strip ( ) . split ( ) <EOL> r = None <EOL> for p in parts : <EOL> r = Ql ( p , thing ) <EOL> thing = r <EOL> return r <EOL> else : <EOL> return flatten ( _Q ( filter_ , thing ) ) <EOL> def Ql ( filter_ , thing ) : <EOL> """<STR_LIT>""" <EOL> res = Q ( filter_ , thing ) <EOL> if isinstance ( filter_ , type ( { } ) ) : <EOL> for k in res : <EOL> res [ k ] = list ( res [ k ] ) <EOL> return res <EOL> else : <EOL> return list ( res ) <EOL> def countit ( fields , iter_of_iter , default = None ) : <EOL> """<STR_LIT>""" <EOL> C = Counter ( ) <EOL> T = namedtuple ( "<STR_LIT>" , fields ) <EOL> get = default_iget ( * fields , default = default ) <EOL> return Counter ( <EOL> ( T ( * get ( thing ) ) for thing in iter_of_iter ) <EOL> ) <EOL> def printout ( queries , things , default = None , f = sys . stdout , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> results = denorm ( queries , things , default = None ) <EOL> fields = set ( itertools . chain ( * ( x . keys ( ) for x in results ) ) ) <EOL> W = csv . DictWriter ( f = f , fieldnames = fields , ** kwargs ) <EOL> W . writeheader ( ) <EOL> for r in results : <EOL> W . writerow ( r ) <EOL> def test_run ( ) : <EOL> print ( "<STR_LIT>" ) <EOL> print ( list ( Q ( '<STR_LIT:url>' , ex1 ) ) ) <EOL> assert list ( Q ( '<STR_LIT:url>' , ex1 ) ) == [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert Ql ( '<STR_LIT:url>' , ex1 ) == [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> print ( "<STR_LIT>" ) <EOL> print ( list ( Q ( [ '<STR_LIT:name>' , '<STR_LIT:id>' ] , ex1 ) ) ) <EOL> assert Ql ( [ '<STR_LIT:name>' , '<STR_LIT:id>' ] , ex1 ) == [ '<STR_LIT>' , '<STR_LIT:hello>' , '<STR_LIT>' ] <EOL> print ( "<STR_LIT>" ) <EOL> print ( Ql ( '<STR_LIT>' , ex1 ) ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( list ( Q ( '<STR_LIT>' , ex1 ) ) ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( Ql ( '<STR_LIT>' , ex1 ) ) <EOL> print ( "<STR_LIT>" ) <EOL> printout ( [ '<STR_LIT:name>' , '<STR_LIT>' ] , [ ex1 , ] , extrasaction = '<STR_LIT:ignore>' ) <EOL> print ( "<STR_LIT>" ) <EOL> from pprint import pprint as pp <EOL> print ( "<STR_LIT>" ) <EOL> pp ( denorm ( [ '<STR_LIT:location>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , [ ex2 , ] , default = "<STR_LIT>" ) [ : <NUM_LIT:2> ] ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> pass </s>
