#NEWLINE# def flatten_context( context , remove_lazy = True ) : #NEWLINE# #INDENT# '\n    Creates a dictionary from a Context instance by traversing\n    its dicts list. Can remove unwanted subjects from the result,\n    e.g. lazy objects.\n    ' #NEWLINE# flat_context = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# def _flatten( context ) : #NEWLINE# #INDENT# if isinstance ( context , dict ) : #NEWLINE# #INDENT# for ( k , v ) in context . items ( ) : #NEWLINE# #INDENT# if isinstance ( context , BaseContext ) : #NEWLINE# #INDENT# _flatten ( context ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# flat_context [ k ] = v #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( context , BaseContext ) : #NEWLINE# #INDENT# for context_dict in context . dicts : #NEWLINE# #INDENT# _flatten ( context_dict ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# _flatten ( context ) #NEWLINE# if remove_lazy : #NEWLINE# #INDENT# only_allowed = ( lambda dic : ( not isinstance ( dic [ 1 ] , forbidden_classes ) ) ) #NEWLINE# return dict ( filter ( only_allowed , flat_context . iteritems ( ) ) ) #NEWLINE# #UNINDENT# return flat_context