Changeset 5856
- Timestamp:
- Oct 22, 2008, 3:32:09 PM (16 years ago)
- Location:
- anuga_core/source/anuga/caching
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/caching/caching.py
r5855 r5856 875 875 bytecode = get_bytecode(func) 876 876 877 #print 'Diags'878 #print argsref879 #print args880 877 #print compare(argsref,args), 881 882 878 #print compare(kwargsref,kwargs), 883 879 #print compare(bytecode,coderef) … … 1327 1323 # ----------------------------------------------------------------------------- 1328 1324 1329 1330 def Xmyhash(T, ids=None):1331 import pickle as pickler # Use non-C version here1332 return hash(pickler.dumps(T,0))1333 1334 1325 1335 1326 def myhash(T, ids=None): … … 1347 1338 from types import TupleType, ListType, DictType, InstanceType 1348 1339 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]: 1354 1343 # Keep track of unique id's to protect against infinite recursion 1355 1344 if ids is None: ids = [] … … 1359 1348 1360 1349 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 1368 1351 else: 1369 #print 'Appending', T, id(T)1370 1352 ids.append(i) 1371 1353 1372 1354 1355 1373 1356 # Start hashing 1374 1375 1357 1376 1358 # On some architectures None, False and True gets different hash values … … 1395 1377 val = myhash(I, ids) 1396 1378 elif type(T) == ArrayType: 1397 val = hash(average(T.flat)) # Use mean value for efficiency1398 #val = myhash(tuple(T), ids)1379 # Use mean value for efficiency 1380 val = hash(average(T.flat)) 1399 1381 elif type(T) == InstanceType: 1400 1382 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', val1406 1407 1383 else: 1408 1384 try: … … 1426 1402 """ 1427 1403 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: 1431 1412 # Use pickle to compare data 1432 1413 # The native pickler must be used … … 1435 1416 1436 1417 import pickle as pickler # Use non-C version here 1418 #import cPickle as pickler 1437 1419 identical = (pickler.dumps(A,0) == pickler.dumps(B,0)) 1438 1420 … … 1440 1422 1441 1423 1442 def Xcompare(A, B, ids=None):1424 def old_compare(A, B, ids=None): 1443 1425 """Safe comparison of general objects 1444 1426 … … 1450 1432 """ 1451 1433 1434 # FIXME (Ole): This is probably obsolete now 1435 1452 1436 from types import TupleType, ListType, DictType, InstanceType 1453 1437 … … 2161 2145 print_header_box('Evaluating function %s' %funcname) 2162 2146 2163 msg7(args, kwargs)2147 msg7(args, kwargs) 2164 2148 msg8(reason) 2165 2149 … … 2328 2312 print string.ljust('| Function:', textwidth1) + funcname 2329 2313 2330 msg7(args, kwargs)2314 msg7(args, kwargs) 2331 2315 2332 2316 # ----------------------------------------------------------------------------- 2333 2317 2334 def msg7(args, kwargs):2318 def msg7(args, kwargs): 2335 2319 """Message 7 2336 2320 2337 2321 USAGE: 2338 msg7(args, kwargs):2322 msg7(args, kwargs): 2339 2323 """ 2340 2324 … … 2417 2401 # ----------------------------------------------------------------------------- 2418 2402 2419 def mkargstr(args, textwidth, argstr = '' ):2403 def mkargstr(args, textwidth, argstr = '', level=0): 2420 2404 """ Generate a string containing first textwidth characters of arguments. 2421 2405 2422 2406 USAGE: 2423 mkargstr(args, textwidth, argstr = '' )2407 mkargstr(args, textwidth, argstr = '', level=0) 2424 2408 2425 2409 DESCRIPTION: … … 2430 2414 import types 2431 2415 2416 if level > 10: 2417 # Protect against circular structures 2418 return '...' 2419 2432 2420 WasTruncated = 0 2433 2421 … … 2451 2439 argstr = argstr + "{" 2452 2440 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) + ", " 2455 2443 if len(argstr) > textwidth: 2456 2444 WasTruncated = 1 … … 2468 2456 argstr = argstr + lc 2469 2457 for arg in args: 2470 argstr = argstr + mkargstr(arg, textwidth ) + ', '2458 argstr = argstr + mkargstr(arg, textwidth, level=level+1) + ', ' 2471 2459 if len(argstr) > textwidth: 2472 2460 WasTruncated = 1 -
anuga_core/source/anuga/caching/test_caching.py
r5855 r5856 382 382 383 383 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): 389 385 """test_caching_of_circular_structures 390 386 … … 393 389 """ 394 390 395 verbose = True391 verbose = False #True 396 392 397 393 # Create input argument … … 400 396 A['B'] = B # Make it circular 401 397 402 print A403 404 398 # Test caching 405 399 comprange = 2 … … 409 403 T1 = cache(f_generic, A, evaluate=1, 410 404 compression=comp, verbose=verbose) 411 412 import sys; sys.exit()413 405 414 406 … … 425 417 426 418 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 431 423 432 424
Note: See TracChangeset
for help on using the changeset viewer.