<s> """<STR_LIT>""" <EOL> __version__ = '<STR_LIT>' <EOL> VERSION = tuple ( map ( int , __version__ . split ( '<STR_LIT:.>' ) ) ) <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> import platform <EOL> _IS_PYTHON_3 = ( platform . version ( ) >= '<STR_LIT:3>' ) <EOL> identity = lambda x : x <EOL> if _IS_PYTHON_3 : <EOL> u = identity <EOL> else : <EOL> import codecs <EOL> def u ( string ) : <EOL> return codecs . unicode_escape_decode ( string ) [ <NUM_LIT:0> ] <EOL> if _IS_PYTHON_3 : <EOL> iteritems = dict . items <EOL> iterkeys = dict . keys <EOL> else : <EOL> iteritems = dict . iteritems <EOL> iterkeys = dict . iterkeys <EOL> class Bunch ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __contains__ ( self , k ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return dict . __contains__ ( self , k ) or hasattr ( self , k ) <EOL> except : <EOL> return False <EOL> def __getattr__ ( self , k ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return object . __getattribute__ ( self , k ) <EOL> except AttributeError : <EOL> try : <EOL> return self [ k ] <EOL> except KeyError : <EOL> raise AttributeError ( k ) <EOL> def __setattr__ ( self , k , v ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> object . __getattribute__ ( self , k ) <EOL> except AttributeError : <EOL> try : <EOL> self [ k ] = v <EOL> except : <EOL> raise AttributeError ( k ) <EOL> else : <EOL> object . __setattr__ ( self , k , v ) <EOL> def __delattr__ ( self , k ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> object . __getattribute__ ( self , k ) <EOL> except AttributeError : <EOL> try : <EOL> del self [ k ] <EOL> except KeyError : <EOL> raise AttributeError ( k ) <EOL> else : <EOL> object . __delattr__ ( self , k ) <EOL> def toDict ( self ) : <EOL> """<STR_LIT>""" <EOL> return unbunchify ( self ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> keys = list ( iterkeys ( self ) ) <EOL> keys . sort ( ) <EOL> args = '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % ( key , self [ key ] ) for key in keys ] ) <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , args ) <EOL> @ staticmethod <EOL> def fromDict ( d ) : <EOL> """<STR_LIT>""" <EOL> return bunchify ( d ) <EOL> def bunchify ( x ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( x , dict ) : <EOL> return Bunch ( ( k , bunchify ( v ) ) for k , v in iteritems ( x ) ) <EOL> elif isinstance ( x , ( list , tuple ) ) : <EOL> return type ( x ) ( bunchify ( v ) for v in x ) <EOL> else : <EOL> return x <EOL> def unbunchify ( x ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( x , dict ) : <EOL> return dict ( ( k , unbunchify ( v ) ) for k , v in iteritems ( x ) ) <EOL> elif isinstance ( x , ( list , tuple ) ) : <EOL> return type ( x ) ( unbunchify ( v ) for v in x ) <EOL> else : <EOL> return x <EOL> try : <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> import simplejson as json <EOL> def toJSON ( self , ** options ) : <EOL> """<STR_LIT>""" <EOL> return json . dumps ( self , ** options ) <EOL> Bunch . toJSON = toJSON <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> import yaml <EOL> from yaml . representer import Representer , SafeRepresenter <EOL> def from_yaml ( loader , node ) : <EOL> """<STR_LIT>""" <EOL> data = Bunch ( ) <EOL> yield data <EOL> value = loader . construct_mapping ( node ) <EOL> data . update ( value ) <EOL> def to_yaml_safe ( dumper , data ) : <EOL> """<STR_LIT>""" <EOL> return dumper . represent_dict ( data ) <EOL> def to_yaml ( dumper , data ) : <EOL> """<STR_LIT>""" <EOL> return dumper . represent_mapping ( u ( '<STR_LIT>' ) , data ) <EOL> yaml . add_constructor ( u ( '<STR_LIT>' ) , from_yaml ) <EOL> yaml . add_constructor ( u ( '<STR_LIT>' ) , from_yaml ) <EOL> SafeRepresenter . add_representer ( Bunch , to_yaml_safe ) <EOL> SafeRepresenter . add_multi_representer ( Bunch , to_yaml_safe ) <EOL> Representer . add_representer ( Bunch , to_yaml ) <EOL> Representer . add_multi_representer ( Bunch , to_yaml ) <EOL> def toYAML ( self , ** options ) : <EOL> """<STR_LIT>""" <EOL> opts = dict ( indent = <NUM_LIT:4> , default_flow_style = False ) <EOL> opts . update ( options ) <EOL> if '<STR_LIT>' not in opts : <EOL> return yaml . safe_dump ( self , ** opts ) <EOL> else : <EOL> return yaml . dump ( self , ** opts ) <EOL> def fromYAML ( * args , ** kwargs ) : <EOL> return bunchify ( yaml . load ( * args , ** kwargs ) ) <EOL> Bunch . toYAML = toYAML <EOL> Bunch . fromYAML = staticmethod ( fromYAML ) <EOL> except ImportError : <EOL> pass <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
