<s> from __future__ import generators <EOL> from schema import * <EOL> import classutil <EOL> def update_graph ( self , graph ) : <EOL> '<STR_LIT>' <EOL> for node , d in graph . iteritems ( ) : <EOL> self += node <EOL> saveDict = self [ node ] <EOL> for target , edge in d . iteritems ( ) : <EOL> saveDict [ target ] = edge <EOL> class PathList ( list ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , nodes = None , edges = None ) : <EOL> if nodes != None : <EOL> list . __init__ ( self , nodes ) <EOL> else : <EOL> list . __init__ ( self ) <EOL> if edges != None : <EOL> self . edge = list ( edges ) <EOL> else : <EOL> self . edge = [ ] <EOL> def append ( self , val ) : <EOL> list . append ( self , val ) <EOL> self . edge . append ( val ) <EOL> def extend ( self , l ) : <EOL> list . extend ( self , l ) <EOL> try : <EOL> self . edge . extend ( l . edge ) <EOL> except AttributeError : <EOL> self . edge . extend ( len ( l ) * [ None ] ) <EOL> class Edge ( list ) : <EOL> "<STR_LIT>" <EOL> isDirected = False <EOL> def __init__ ( self , graph , nodes , edgeInfo ) : <EOL> self . graph = graph <EOL> if edgeInfo : <EOL> self . edgeInfo = edgeInfo <EOL> list . __init__ ( self , nodes ) <EOL> def __getattr__ ( self , attr ) : <EOL> try : <EOL> return getattr ( self . edgeInfo , attr ) <EOL> except AttributeError : <EOL> if isinstance ( self . edgeInfo , types . DictType ) : <EOL> return self . edgeInfo [ attr ] <EOL> raise AttributeError ( attr ) <EOL> def __cmp__ ( self , other ) : <EOL> if not isinstance ( other , Edge ) : <EOL> return - <NUM_LIT:1> <EOL> diff = cmp ( self . graph , other . graph ) <EOL> if diff : <EOL> return diff <EOL> elif self . isDirected : <EOL> return tuple . __cmp__ ( self , other ) <EOL> else : <EOL> me = [ i for i in self ] <EOL> you = [ i for i in other ] <EOL> me . sort ( ) <EOL> you . sort ( ) <EOL> return cmp ( me , you ) <EOL> class DirectedEdge ( Edge ) : <EOL> isDirected = True <EOL> class dictEdge ( dict ) : <EOL> """<STR_LIT>""" <EOL> dictClass = dict <EOL> def __init__ ( self , graph , fromNode ) : <EOL> self . graph = graph <EOL> self . fromNode = fromNode <EOL> self . dictClass . __init__ ( self ) <EOL> def __iadd__ ( self , target ) : <EOL> "<STR_LIT>" <EOL> self [ target ] = None <EOL> return self <EOL> def __setitem__ ( self , target , edgeInfo ) : <EOL> "<STR_LIT>" <EOL> self . dictClass . __setitem__ ( self , target , edgeInfo ) <EOL> if target not in self . graph : <EOL> self . graph += target <EOL> _setitem_ = dict . __setitem__ <EOL> def __delitem__ ( self , target ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> self . dictClass . __delitem__ ( self , target ) <EOL> except KeyError : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> def __isub__ ( self , target ) : <EOL> "<STR_LIT>" <EOL> self . __delitem__ ( target ) <EOL> return self <EOL> def edges ( self ) : <EOL> "<STR_LIT>" <EOL> for target , edgeInfo in self . items ( ) : <EOL> if isinstance ( edgeInfo , Edge ) : <EOL> yield edgeInfo <EOL> else : <EOL> yield Edge ( self . graph , ( self . fromNode , target , edgeInfo ) , <EOL> edgeInfo ) <EOL> class dictGraph ( dict ) : <EOL> """<STR_LIT>""" <EOL> dictClass = dict <EOL> edgeDictClass = dictEdge <EOL> def __init__ ( self , schema = None , domain = None , range = None ) : <EOL> if schema and domain and range : <EOL> if domain not in schema : <EOL> schema += domain <EOL> schema [ domain ] [ range ] = self <EOL> self . dictClass . __init__ ( self ) <EOL> def __iadd__ ( self , node , ruleSet = False ) : <EOL> "<STR_LIT>" <EOL> if node not in self : <EOL> self . dictClass . __setitem__ ( self , node , self . edgeDictClass ( self , <EOL> node ) ) <EOL> if ruleSet == False : <EOL> ruleSet = getschema ( node , graph = self ) <EOL> for rule in ruleSet : <EOL> if isinstance ( rule [ <NUM_LIT:1> ] , types . StringType ) : <EOL> setattr ( node , rule [ <NUM_LIT:1> ] , self [ node ] ) <EOL> return self <EOL> def __setitem__ ( self , node , target ) : <EOL> "<STR_LIT>" <EOL> if self [ node ] != target : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def __delitem__ ( self , node ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> self . dictClass . __delitem__ ( self , node ) <EOL> except KeyError : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> for rule in getschema ( node , graph = self ) : <EOL> if isinstance ( rule [ <NUM_LIT:1> ] , types . StringType ) : <EOL> delattr ( node , rule [ <NUM_LIT:1> ] ) <EOL> def __isub__ ( self , node ) : <EOL> "<STR_LIT>" <EOL> self . __delitem__ ( node ) <EOL> return self <EOL> def __hash__ ( self ) : <EOL> return id ( self ) <EOL> def edges ( self ) : <EOL> "<STR_LIT>" <EOL> for edgedict in self . values ( ) : <EOL> for edge in edgedict . edges ( ) : <EOL> yield edge <EOL> update = update_graph <EOL> class dictEdgeFB ( dictEdge ) : <EOL> "<STR_LIT>" <EOL> def __setitem__ ( self , target , edgeInfo ) : <EOL> "<STR_LIT>" <EOL> dictEdge . __setitem__ ( self , target , edgeInfo ) <EOL> try : <EOL> d = self . graph . _inverse [ target ] <EOL> except KeyError : <EOL> d = self . dictClass ( ) <EOL> self . graph . _inverse [ target ] = d <EOL> d [ self . fromNode ] = edgeInfo <EOL> def __invert__ ( self ) : <EOL> "<STR_LIT>" <EOL> return self . graph . _inverse [ self . fromNode ] <EOL> class dictGraphFB ( dictGraph ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , ** kwargs ) : <EOL> dictGraph . __init__ ( self , ** kwargs ) <EOL> self . _inverse = self . dictClass ( ) <EOL> __invert__ = classutil . standard_invert <EOL> def __delitem__ ( self , node ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> fromNodes = self . _inverse [ node ] <EOL> del self . _inverse [ node ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> for i in fromNodes : <EOL> del self [ i ] [ node ] <EOL> dictGraph . __delitem__ ( self , node ) <EOL> def listUnion ( ivals ) : <EOL> '<STR_LIT>' <EOL> union = None <EOL> for ival in ivals : <EOL> try : <EOL> union += ival <EOL> except TypeError : <EOL> union = ival <EOL> return union <EOL> class DictQueue ( dict ) : <EOL> '<STR_LIT>' <EOL> def __setitem__ ( self , k , val ) : <EOL> try : <EOL> dict . __getitem__ ( self , k ) . append ( val ) <EOL> except KeyError : <EOL> dict . __setitem__ ( self , k , [ val ] ) <EOL> def __getitem__ ( self , k ) : <EOL> return dict . __getitem__ ( self , k ) [ <NUM_LIT:0> ] <EOL> def __delitem__ ( self , k ) : <EOL> l = dict . __getitem__ ( self , k ) <EOL> del l [ <NUM_LIT:0> ] <EOL> if len ( l ) == <NUM_LIT:0> : <EOL> dict . __delitem__ ( self , k ) <EOL> def close_if_possible ( self ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> do_close = self . d . close <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> do_close ( ) <EOL> class Collection ( object ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , saveDict = None , dictClass = dict , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if saveDict is not None : <EOL> self . d = saveDict <EOL> elif '<STR_LIT:filename>' in kwargs : <EOL> try : <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> self . __class__ = IntShelve <EOL> else : <EOL> raise KeyError <EOL> except KeyError : <EOL> self . __class__ = PicklableShelve <EOL> return self . __init__ ( ** kwargs ) <EOL> else : <EOL> self . d = dictClass ( ) <EOL> classutil . apply_itemclass ( self , kwargs ) <EOL> def __getitem__ ( self , k ) : <EOL> return self . d [ k ] <EOL> def __setitem__ ( self , k , v ) : <EOL> self . d [ k ] = v <EOL> def __delitem__ ( self , k ) : <EOL> del self . d [ k ] <EOL> def __len__ ( self ) : <EOL> return len ( self . d ) <EOL> def __contains__ ( self , k ) : <EOL> return k in self . d <EOL> def __iter__ ( self ) : <EOL> return iter ( self . d ) <EOL> def __getattr__ ( self , attr ) : <EOL> if attr == '<STR_LIT>' or attr == '<STR_LIT>' : <EOL> raise AttributeError <EOL> try : <EOL> return getattr ( self . __dict__ [ '<STR_LIT:d>' ] , attr ) <EOL> except KeyError : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> close = close_if_possible <EOL> def __del__ ( self ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> self . close ( ) <EOL> except classutil . FileAlreadyClosedError : <EOL> pass <EOL> class PicklableShelve ( Collection ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , filename , mode = None , writeback = False , <EOL> unpicklingMode = False , verbose = True , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> self . filename = classutil . SourceFileName ( str ( filename ) ) <EOL> self . writeback = writeback <EOL> if unpicklingMode or mode is None or mode == '<STR_LIT:r>' : <EOL> self . mode = mode <EOL> elif mode == '<STR_LIT:n>' or mode == '<STR_LIT:c>' or mode == '<STR_LIT:w>' : <EOL> if verbose : <EOL> import sys <EOL> print >> sys . stderr , '''<STR_LIT>''' % ( filename , mode , mode ) <EOL> self . mode = '<STR_LIT:r>' <EOL> else : <EOL> try : <EOL> if len ( mode ) == <NUM_LIT:2> and mode [ <NUM_LIT:0> ] in '<STR_LIT>' and mode [ <NUM_LIT:1> ] in '<STR_LIT>' : <EOL> self . mode = mode [ <NUM_LIT:1> ] <EOL> mode = mode [ <NUM_LIT:0> ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' + mode ) <EOL> except TypeError : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if unpicklingMode : <EOL> self . d = classutil . open_shelve ( filename , mode , writeback , <EOL> allowReadOnly = True ) <EOL> else : <EOL> self . d = classutil . open_shelve ( filename , mode , writeback ) <EOL> classutil . apply_itemclass ( self , kwargs ) <EOL> __getstate__ = classutil . standard_getstate <EOL> __setstate__ = classutil . standard_setstate <EOL> _pickleAttrs = dict ( filename = <NUM_LIT:0> , mode = <NUM_LIT:0> , writeback = <NUM_LIT:0> ) <EOL> def close ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . d . close ( ) <EOL> def __setitem__ ( self , k , v ) : <EOL> try : <EOL> self . d [ k ] = v <EOL> except TypeError : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def reopen ( self , mode = '<STR_LIT:r>' ) : <EOL> '<STR_LIT>' <EOL> self . close ( ) <EOL> self . d = classutil . open_shelve ( self . filename , mode , <EOL> writeback = self . writeback ) <EOL> self . mode = mode <EOL> class IntShelve ( PicklableShelve ) : <EOL> '<STR_LIT>' <EOL> def saveKey ( self , i ) : <EOL> '<STR_LIT>' <EOL> if isinstance ( i , int ) : <EOL> return '<STR_LIT>' % i <EOL> elif isinstance ( i , str ) : <EOL> return i <EOL> try : <EOL> return '<STR_LIT>' % int ( i ) <EOL> except TypeError : <EOL> pass <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> def trueKey ( self , k ) : <EOL> "<STR_LIT>" <EOL> if k . startswith ( '<STR_LIT>' ) : <EOL> return int ( k [ <NUM_LIT:4> : ] ) <EOL> else : <EOL> return k <EOL> def __getitem__ ( self , k ) : <EOL> return self . d [ self . saveKey ( k ) ] <EOL> def __setitem__ ( self , k , v ) : <EOL> self . d [ self . saveKey ( k ) ] = v <EOL> def __delitem__ ( self , k ) : <EOL> del self . d [ self . saveKey ( k ) ] <EOL> def __contains__ ( self , k ) : <EOL> return self . saveKey ( k ) in self . d <EOL> def __iter__ ( self ) : <EOL> for k in self . d : <EOL> yield self . trueKey ( k ) <EOL> def keys ( self ) : <EOL> return [ k for k in self ] <EOL> def iteritems ( self ) : <EOL> for k , v in self . d . iteritems ( ) : <EOL> yield self . trueKey ( k ) , v <EOL> def items ( self ) : <EOL> return [ k for k in self . iteritems ( ) ] <EOL> def pack_id ( self , obj ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> return obj . id <EOL> except AttributeError : <EOL> if obj is None : <EOL> return None <EOL> raise <EOL> def unpack_source ( self , objID ) : <EOL> return self . sourceDB [ objID ] <EOL> def unpack_target ( self , objID ) : <EOL> return self . targetDB [ objID ] <EOL> def unpack_edge ( self , objID ) : <EOL> try : <EOL> return self . edgeDB [ objID ] <EOL> except KeyError : <EOL> if objID is None : <EOL> return None <EOL> raise <EOL> def add_standard_packing_methods ( localDict ) : <EOL> localDict [ '<STR_LIT>' ] = pack_id <EOL> localDict [ '<STR_LIT>' ] = pack_id <EOL> localDict [ '<STR_LIT>' ] = pack_id <EOL> localDict [ '<STR_LIT>' ] = unpack_source <EOL> localDict [ '<STR_LIT>' ] = unpack_target <EOL> localDict [ '<STR_LIT>' ] = unpack_edge <EOL> def add_trivial_packing_methods ( localDict ) : <EOL> for name in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> localDict [ name ] = lambda self , obj : obj <EOL> def pack_pickle ( self , obj ) : <EOL> '<STR_LIT>' <EOL> import pickle <EOL> return pickle . dumps ( obj ) <EOL> def unpack_pickle ( self , s ) : <EOL> '<STR_LIT>' <EOL> import pickle <EOL> return pickle . loads ( s ) <EOL> class MappingInverse ( object ) : <EOL> def __init__ ( self , db ) : <EOL> self . _inverse = db <EOL> self . attr = db . inverseAttr <EOL> def __getitem__ ( self , k ) : <EOL> return self . _inverse . sourceDB [ getattr ( k , self . attr ) ] <EOL> __invert__ = classutil . standard_invert <EOL> class Mapping ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , sourceDB , targetDB , saveDict = None , IDAttr = '<STR_LIT:id>' , <EOL> targetIDAttr = '<STR_LIT:id>' , itemAttr = None , multiValue = False , <EOL> inverseAttr = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if saveDict is None : <EOL> self . d = classutil . get_shelve_or_dict ( ** kwargs ) <EOL> else : <EOL> self . d = saveDict <EOL> self . IDAttr = IDAttr <EOL> self . targetIDAttr = targetIDAttr <EOL> self . itemAttr = itemAttr <EOL> self . multiValue = multiValue <EOL> self . sourceDB = sourceDB <EOL> self . targetDB = targetDB <EOL> if inverseAttr is not None : <EOL> self . inverseAttr = inverseAttr <EOL> def __getitem__ ( self , k ) : <EOL> kID = getattr ( k , self . IDAttr ) <EOL> return self . getTarget ( self . d [ kID ] ) <EOL> def getTarget ( self , vID ) : <EOL> if self . itemAttr is not None : <EOL> vID = getattr ( vID , self . itemAttr ) <EOL> if self . multiValue : <EOL> return [ self . targetDB [ j ] for j in vID ] <EOL> else : <EOL> return self . targetDB [ vID ] <EOL> def __setitem__ ( self , k , v ) : <EOL> if self . multiValue : <EOL> v = [ getattr ( x , self . targetIDAttr ) for x in v ] <EOL> else : <EOL> v = getattr ( v , self . targetIDAttr ) <EOL> self . d [ getattr ( k , self . IDAttr ) ] = v <EOL> def __delitem__ ( self , k ) : <EOL> del self . d [ getattr ( k , self . IDAttr ) ] <EOL> def __contains__ ( self , k ) : <EOL> return getattr ( k , self . IDAttr ) in self . d <EOL> def __len__ ( self ) : <EOL> return len ( self . d ) <EOL> def clear ( self ) : <EOL> self . d . clear ( ) <EOL> def copy ( self ) : <EOL> return Mapping ( self . sourceDB , self . targetDB , self . d . copy ( ) , <EOL> self . IDAttr , self . targetIDAttr , self . itemAttr , <EOL> self . multiValue ) <EOL> def update ( self , b ) : <EOL> for k , v in b . iteritems ( ) : <EOL> self [ k ] = v <EOL> def get ( self , k , v = None ) : <EOL> try : <EOL> return self [ k ] <EOL> except KeyError : <EOL> return v <EOL> def setdefault ( self , k , v = None ) : <EOL> try : <EOL> return self [ k ] <EOL> except KeyError : <EOL> self [ k ] = v <EOL> return v <EOL> def pop ( self , k , v = None ) : <EOL> try : <EOL> v = self [ k ] <EOL> except KeyError : <EOL> return v <EOL> del self [ k ] <EOL> return v <EOL> def popitem ( self ) : <EOL> kID , vID = self . d . popitem ( ) <EOL> return kID , self . getTarget ( vID ) <EOL> def __iter__ ( self ) : <EOL> for kID in self . d : <EOL> yield self . sourceDB [ kID ] <EOL> def keys ( self ) : <EOL> return [ k for k in self ] <EOL> def itervalues ( self ) : <EOL> for vID in self . d . itervalues ( ) : <EOL> yield self . getTarget ( vID ) <EOL> def values ( self ) : <EOL> return [ v for v in self . itervalues ( ) ] <EOL> def iteritems ( self ) : <EOL> for kID , vID in self . d . iteritems ( ) : <EOL> yield self . sourceDB [ kID ] , self . getTarget ( vID ) <EOL> def items ( self ) : <EOL> return [ x for x in self . iteritems ( ) ] <EOL> __invert__ = classutil . standard_invert <EOL> _inverseClass = MappingInverse <EOL> close = close_if_possible <EOL> def __del__ ( self ) : <EOL> close_if_possible ( self ) <EOL> def graph_cmp ( self , other ) : <EOL> '<STR_LIT>' <EOL> import sys <EOL> diff = cmp ( len ( self ) , len ( other ) ) <EOL> if diff != <NUM_LIT:0> : <EOL> print >> sys . stderr , '<STR_LIT>' , len ( self ) , len ( other ) <EOL> return diff <EOL> for node , d in self . iteritems ( ) : <EOL> try : <EOL> d2 = other [ node ] <EOL> except KeyError : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> return <NUM_LIT:1> <EOL> diff = cmp ( d , d2 ) <EOL> if diff != <NUM_LIT:0> : <EOL> print >> sys . stderr , '<STR_LIT>' , d , d2 <EOL> return diff <EOL> return <NUM_LIT:0> <EOL> class IDNodeDict ( object ) : <EOL> """<STR_LIT>""" <EOL> dictClass = dict <EOL> def __init__ ( self , graph , fromNode ) : <EOL> self . graph = graph <EOL> self . fromNode = fromNode <EOL> def __getitem__ ( self , target ) : <EOL> edgeID = self . graph . d [ self . fromNode ] [ self . graph . pack_target ( target ) ] <EOL> return self . graph . unpack_edge ( edgeID ) <EOL> def __setitem__ ( self , target , edgeInfo ) : <EOL> "<STR_LIT>" <EOL> self . graph . d [ self . fromNode ] [ self . graph . pack_target ( target ) ] = self . graph . pack_edge ( edgeInfo ) <EOL> if not hasattr ( self . graph , '<STR_LIT>' ) or ( hasattr ( self . graph , '<STR_LIT>' ) and self . graph . sourceDB == self . graph . targetDB ) : <EOL> self . graph += target <EOL> def __delitem__ ( self , target ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> del self . graph . d [ self . fromNode ] [ self . graph . pack_target ( target ) ] <EOL> except KeyError : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> def __iadd__ ( self , target ) : <EOL> "<STR_LIT>" <EOL> self [ target ] = None <EOL> return self <EOL> def __isub__ ( self , target ) : <EOL> "<STR_LIT>" <EOL> self . __delitem__ ( target ) <EOL> return self <EOL> def edges ( self ) : <EOL> "<STR_LIT>" <EOL> for target , edgeInfo in self . graph . d [ self . fromNode ] . items ( ) : <EOL> yield ( self . graph . unpack_source ( self . fromNode ) , <EOL> self . graph . unpack_target ( target ) , <EOL> self . graph . unpack_edge ( edgeInfo ) ) <EOL> def __len__ ( self ) : <EOL> return len ( self . graph . d [ self . fromNode ] ) <EOL> def keys ( self ) : <EOL> return [ k [ <NUM_LIT:1> ] for k in self . edges ( ) ] <EOL> def values ( self ) : <EOL> return [ k [ <NUM_LIT:2> ] for k in self . edges ( ) ] <EOL> def items ( self ) : <EOL> return [ k [ <NUM_LIT:1> : <NUM_LIT:3> ] for k in self . edges ( ) ] <EOL> def __iter__ ( self ) : <EOL> for source , target , edgeInfo in self . edges ( ) : <EOL> yield target <EOL> def itervalues ( self ) : <EOL> for source , target , edgeInfo in self . edges ( ) : <EOL> yield edgeInfo <EOL> def iteritems ( self ) : <EOL> for source , target , edgeInfo in self . edges ( ) : <EOL> yield target , edgeInfo <EOL> __cmp__ = graph_cmp <EOL> class IDNodeDictWriteback ( IDNodeDict ) : <EOL> '<STR_LIT>' <EOL> def __setitem__ ( self , target , edgeInfo ) : <EOL> d = self . graph . d [ self . fromNode ] <EOL> d [ self . graph . pack_target ( target ) ] = self . graph . pack_edge ( edgeInfo ) <EOL> self . graph . d [ self . fromNode ] = d <EOL> self . graph += target <EOL> def __delitem__ ( self , target ) : <EOL> d = self . graph . d [ self . fromNode ] <EOL> del d [ self . graph . pack_target ( target ) ] <EOL> self . graph . d [ self . fromNode ] = d <EOL> class IDNodeDictWriteNow ( IDNodeDictWriteback ) : <EOL> '<STR_LIT>' <EOL> def __setitem__ ( self , target , edgeInfo ) : <EOL> self . graph . d . reopen ( '<STR_LIT:w>' ) <EOL> IDNodeDictWriteback . __setitem__ ( self , target , edgeInfo ) <EOL> self . graph . d . reopen ( '<STR_LIT:w>' ) <EOL> def __delitem__ ( self , target ) : <EOL> self . graph . d . reopen ( '<STR_LIT:w>' ) <EOL> IDNodeDictWriteback . __delitem__ ( self , target ) <EOL> self . graph . d . reopen ( '<STR_LIT:w>' ) <EOL> class IDGraphEdges ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , g ) : <EOL> self . g = g <EOL> def __iter__ ( self ) : <EOL> for d in self . g . itervalues ( ) : <EOL> for edge in d . edges ( ) : <EOL> yield edge <EOL> def __getitem__ ( self , edge ) : <EOL> l = [ ] <EOL> for sourceID , targetID in self . d [ edge . id ] : <EOL> l . append ( ( self . g . sourceDB [ sourceID ] , self . g . targetDB [ targetID ] ) ) <EOL> return l <EOL> def __call__ ( self ) : <EOL> return self <EOL> class IDGraphEdgeDescriptor ( object ) : <EOL> '<STR_LIT>' <EOL> def __get__ ( self , obj , objtype ) : <EOL> return IDGraphEdges ( obj ) <EOL> def save_graph_db_refs ( self , sourceDB = None , targetDB = None , edgeDB = None , <EOL> simpleKeys = False , unpack_edge = None , <EOL> edgeDictClass = None , graph = None , ** kwargs ) : <EOL> '<STR_LIT>' <EOL> if sourceDB is not None : <EOL> self . sourceDB = sourceDB <EOL> else : <EOL> simpleKeys = True <EOL> if targetDB is not None : <EOL> self . targetDB = targetDB <EOL> if edgeDB is not None : <EOL> self . edgeDB = edgeDB <EOL> else : <EOL> self . pack_edge = self . unpack_edge = lambda edge : edge <EOL> if simpleKeys : <EOL> self . __class__ = self . _IDGraphClass <EOL> if unpack_edge is not None : <EOL> self . unpack_edge = unpack_edge <EOL> if graph is not None : <EOL> self . graph = graph <EOL> if edgeDictClass is not None : <EOL> self . edgeDictClass = edgeDictClass <EOL> def graph_db_inverse_refs ( self , edgeIndex = False ) : <EOL> '<STR_LIT>' <EOL> if edgeIndex : <EOL> db = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> db = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> try : <EOL> d = dict ( sourceDB = getattr ( self , db [ <NUM_LIT:0> ] ) , targetDB = getattr ( self , db [ <NUM_LIT:1> ] ) ) <EOL> try : <EOL> d [ '<STR_LIT>' ] = getattr ( self , db [ <NUM_LIT:2> ] ) <EOL> except AttributeError : <EOL> pass <EOL> except AttributeError : <EOL> d = dict ( simpleKeys = True ) <EOL> try : <EOL> if not edgeIndex : <EOL> d [ '<STR_LIT>' ] = self . __dict__ [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> return d <EOL> def graph_setitem ( self , node , target ) : <EOL> "<STR_LIT>" <EOL> node = self . pack_source ( node ) <EOL> try : <EOL> if node == target . fromNode : <EOL> return <EOL> except AttributeError : <EOL> pass <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> class Graph ( object ) : <EOL> """<STR_LIT>""" <EOL> edgeDictClass = IDNodeDict <EOL> def __init__ ( self , saveDict = None , dictClass = dict , writeNow = False , <EOL> ** kwargs ) : <EOL> if saveDict is not None : <EOL> self . d = saveDict <EOL> elif '<STR_LIT:filename>' in kwargs : <EOL> try : <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> self . d = IntShelve ( writeback = False , ** kwargs ) <EOL> else : <EOL> raise KeyError <EOL> except KeyError : <EOL> self . d = PicklableShelve ( writeback = False , ** kwargs ) <EOL> if writeNow : <EOL> self . edgeDictClass = IDNodeDictWriteNow <EOL> else : <EOL> self . edgeDictClass = IDNodeDictWriteback <EOL> else : <EOL> self . d = dictClass ( ) <EOL> save_graph_db_refs ( self , ** kwargs ) <EOL> __getstate__ = classutil . standard_getstate <EOL> __setstate__ = classutil . standard_setstate <EOL> _pickleAttrs = dict ( d = '<STR_LIT>' , sourceDB = <NUM_LIT:0> , targetDB = <NUM_LIT:0> , edgeDB = <NUM_LIT:0> , <EOL> edgeDictClass = <NUM_LIT:0> ) <EOL> add_standard_packing_methods ( locals ( ) ) <EOL> def close ( self ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> do_close = self . d . close <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> do_close ( ) <EOL> def __len__ ( self ) : <EOL> return len ( self . d ) <EOL> def __iter__ ( self ) : <EOL> for node in self . d : <EOL> yield self . unpack_source ( node ) <EOL> def keys ( self ) : <EOL> return [ k for k in self ] <EOL> def itervalues ( self ) : <EOL> for node in self . d : <EOL> yield self . edgeDictClass ( self , node ) <EOL> def values ( self ) : <EOL> return [ v for v in self . itervalues ( ) ] <EOL> def iteritems ( self ) : <EOL> for node in self . d : <EOL> yield self . unpack_source ( node ) , self . edgeDictClass ( self , node ) <EOL> def items ( self ) : <EOL> return [ v for v in self . iteritems ( ) ] <EOL> edges = IDGraphEdgeDescriptor ( ) <EOL> def __iadd__ ( self , node ) : <EOL> "<STR_LIT>" <EOL> node = self . pack_source ( node ) <EOL> if node not in self . d : <EOL> self . d [ node ] = { } <EOL> return self <EOL> def __contains__ ( self , node ) : <EOL> return self . pack_source ( node ) in self . d <EOL> def __getitem__ ( self , node ) : <EOL> if node in self : <EOL> return self . edgeDictClass ( self , self . pack_source ( node ) ) <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> __setitem__ = graph_setitem <EOL> def __delitem__ ( self , node ) : <EOL> "<STR_LIT>" <EOL> node = self . pack_source ( node ) <EOL> try : <EOL> del self . d [ node ] <EOL> except KeyError : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> def __isub__ ( self , node ) : <EOL> "<STR_LIT>" <EOL> self . __delitem__ ( node ) <EOL> return self <EOL> update = update_graph <EOL> __cmp__ = graph_cmp <EOL> def __del__ ( self ) : <EOL> try : <EOL> self . close ( ) <EOL> except classutil . FileAlreadyClosedError : <EOL> pass <EOL> def __hash__ ( self ) : <EOL> return id ( self ) <EOL> class IDGraph ( Graph ) : <EOL> add_trivial_packing_methods ( locals ( ) ) <EOL> Graph . _IDGraphClass = IDGraph <EOL> class KeepUniqueDict ( dict ) : <EOL> '<STR_LIT>' <EOL> def __setitem__ ( self , k , v ) : <EOL> try : <EOL> if self [ k ] is v : <EOL> return <EOL> except KeyError : <EOL> dict . __setitem__ ( self , k , v ) <EOL> return <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> def __hash__ ( self ) : <EOL> '<STR_LIT>' <EOL> return id ( self ) </s>
