<s> """<STR_LIT>""" <EOL> def valid_choices ( choices ) : <EOL> """<STR_LIT>""" <EOL> for key , value in choices : <EOL> if isinstance ( value , ( list , tuple ) ) : <EOL> for key , _ in value : <EOL> yield key <EOL> else : <EOL> yield key <EOL> def split_model_kwargs ( kw ) : <EOL> """<STR_LIT>""" <EOL> from collections import defaultdict <EOL> model_fields = { } <EOL> fields_agrs = defaultdict ( lambda : { } ) <EOL> for key in kw . keys ( ) : <EOL> if '<STR_LIT>' in key : <EOL> field , _ , subfield = key . partition ( '<STR_LIT>' ) <EOL> fields_agrs [ field ] [ subfield ] = kw [ key ] <EOL> else : <EOL> model_fields [ key ] = kw [ key ] <EOL> return model_fields , fields_agrs <EOL> class ExtensionMethod ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , by_instance = False ) : <EOL> self . registry = { } <EOL> self . by_instance = by_instance <EOL> self . default = None <EOL> def register ( self , field_type , impl = None ) : <EOL> """<STR_LIT>""" <EOL> def _wrapper ( func ) : <EOL> self . registry [ field_type ] = func <EOL> return func <EOL> if impl : <EOL> return _wrapper ( impl ) <EOL> return _wrapper <EOL> def register_default ( self , func ) : <EOL> self . default = func <EOL> return func <EOL> def decorator ( self , impl ) : <EOL> """<STR_LIT>""" <EOL> self . _create_value = impl ( self . _create_value ) <EOL> return impl <EOL> def _create_value ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not len ( args ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if self . by_instance : <EOL> field_type = args [ <NUM_LIT:0> ] <EOL> else : <EOL> field_type = args [ <NUM_LIT:0> ] . __class__ <EOL> function = self . registry . get ( field_type , self . default ) <EOL> if function is None : <EOL> raise TypeError ( "<STR_LIT>" % field_type ) <EOL> return function ( * args , ** kwargs ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> return self . _create_value ( * args , ** kwargs ) </s>
