<s> import numpy as np <EOL> from chaco . api import AbstractPlotData <EOL> from traits . api import Any , Dict , Tuple <EOL> import collections <EOL> import time <EOL> class PandasPlotData ( AbstractPlotData ) : <EOL> '''<STR_LIT>''' <EOL> df = Any ( ) <EOL> _colmap = Dict ( ) <EOL> _extras = Dict ( ) <EOL> _reserved = Tuple ( '<STR_LIT:index>' , '<STR_LIT>' ) <EOL> selections = Dict ( ) <EOL> def __init__ ( self , df , extras = None , ** kwtraits ) : <EOL> """<STR_LIT>""" <EOL> super ( PandasPlotData , self ) . __init__ ( ** kwtraits ) <EOL> if len ( df . shape ) > <NUM_LIT:2> : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> self . set_df ( df ) <EOL> def __getitem__ ( self , name ) : <EOL> return self . df [ name ] <EOL> def __setitem__ ( self , name , value ) : <EOL> raise NotImplemented <EOL> def __delitem__ ( self , name ) : <EOL> raise NotImplemented <EOL> def list_data ( self , as_strings = False ) : <EOL> """<STR_LIT>""" <EOL> if self . df is None : <EOL> return [ ] <EOL> if as_strings : <EOL> return [ str ( col ) for col in self . df . columns ] <EOL> else : <EOL> return list ( self . df . columns . values ) <EOL> def get_data ( self , name ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( name , basestring ) : <EOL> return self . df [ name ] . values <EOL> else : <EOL> if name == '<STR_LIT:index>' : <EOL> return self . df . index . values <EOL> elif name == '<STR_LIT>' : <EOL> return self . df . columns . values <EOL> try : <EOL> return self . df [ self . _colmap [ name ] ] . values <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> return self . _extras [ name ] <EOL> except KeyError : <EOL> raise AttributeError ( '<STR_LIT>' % name ) <EOL> def del_data ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def set_data ( self , name , new_data ) : <EOL> """<STR_LIT>""" <EOL> if not self . writable : <EOL> return None <EOL> try : <EOL> new_data = np . array ( new_data ) <EOL> except Exception : <EOL> raise Exception ( '<STR_LIT>' % type ( new_data ) ) <EOL> if not isinstance ( name , basestring ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if name in self . _stringmap : <EOL> raise AttributeError ( '<STR_LIT>' % name ) <EOL> if name in self . _reserved : <EOL> raise NameError ( '<STR_LIT>' % ( name , '<STR_LIT:U+002C>' . join ( self . _reserved ) ) ) <EOL> if name in self . _extras : <EOL> self . data_changed = { '<STR_LIT>' : [ name ] } <EOL> else : <EOL> self . data_changed = { '<STR_LIT>' : [ name ] } <EOL> self . _extras [ name ] = new_data <EOL> return name <EOL> def set_df ( self , dfnew ) : <EOL> '''<STR_LIT>''' <EOL> event = self . _set_df ( dfnew ) <EOL> event2 = self . _reset_extras ( ) <EOL> if event2 : <EOL> event . update ( event2 ) <EOL> self . data_changed = event <EOL> if event . has_key ( '<STR_LIT>' ) : <EOL> return True <EOL> return False <EOL> def _set_df ( self , dfnew ) : <EOL> '''<STR_LIT>''' <EOL> event = { } <EOL> if isinstance ( self . df , type ( None ) ) : <EOL> pass <EOL> else : <EOL> if np . array_equal ( dfnew . columns . values , self . df . columns . values ) : <EOL> self . df = dfnew <EOL> event [ '<STR_LIT>' ] = self . _colmap . keys ( ) <EOL> return event <EOL> else : <EOL> event [ '<STR_LIT>' ] = self . _colmap . keys ( ) <EOL> event . update ( self . _add_df ( dfnew ) ) <EOL> return event <EOL> def _reset_extras ( self ) : <EOL> if self . _extras : <EOL> return { '<STR_LIT>' : self . _extras . keys ( ) } <EOL> def _add_df ( self , dfnew ) : <EOL> '''<STR_LIT>''' <EOL> self . _colmap = dict ( ( str ( c ) , c ) for c in dfnew . columns . values ) <EOL> self . df = dfnew <EOL> return { '<STR_LIT>' : self . _colmap . keys ( ) } <EOL> def _get_indicies ( self , * names ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplemented <EOL> def get_selection ( self , name ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT>' <EOL> return self . selections . get ( name , None ) <EOL> def set_selection ( self , name , selection ) : <EOL> self . selections [ name ] = selection <EOL> self . data_changed = True </s>
