Changeset 5856


Ignore:
Timestamp:
Oct 22, 2008, 3:32:09 PM (16 years ago)
Author:
ole
Message:

A bit more fiddling with caching and circular structures not
involving instances.

All relevant tests pass

Location:
anuga_core/source/anuga/caching
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/caching/caching.py

    r5855 r5856  
    875875  bytecode = get_bytecode(func)
    876876
    877   #print 'Diags'
    878   #print argsref
    879   #print args
    880877  #print compare(argsref,args),   
    881  
    882878  #print compare(kwargsref,kwargs),
    883879  #print compare(bytecode,coderef)
     
    13271323# -----------------------------------------------------------------------------
    13281324
    1329 
    1330 def Xmyhash(T, ids=None):
    1331     import pickle as pickler # Use non-C version here
    1332     return hash(pickler.dumps(T,0))
    1333 
    13341325   
    13351326def myhash(T, ids=None):
     
    13471338  from types import TupleType, ListType, DictType, InstanceType 
    13481339  from Numeric import ArrayType, average
    1349  
    1350   #if type(T) in [TupleType, ListType, DictType, InstanceType]: 
    1351   #if type(T) in [ListType, DictType, InstanceType]: 
    1352   if type(T) == InstanceType: 
    1353   #if False:
     1340
     1341   
     1342  if type(T) in [TupleType, ListType, DictType, InstanceType]: 
    13541343      # Keep track of unique id's to protect against infinite recursion
    13551344      if ids is None: ids = []
     
    13591348 
    13601349      if i in ids:
    1361           # T has been hashed already     
    1362      
    1363           # FIXME (Ole): It seems that different objects get the same id
    1364           # For example id(D.items()) is the same as id(D.values()).
    1365 
    1366           #print 'T has already been hashed:', mkargstr(T, 132), id(T), type(T)
    1367           return 0
     1350          return 0 # T has been hashed already     
    13681351      else:
    1369           #print 'Appending', T, id(T)
    13701352          ids.append(i)
    13711353   
    13721354
     1355   
    13731356  # Start hashing 
    1374  
    13751357 
    13761358  # On some architectures None, False and True gets different hash values
     
    13951377      val = myhash(I, ids)
    13961378  elif type(T) == ArrayType:
    1397       val = hash(average(T.flat))  # Use mean value for efficiency
    1398       #val = myhash(tuple(T), ids)
     1379      # Use mean value for efficiency 
     1380      val = hash(average(T.flat))
    13991381  elif type(T) == InstanceType:
    14001382      val = myhash(T.__dict__, ids)
    1401      
    1402       #print 'Hashed instance:'
    1403       #print T.__dict__.keys()
    1404       #print mkargstr(T.__dict__, 132)
    1405       #print 'with value', val
    1406 
    14071383  else:
    14081384      try:
     
    14261402    """
    14271403
    1428     if A == B:
    1429         identical = True
    1430     else:
     1404    try:
     1405        identical = (A == B)
     1406    except:
     1407        # E.g. if A and B are circular or otherwise can't be compared.
     1408        identical = False
     1409       
     1410       
     1411    if identical is False:   
    14311412        # Use pickle to compare data
    14321413        # The native pickler must be used
     
    14351416       
    14361417        import pickle as pickler # Use non-C version here
     1418        #import cPickle as pickler
    14371419        identical = (pickler.dumps(A,0) == pickler.dumps(B,0))
    14381420
     
    14401422
    14411423
    1442 def Xcompare(A, B, ids=None):
     1424def old_compare(A, B, ids=None):
    14431425    """Safe comparison of general objects
    14441426
     
    14501432    """
    14511433
     1434    # FIXME (Ole): This is probably obsolete now
     1435   
    14521436    from types import TupleType, ListType, DictType, InstanceType
    14531437   
     
    21612145  print_header_box('Evaluating function %s' %funcname)
    21622146 
    2163   msg7(args,kwargs)
     2147  msg7(args, kwargs)
    21642148  msg8(reason) 
    21652149 
     
    23282312  print string.ljust('| Function:', textwidth1) + funcname
    23292313
    2330   msg7(args,kwargs)
     2314  msg7(args, kwargs)
    23312315 
    23322316# -----------------------------------------------------------------------------   
    23332317
    2334 def msg7(args,kwargs):
     2318def msg7(args, kwargs):
    23352319  """Message 7
    23362320 
    23372321  USAGE:
    2338     msg7(args,kwargs):
     2322    msg7(args, kwargs):
    23392323  """
    23402324 
     
    24172401# -----------------------------------------------------------------------------
    24182402
    2419 def mkargstr(args, textwidth, argstr = ''):
     2403def mkargstr(args, textwidth, argstr = '', level=0):
    24202404  """ Generate a string containing first textwidth characters of arguments.
    24212405
    24222406  USAGE:
    2423     mkargstr(args, textwidth, argstr = '')
     2407    mkargstr(args, textwidth, argstr = '', level=0)
    24242408
    24252409  DESCRIPTION:
     
    24302414  import types
    24312415
     2416  if level > 10:
     2417      # Protect against circular structures
     2418      return '...'
     2419 
    24322420  WasTruncated = 0
    24332421
     
    24512439      argstr = argstr + "{"
    24522440      for key in args.keys():
    2453         argstr = argstr + mkargstr(key, textwidth) + ": " + \
    2454                  mkargstr(args[key], textwidth) + ", "
     2441        argstr = argstr + mkargstr(key, textwidth, level=level+1) + ": " + \
     2442                 mkargstr(args[key], textwidth, level=level+1) + ", "
    24552443        if len(argstr) > textwidth:
    24562444          WasTruncated = 1
     
    24682456      argstr = argstr + lc
    24692457      for arg in args:
    2470         argstr = argstr + mkargstr(arg, textwidth) + ', '
     2458        argstr = argstr + mkargstr(arg, textwidth, level=level+1) + ', '
    24712459        if len(argstr) > textwidth:
    24722460          WasTruncated = 1
  • anuga_core/source/anuga/caching/test_caching.py

    r5855 r5856  
    382382           
    383383
    384     def XXtest_caching_of_simple_circular_structures(self):
    385    
    386         # FIXME (Ole): This one recurses infinitly on
    387         # arg strings.
    388        
     384    def test_caching_of_simple_circular_dictionaries(self):
    389385        """test_caching_of_circular_structures
    390386       
     
    393389        """
    394390       
    395         verbose = True
     391        verbose = False #True
    396392       
    397393        # Create input argument
     
    400396        A['B'] = B # Make it circular
    401397       
    402         print A
    403 
    404398        # Test caching
    405399        comprange = 2
     
    409403            T1 = cache(f_generic, A, evaluate=1,
    410404                       compression=comp, verbose=verbose)
    411                        
    412             import sys; sys.exit()
    413405                       
    414406
     
    425417
    426418
    427             assert T1 == T2, 'Cached result does not match computed result'
    428             assert T2 == T3, 'Cached result does not match computed result'
    429            
    430                                    
     419            msg = 'Cached result does not match computed result'
     420            assert str(T1) == str(T2), msg
     421            assert str(T2) == str(T3), msg
     422
    431423           
    432424           
Note: See TracChangeset for help on using the changeset viewer.