<s> from __future__ import division <EOL> from vistrails . core . modules . basic_modules import ListType <EOL> from vistrails . core . modules . vistrails_module import ModuleError <EOL> from . . common import Table , TableObject , InternalModuleError <EOL> class BaseConverter ( Table ) : <EOL> _output_ports = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:value>' , Table ) ] <EOL> def convert_to_table ( self , obj ) : <EOL> try : <EOL> table = self . make_table ( obj ) <EOL> except InternalModuleError , e : <EOL> e . raise_module_error ( self ) <EOL> self . set_output ( '<STR_LIT>' , table . columns ) <EOL> if table . names is not None : <EOL> self . set_output ( '<STR_LIT>' , table . names ) <EOL> self . set_output ( '<STR_LIT:value>' , table ) <EOL> @ staticmethod <EOL> def add_list ( columns , key , value ) : <EOL> if not isinstance ( value , ListType ) : <EOL> raise InternalModuleError ( "<STR_LIT>" % key ) <EOL> if key is not None : <EOL> value = [ key ] + value <EOL> for i , v in enumerate ( value ) : <EOL> columns [ i ] . append ( v ) <EOL> @ staticmethod <EOL> def add_dict ( columns , keys , key_set , key , value ) : <EOL> if not isinstance ( value , dict ) : <EOL> raise InternalModuleError ( <EOL> "<STR_LIT>" % key ) <EOL> value_keys = set ( value . keys ( ) ) <EOL> m = key_set - value_keys <EOL> if m : <EOL> raise InternalModuleError ( <EOL> "<STR_LIT>" % ( <EOL> key , <EOL> next ( iter ( m ) ) ) ) <EOL> m = value_keys - key_set <EOL> if m : <EOL> raise InternalModuleError ( <EOL> "<STR_LIT>" % ( <EOL> key , <EOL> next ( iter ( m ) ) ) ) <EOL> if key is None : <EOL> for i , k in enumerate ( keys ) : <EOL> columns [ i ] . append ( value [ k ] ) <EOL> else : <EOL> columns [ <NUM_LIT:0> ] . append ( key ) <EOL> for i , k in enumerate ( keys ) : <EOL> columns [ i + <NUM_LIT:1> ] . append ( value [ k ] ) <EOL> class BaseDictToTable ( BaseConverter ) : <EOL> _input_ports = [ ( '<STR_LIT>' , '<STR_LIT>' , <EOL> { '<STR_LIT>' : True , '<STR_LIT>' : repr ( [ "<STR_LIT:key>" ] ) } ) ] <EOL> def make_table ( self , obj ) : <EOL> if not isinstance ( obj , dict ) : <EOL> raise ModuleError ( self , "<STR_LIT>" ) <EOL> key_name = self . get_input ( '<STR_LIT>' , True ) <EOL> iterator = obj . iteritems ( ) <EOL> try : <EOL> first_key , first_value = next ( iterator ) <EOL> except StopIteration : <EOL> raise ModuleError ( self , "<STR_LIT>" ) <EOL> count = <NUM_LIT:1> <EOL> if isinstance ( first_value , ListType ) : <EOL> keys = None <EOL> columns = [ [ ] for i in xrange ( <NUM_LIT:1> + len ( first_value ) ) ] <EOL> self . add_list ( columns , first_key , first_value ) <EOL> for key , value in iterator : <EOL> self . add_list ( columns , key , value ) <EOL> count += <NUM_LIT:1> <EOL> elif isinstance ( first_value , dict ) : <EOL> keys = first_value . keys ( ) <EOL> key_set = set ( keys ) <EOL> columns = [ [ ] for i in xrange ( <NUM_LIT:1> + len ( keys ) ) ] <EOL> self . add_dict ( columns , keys , key_set , first_key , first_value ) <EOL> for key , value in iterator : <EOL> self . add_dict ( columns , keys , key_set , key , value ) <EOL> count += <NUM_LIT:1> <EOL> else : <EOL> raise ModuleError ( self , "<STR_LIT>" ) <EOL> if keys is not None : <EOL> names = [ key_name ] + keys <EOL> else : <EOL> names = None <EOL> return TableObject ( columns , count , names ) <EOL> class BaseListToTable ( BaseConverter ) : <EOL> def make_table ( self , obj ) : <EOL> if not isinstance ( obj , ListType ) : <EOL> raise ModuleError ( self , "<STR_LIT>" ) <EOL> iterator = iter ( obj ) <EOL> try : <EOL> first = next ( iterator ) <EOL> except StopIteration : <EOL> raise ModuleError ( self , "<STR_LIT>" ) <EOL> count = <NUM_LIT:1> <EOL> if isinstance ( first , ListType ) : <EOL> keys = None <EOL> columns = [ [ ] for i in xrange ( len ( first ) ) ] <EOL> self . add_list ( columns , None , first ) <EOL> for value in iterator : <EOL> self . add_list ( columns , None , value ) <EOL> count += <NUM_LIT:1> <EOL> elif isinstance ( first , dict ) : <EOL> keys = first . keys ( ) <EOL> key_set = set ( keys ) <EOL> columns = [ [ ] for i in xrange ( len ( keys ) ) ] <EOL> self . add_dict ( columns , keys , key_set , None , first ) <EOL> for value in iterator : <EOL> self . add_dict ( columns , keys , key_set , None , value ) <EOL> count += <NUM_LIT:1> <EOL> else : <EOL> raise ModuleError ( self , "<STR_LIT>" ) <EOL> return TableObject ( columns , count , keys ) <EOL> class DictToTable ( BaseDictToTable ) : <EOL> """<STR_LIT>""" <EOL> _input_ports = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> def compute ( self ) : <EOL> self . convert_to_table ( self . get_input ( '<STR_LIT>' ) ) <EOL> class ListToTable ( BaseListToTable ) : <EOL> """<STR_LIT>""" <EOL> _input_ports = [ ( '<STR_LIT:list>' , '<STR_LIT>' ) ] <EOL> def compute ( self ) : <EOL> self . convert_to_table ( self . get_input ( '<STR_LIT:list>' ) ) <EOL> _modules = [ ( BaseConverter , { '<STR_LIT>' : True } ) , <EOL> ( BaseDictToTable , { '<STR_LIT>' : True } ) , <EOL> ( BaseListToTable , { '<STR_LIT>' : True } ) , <EOL> DictToTable , ListToTable ] </s>
