<s> """<STR_LIT>""" <EOL> from datetime import datetime <EOL> from IPython . core import ipapi <EOL> ip = ipapi . get ( ) <EOL> import pickleshare <EOL> import inspect , pickle , os , sys , textwrap <EOL> from IPython . core . fakemodule import FakeModule <EOL> from IPython . utils . ipstruct import Struct <EOL> from IPython . utils . warn import error <EOL> def refresh_variables ( ip , key = None ) : <EOL> db = ip . db <EOL> if key is None : <EOL> keys = db . keys ( '<STR_LIT>' ) <EOL> else : <EOL> keys = db . keys ( '<STR_LIT>' + key ) <EOL> for key in keys : <EOL> justkey = os . path . basename ( key ) <EOL> print ( "<STR_LIT>" , justkey , "<STR_LIT>" ) <EOL> try : <EOL> obj = db [ key ] <EOL> except KeyError : <EOL> print ( "<STR_LIT>" % justkey ) <EOL> print ( "<STR_LIT>" , sys . exc_info ( ) [ <NUM_LIT:0> ] ) <EOL> else : <EOL> try : <EOL> origname = obj . name <EOL> except : <EOL> ip . user_ns [ justkey ] = obj <EOL> print ( "<STR_LIT>" , justkey ) <EOL> else : <EOL> ip . user_ns [ origname ] = obj [ '<STR_LIT>' ] <EOL> print ( "<STR_LIT>" , origname ) <EOL> def read_variables ( ip , key = None ) : <EOL> db = ip . db <EOL> if key is None : <EOL> return None <EOL> else : <EOL> keys = db . keys ( '<STR_LIT>' + key ) <EOL> for key in keys : <EOL> justkey = os . path . basename ( key ) <EOL> print ( "<STR_LIT>" , justkey ) <EOL> try : <EOL> obj = db [ key ] <EOL> except KeyError : <EOL> print ( "<STR_LIT>" % justkey ) <EOL> print ( "<STR_LIT>" , sys . exc_info ( ) [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return obj <EOL> def detail_variables ( ip , key = None ) : <EOL> db , get = ip . db , ip . db . get <EOL> if key is None : <EOL> keys = db . keys ( '<STR_LIT>' ) <EOL> else : <EOL> keys = db . keys ( '<STR_LIT>' + key ) <EOL> if keys : <EOL> size = max ( list ( map ( len , keys ) ) ) <EOL> else : <EOL> size = <NUM_LIT:0> <EOL> fmthead = '<STR_LIT>' + str ( size ) + '<STR_LIT>' <EOL> fmtbody = '<STR_LIT>' <EOL> fmtdata = '<STR_LIT>' <EOL> for key in keys : <EOL> v = get ( key , '<STR_LIT>' ) <EOL> justkey = os . path . basename ( key ) <EOL> try : <EOL> print ( fmthead % ( justkey , datetime . ctime ( v . get ( '<STR_LIT:time>' , '<STR_LIT>' ) ) ) ) <EOL> print ( fmtbody % ( v . get ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> d = v . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> print ( fmtdata % ( repr ( type ( d ) ) , '<STR_LIT>' ) ) <EOL> print ( repr ( d ) [ <NUM_LIT:0> : <NUM_LIT:200> ] ) <EOL> print ( ) <EOL> print ( ) <EOL> except AttributeError : <EOL> print ( fmt % ( justkey , '<STR_LIT>' , '<STR_LIT>' , repr ( v ) [ : <NUM_LIT:50> ] ) ) <EOL> def intm ( n ) : <EOL> try : <EOL> return int ( n ) <EOL> except : <EOL> return <NUM_LIT:0> <EOL> def jot_obj ( self , obj , name , comment = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> had = self . db . keys ( '<STR_LIT>' + name + '<STR_LIT:*>' ) <EOL> suffix = '<STR_LIT>' <EOL> if len ( had ) > <NUM_LIT:0> : <EOL> pre = os . path . commonprefix ( had ) <EOL> suf = [ n . split ( pre ) [ <NUM_LIT:1> ] for n in had ] <EOL> versions = list ( map ( intm , suf ) ) <EOL> suffix = str ( max ( versions ) + <NUM_LIT:1> ) <EOL> uname = '<STR_LIT>' + name + suffix <EOL> all = ip . shell . history_manager . input_hist_parsed <EOL> try : <EOL> comment = ip . magic_edit ( '<STR_LIT>' ) . strip ( ) <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> comment = '<STR_LIT>' <EOL> self . db [ uname ] = Struct ( { '<STR_LIT>' : obj , <EOL> '<STR_LIT:time>' : datetime . now ( ) , <EOL> '<STR_LIT>' : all , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : comment , } ) <EOL> print ( "<STR_LIT>" % ( uname , obj . __class__ . __name__ ) ) <EOL> def magic_jot ( self , parameter_s = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> opts , argsl = self . parse_options ( parameter_s , '<STR_LIT>' , mode = '<STR_LIT:string>' ) <EOL> args = argsl . split ( None , <NUM_LIT:1> ) <EOL> ip = self . getapi ( ) <EOL> db = ip . db <EOL> if '<STR_LIT:d>' in opts : <EOL> try : <EOL> todel = args [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> error ( '<STR_LIT>' ) <EOL> else : <EOL> try : <EOL> del db [ '<STR_LIT>' + todel ] <EOL> except : <EOL> error ( "<STR_LIT>" % todel ) <EOL> elif '<STR_LIT:z>' in opts : <EOL> print ( "<STR_LIT>" ) <EOL> elif '<STR_LIT:r>' in opts : <EOL> try : <EOL> toret = args [ <NUM_LIT:0> ] <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> refresh_variables ( ip ) <EOL> else : <EOL> refresh_variables ( ip , toret ) <EOL> elif '<STR_LIT:l>' in opts : <EOL> try : <EOL> tolist = args [ <NUM_LIT:0> ] <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> detail_variables ( ip ) <EOL> else : <EOL> print ( "<STR_LIT>" , tolist , "<STR_LIT::>" ) <EOL> detail_variables ( ip , tolist ) <EOL> elif not args : <EOL> vars = self . db . keys ( '<STR_LIT>' ) <EOL> vars . sort ( ) <EOL> if vars : <EOL> size = max ( list ( map ( len , vars ) ) ) - <NUM_LIT:4> <EOL> else : <EOL> size = <NUM_LIT:0> <EOL> print ( '<STR_LIT>' ) <EOL> fmt = '<STR_LIT>' + str ( size ) + '<STR_LIT>' <EOL> get = db . get <EOL> for var in vars : <EOL> justkey = os . path . basename ( var ) <EOL> v = get ( var , '<STR_LIT>' ) <EOL> try : <EOL> print ( fmt % ( justkey , datetime . ctime ( v . get ( '<STR_LIT:time>' , '<STR_LIT>' ) ) , v . get ( '<STR_LIT>' , '<STR_LIT>' ) [ : <NUM_LIT> ] . replace ( '<STR_LIT:\n>' , '<STR_LIT:U+0020>' ) , ) ) <EOL> except AttributeError : <EOL> print ( fmt % ( justkey , '<STR_LIT>' , '<STR_LIT>' , repr ( v ) [ : <NUM_LIT:50> ] ) ) <EOL> else : <EOL> if len ( args ) > <NUM_LIT:1> and args [ <NUM_LIT:1> ] . startswith ( '<STR_LIT:>>' ) : <EOL> fnam = os . path . expanduser ( args [ <NUM_LIT:1> ] . lstrip ( '<STR_LIT:>>' ) . lstrip ( ) ) <EOL> if args [ <NUM_LIT:1> ] . startswith ( '<STR_LIT>' ) : <EOL> fil = open ( fnam , '<STR_LIT:a>' ) <EOL> else : <EOL> fil = open ( fnam , '<STR_LIT:w>' ) <EOL> obj = ip . ev ( args [ <NUM_LIT:0> ] ) <EOL> print ( "<STR_LIT>" % ( args [ <NUM_LIT:0> ] , <EOL> obj . __class__ . __name__ , fnam ) ) <EOL> if not isinstance ( obj , str ) : <EOL> from pprint import pprint <EOL> pprint ( obj , fil ) <EOL> else : <EOL> fil . write ( obj ) <EOL> if not obj . endswith ( '<STR_LIT:\n>' ) : <EOL> fil . write ( '<STR_LIT:\n>' ) <EOL> fil . close ( ) <EOL> return <EOL> try : <EOL> obj = ip . user_ns [ args [ <NUM_LIT:0> ] ] <EOL> except KeyError : <EOL> print ( ) <EOL> print ( "<STR_LIT>" % args [ <NUM_LIT:0> ] ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" + "<STR_LIT>" ) <EOL> return <EOL> else : <EOL> if isinstance ( inspect . getmodule ( obj ) , FakeModule ) : <EOL> print ( textwrap . dedent ( """<STR_LIT>""" % ( args [ <NUM_LIT:0> ] , obj ) ) ) <EOL> return <EOL> jot_obj ( self , obj , args [ <NUM_LIT:0> ] ) <EOL> def magic_read ( self , parameter_s = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> opts , argsl = self . parse_options ( parameter_s , '<STR_LIT>' , mode = '<STR_LIT:string>' ) <EOL> args = argsl . split ( None , <NUM_LIT:1> ) <EOL> ip = self . getapi ( ) <EOL> db = ip . db <EOL> try : <EOL> toret = args [ <NUM_LIT:0> ] <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> else : <EOL> return read_variables ( ip , toret ) <EOL> ip . define_magic ( '<STR_LIT>' , magic_jot ) <EOL> ip . define_magic ( '<STR_LIT>' , magic_read ) </s>
