Ignore:
Timestamp:
Jul 22, 2009, 9:22:11 AM (15 years ago)
Author:
rwilson
Message:

Replaced 'print' statements with log.critical() calls.

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

Legend:

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

    r7309 r7317  
    5151  unix = True
    5252
     53import anuga.utilities.log as log
     54
    5355import numpy as num
    5456
     
    332334            os.system('del '+fn)
    333335          if verbose is True:
    334             print 'MESSAGE (caching): File %s deleted' %fn
     336            log.critical('MESSAGE (caching): File %s deleted' % fn)
    335337        ##else:
    336         ##  print '%s was not accessed' %fn
     338        ##  log.critical('%s was not accessed' % fn)
    337339    return None
    338340
     
    468470    import zlib
    469471  except:
    470     print
    471     print '*** Could not find zlib, default to no-compression      ***'
    472     print '*** Installing zlib will improve performance of caching ***'
    473     print
     472    log.critical()
     473    log.critical('*** Could not find zlib, default to no-compression      ***')
     474    log.critical('*** Installing zlib will improve performance of caching ***')
     475    log.critical()
    474476    compression = 0       
    475477    set_option('compression', compression)   
    476478 
    477   print 
    478   print_header_box('Testing caching module - please stand by')
    479   print   
     479  log.critical('\nTesting caching module - please stand by\n')
    480480
    481481  # Define a test function to be cached
     
    709709      test_OK('Performance test: relative time saved = %s pct' \
    710710              %str(round((t1-t2)*100/t1,2)))
    711     #else:
    712     #  print 'WARNING: Performance a bit low - this could be specific to current platform'
    713711  else:       
    714712    test_error('Basic caching failed for new problem')
     
    742740      cachestat()
    743741    except: 
    744       print 'cachestat() does not work here, because it'
    745       print 'relies on time.strptime() which is unavailable in Windows'
     742      log.critical('cachestat() does not work here, because it relies on '
     743                   'time.strptime() which is unavailable in Windows')
    746744     
    747   print
    748745  test_OK('Caching self test completed')   
    749746     
     
    841838  #
    842839  if verbose:
    843     print 'Caching: looking for cached files %s_{%s,%s,%s}.z'\
    844            %(CD+FN, file_types[0], file_types[1], file_types[2])
     840    log.critical('Caching: looking for cached files %s_{%s,%s,%s}.z'
     841                 % (CD+FN, file_types[0], file_types[1], file_types[2]))
    845842  (datafile,compressed0) = myopen(CD+FN+'_'+file_types[0],"rb",compression)
    846843  (argsfile,compressed1) = myopen(CD+FN+'_'+file_types[1],"rb",compression)
     
    848845
    849846  if verbose is True and deps is not None:
    850     print 'Caching: Dependencies are', deps.keys()
     847    log.critical('Caching: Dependencies are %s' % deps.keys())
    851848
    852849  if not (argsfile and datafile and admfile) or \
     
    890887  if dependencies and not compare(depsref, deps):
    891888    if verbose:
    892       print 'MESSAGE (caching.py): Dependencies', dependencies, \
    893             'have changed - recomputing'
     889      log.critical('Dependencies %s have changed - recomputing' % dependencies)
     890
    894891    # Don't use cached file - recompute
    895892    reason = 2
     
    899896  #
    900897  bytecode = get_bytecode(my_F)
    901 
    902   #print compare(argsref,args),   
    903   #print compare(kwargsref,kwargs),
    904   #print compare(bytecode,coderef)
    905898
    906899  # Check if arguments or bytecode have changed
     
    921914
    922915      if loadtime >= comptime:
    923         print 'WARNING (caching.py): Caching did not yield any gain.'
    924         print '                      Consider executing function ',
    925         print '('+funcname+') without caching.'
     916        log.critical('Caching did not yield any gain.')
     917        log.critical('Consider executing function %s without caching.'
     918                     % funcname)
    926919  else:
    927920
     
    934927                    verbose, compression, dependencies)
    935928
    936     # DEBUGGING
    937     # if not Retrieved:
    938     #   print 'Arguments did not match'
    939     # else:
    940     #   print 'Match found !'
    941    
    942929    # The real reason is that args or bytecodes have changed.
    943930    # Not that the recursive seach has found an unused filename
     
    983970    funcname = get_funcname(my_F)
    984971    if verbose:
    985       print 'MESSAGE (caching.py): Clearing', CD+funcname+'*'
     972      log.critical('Clearing %s' % CD+funcname+'*')
    986973
    987974    file_names = os.listdir(CD)
     
    999986    if len(file_names) > 0:
    1000987      if verbose:
    1001         print 'MESSAGE (caching.py): Remove the following files:'
     988        log.critical('Remove the following files:')
    1002989        for file_name in file_names:
    1003             print file_name
     990            log.critical('     ' + file_name)
    1004991
    1005992        A = raw_input('Delete (Y/N)[N] ?')
     
    10411028    delfiles = numfiles-maxfiles+block
    10421029    if verbose:
    1043       print 'Deleting '+`delfiles`+' expired files:'
     1030      log.critical('Deleting %d expired files:' % delfiles)
    10441031      os.system('ls -lur '+CD+'* | head -' + `delfiles`)            # List them
    10451032    os.system('ls -ur '+CD+'* | head -' + `delfiles` + ' | xargs /bin/rm')
     
    10971084  if not datafile:
    10981085    if verbose:
    1099       print 'ERROR (caching): Could not open %s' %datafile.name
     1086        log.critical('ERROR: Could not open %s' % datafile.name)
    11001087    raise IOError
    11011088
    11021089  if not admfile:
    11031090    if verbose:
    1104       print 'ERROR (caching): Could not open %s' %admfile.name
     1091        log.critical('ERROR: Could not open %s' % admfile.name)
    11051092    raise IOError
    11061093
     
    12461233        #  Rs  = zlib.decompress(RsC)
    12471234        #  zlib.error: Error -5 while decompressing data
    1248         #print 'ERROR (caching): Could not decompress ', file.name
    12491235        #raise Exception
    12501236        reason = 6  # Unreadable file
     
    12661252    import sys
    12671253    if options['verbose']:
    1268       print 'ERROR (caching): Out of memory while loading %s, aborting' \
    1269             %(file.name)
     1254      log.critical('ERROR: Out of memory while loading %s, aborting'
     1255                   % file.name)
    12701256
    12711257    # Raise the error again for now
     
    12911277      import zlib
    12921278    except:
    1293       print
    1294       print '*** Could not find zlib ***'
    1295       print '*** Try to run caching with compression off ***'
    1296       print "*** caching.set_option('compression', 0) ***"
     1279      log.critical()
     1280      log.critical('*** Could not find zlib ***')
     1281      log.critical('*** Try to run caching with compression off ***')
     1282      log.critical("*** caching.set_option('compression', 0) ***")
    12971283      raise Exception
    12981284     
     
    14401426    if (iA, iB) in ids:
    14411427        # A and B have been compared already
    1442         #print 'Found', (iA, iB), A, B
    14431428        return ids[(iA, iB)]
    14441429    else:
     
    16181603  import types
    16191604
    1620   #print 'Caching DEBUG: Dependencies are', dependencies
    16211605  d = {}
    16221606  if dependencies:
     
    16791663    # Hack to get the results anyway (works only on Unix at the moment)
    16801664    #
    1681     print 'Hack to get os.stat when files are too large'
     1665    log.critical('Hack to get os.stat when files are too large')
    16821666
    16831667    if unix:
     
    17751759      else:
    17761760        pass  # FIXME: What about acces rights under Windows?
    1777       if verbose: print 'MESSAGE: Directory', CD, 'created.'
     1761      if verbose: log.critical('MESSAGE: Directory %s created.' % CD)
    17781762    except:
    17791763      if warn is True:
    1780         print 'WARNING: Directory', CD, 'could not be created.'
     1764        log.critical('WARNING: Directory %s could not be created.' % CD)
    17811765      if unix:
    17821766        CD = '/tmp/'
     
    17841768        CD = 'C:' 
    17851769      if warn is True:
    1786         print 'Using directory %s instead' %CD
     1770        log.critical('Using directory %s instead' % CD)
    17871771
    17881772  return(CD)
     
    18231807    #    pass
    18241808  except:
    1825     print 'Warning: Stat file could not be opened'
     1809    log.critical('Warning: Stat file could not be opened')
    18261810
    18271811  try:
     
    18661850    statfile.close()
    18671851  except:
    1868     print 'Warning: Writing of stat file failed'
     1852    log.critical('Warning: Writing of stat file failed')
    18691853
    18701854# -----------------------------------------------------------------------------
     
    19331917  for FN in SF:
    19341918    input = open(SD+FN,'r')
    1935     print 'Reading file ', SD+FN
     1919    log.critical('Reading file %s' % SD+FN)
    19361920
    19371921    while True:
     
    19411925      for record in A:
    19421926        record = tuple(split(rstrip(record),','))
    1943         #print record, len(record)
    19441927
    19451928        if len(record) == 9:
     
    19731956            compression = 0
    19741957          else:
    1975             print 'Unknown value of compression', record[4]
    1976             print record
     1958            log.critical('Unknown value of compression %s' % str(record[4]))
     1959            log.critical(str(record))
    19771960            total_discarded = total_discarded + 1           
    19781961            continue
     
    20021985             
    20031986        else:
    2004           #print 'Record discarded'
    2005           #print record
    20061987          total_discarded = total_discarded + 1
    20071988
     
    20131994  if total_read == 0:
    20141995    printline(Widths,'=')
    2015     print 'CACHING STATISTICS: No valid records read'
     1996    log.critical('CACHING STATISTICS: No valid records read')
    20161997    printline(Widths,'=')
    20171998    return
    20181999
    2019   print
     2000  log.critical()
    20202001  printline(Widths,'=')
    2021   print 'CACHING STATISTICS: '+ctime(firstday)+' to '+ctime(lastday)
     2002  log.critical('CACHING STATISTICS: '+ctime(firstday)+' to '+ctime(lastday))
    20222003  printline(Widths,'=')
    2023   #print '  Period:', ctime(firstday), 'to', ctime(lastday)
    2024   print '  Total number of valid records', total_read
    2025   print '  Total number of discarded records', total_discarded
    2026   print '  Total number of hits', total_hits
    2027   print
    2028 
    2029   print '  Fields', Fields[2:], 'are averaged over number of hits'
    2030   print '  Time is measured in seconds and size in bytes'
    2031   print '  Tables are sorted by', Fields[1:][sortidx]
    2032 
    2033   # printline(Widths,'-')
     2004  log.critical('  Total number of valid records %d' % total_read)
     2005  log.critical('  Total number of discarded records %d' % total_discarded)
     2006  log.critical('  Total number of hits %d' % total_hits)
     2007  log.critical()
     2008
     2009  log.critical('  Fields %s are averaged over number of hits' % Fields[2:])
     2010  log.critical('  Time is measured in seconds and size in bytes')
     2011  log.critical('  Tables are sorted by %s' % Fields[1:][sortidx])
    20342012
    20352013  if showuser:
     
    20532031    # Write Header
    20542032    #
    2055     print
    2056     #print Dictnames[i], 'statistics:'; i=i+1
     2033    log.critical()
    20572034    printline(Widths,'-')
    20582035    n = 0
     
    20602037      if s == Fields[0]:  # Left justify
    20612038        s = Dictnames[i] + ' ' + s; i=i+1
    2062         exec "print '%-" + str(Widths[n]) + "s'%s,"; n=n+1
     2039        #exec "print '%-" + str(Widths[n]) + "s'%s,"; n=n+1
     2040        log.critical('%-*s' % (Widths[n], s))
     2041        n += 1
    20632042      else:
    2064         exec "print '%" + str(Widths[n]) + "s'%s,"; n=n+1
    2065     print
     2043        #exec "print '%" + str(Widths[n]) + "s'%s,"; n=n+1
     2044        log.critical('%*s' % (Widths[n], s))
     2045        n += 1
     2046    log.critical()
    20662047    printline(Widths,'-')
    20672048
     
    20722053      n = 0
    20732054      if len(key) > Widths[n]: key = key[:Widths[n]-3] + '...'
    2074       exec "print '%-" + str(Widths[n]) + Types[n]+"'%key,";n=n+1
     2055      #exec "print '%-" + str(Widths[n]) + Types[n]+"'%key,";n=n+1
     2056      log.critical('%-*s' % (Widths[n], str(key)))
     2057      n += 1
    20752058      for val in rec:
    2076         exec "print '%" + str(Widths[n]) + Types[n]+"'%val,"; n=n+1
    2077       print
    2078     print
     2059        #exec "print '%" + str(Widths[n]) + Types[n]+"'%val,"; n=n+1
     2060        log.critical('%*s' % (Widths[n], str(key)))
     2061        n += 1
     2062      log.critical()
     2063    log.critical()
    20792064
    20802065#==============================================================================
     
    21212106
    21222107    if sortidx > len(rec)-1:
    2123       if options['verbose']:
    2124         print 'ERROR: Sorting index to large, sortidx = ', sortidx
    2125       raise IndexError
     2108      msg = 'ERROR: Sorting index too large, sortidx = %s' % str(sortidx)
     2109      raise IndexError, msg
    21262110
    21272111    val = rec[sortidx]
     
    21492133      s = s+char
    21502134
    2151   print s
     2135  log.critical(s)
    21522136
    21532137#==============================================================================
     
    21632147
    21642148  import string
    2165   #print 'MESSAGE (caching.py): Evaluating function', funcname,
    21662149
    21672150  print_header_box('Evaluating function %s' %funcname)
     
    21722155  print_footer()
    21732156 
    2174   #
    2175   # Old message
    2176   #
    2177   #args_present = 0
    2178   #if args:
    2179   #  if len(args) == 1:
    2180   #    print 'with argument', mkargstr(args[0], textwidth2),
    2181   #  else:
    2182   #    print 'with arguments', mkargstr(args, textwidth2),
    2183   #  args_present = 1     
    2184   #   
    2185   #if kwargs:
    2186   #  if args_present:
    2187   #    word = 'and'
    2188   #  else:
    2189   #    word = 'with'
    2190   #     
    2191   #  if len(kwargs) == 1:
    2192   #    print word + ' keyword argument', mkargstr(kwargs, textwidth2)
    2193   #  else:
    2194   #    print word + ' keyword arguments', mkargstr(kwargs, textwidth2)
    2195   #  args_present = 1           
    2196   #else:
    2197   #  print    # Newline when no keyword args present
    2198   #       
    2199   #if not args_present:   
    2200   #  print '',  # Default if no args or kwargs present
    2201    
    2202    
    2203 
    22042157# -----------------------------------------------------------------------------
    22052158
     
    22242177  msg8(reason)
    22252178
    2226   print string.ljust('| CPU time:', textwidth1) + str(round(comptime,2)) + ' seconds'
     2179  log.critical(string.ljust('| CPU time:', textwidth1) +
     2180               str(round(comptime,2)) + ' seconds')
    22272181
    22282182# -----------------------------------------------------------------------------
     
    22362190
    22372191  import string
    2238   print string.ljust('| Loading time:', textwidth1) + str(round(savetime,2)) + \
    2239                      ' seconds (estimated)'
     2192  log.critical(string.ljust('| Loading time:', textwidth1) +
     2193               str(round(savetime,2)) + ' seconds (estimated)')
    22402194  msg5(CD,FN,deps,compression)
    22412195
     
    22542208 
    22552209  msg6(funcname,args,kwargs)
    2256   print string.ljust('| CPU time:', textwidth1) + str(round(comptime,2)) + ' seconds'
    2257   print string.ljust('| Loading time:', textwidth1) + str(round(loadtime,2)) + ' seconds'
    2258   print string.ljust('| Time saved:', textwidth1) + str(round(comptime-loadtime,2)) + \
    2259         ' seconds'
     2210  log.critical(string.ljust('| CPU time:', textwidth1) +
     2211               str(round(comptime,2)) + ' seconds')
     2212  log.critical(string.ljust('| Loading time:', textwidth1) +
     2213               str(round(loadtime,2)) + ' seconds')
     2214  log.critical(string.ljust('| Time saved:', textwidth1) +
     2215               str(round(comptime-loadtime,2)) + ' seconds')
    22602216  msg5(CD,FN,deps,compression)
    22612217
     
    22742230  import os, time, string
    22752231
    2276   print '|'
    2277   print string.ljust('| Caching dir: ', textwidth1) + CD
     2232  log.critical('|')
     2233  log.critical(string.ljust('| Caching dir: ', textwidth1) + CD)
    22782234
    22792235  if compression:
     
    22862242  for file_type in file_types:
    22872243    file_name = FN + '_' + file_type + suffix
    2288     print string.ljust('| ' + file_type + ' file: ', textwidth1) + file_name,
    22892244    stats = os.stat(CD+file_name)
    2290     print '('+ str(stats[6]) + ' ' + bytetext + ')'
    2291 
    2292   print '|'
     2245    log.critical(string.ljust('| ' + file_type + ' file: ', textwidth1) +
     2246                 file_name + '('+ str(stats[6]) + ' ' + bytetext + ')')
     2247
     2248  log.critical('|')
    22932249  if len(deps) > 0:
    2294     print '| Dependencies:  '
     2250    log.critical('| Dependencies:  ')
    22952251    dependencies  = deps.keys()
    22962252    dlist = []; maxd = 0
     
    23172273      s = string.rjust(slist[n], maxs)
    23182274
    2319       print '| ', d, t, ' ', s, 'bytes'
     2275      log.critical('| %s %s %s bytes' % (d, t, s))
    23202276  else:
    2321     print '| No dependencies'
     2277    log.critical('| No dependencies')
    23222278  print_footer()
    23232279
     
    23322288
    23332289  import string
    2334   print string.ljust('| Function:', textwidth1) + funcname
     2290  log.critical(string.ljust('| Function:', textwidth1) + funcname)
    23352291
    23362292  msg7(args, kwargs)
     
    23502306  if args:
    23512307    if len(args) == 1:
    2352       print string.ljust('| Argument:', textwidth1) + mkargstr(args[0], \
    2353                          textwidth2)
     2308      log.critical(string.ljust('| Argument:', textwidth1) +
     2309                   mkargstr(args[0], textwidth2))
    23542310    else:
    2355       print string.ljust('| Arguments:', textwidth1) + \
    2356             mkargstr(args, textwidth2)
     2311      log.critical(string.ljust('| Arguments:', textwidth1) +
     2312                   mkargstr(args, textwidth2))
    23572313    args_present = 1
    23582314           
    23592315  if kwargs:
    23602316    if len(kwargs) == 1:
    2361       print string.ljust('| Keyword Arg:', textwidth1) + mkargstr(kwargs, \
    2362                          textwidth2)
     2317      log.critical(string.ljust('| Keyword Arg:', textwidth1) +
     2318                   mkargstr(kwargs, textwidth2))
    23632319    else:
    2364       print string.ljust('| Keyword Args:', textwidth1) + \
    2365             mkargstr(kwargs, textwidth2)
     2320      log.critical(string.ljust('| Keyword Args:', textwidth1) +
     2321                   mkargstr(kwargs, textwidth2))
    23662322    args_present = 1
    23672323
    23682324  if not args_present:               
    2369     print '| No arguments' # Default if no args or kwargs present
     2325    log.critical('| No arguments')    # Default if no args or kwargs present
    23702326
    23712327# -----------------------------------------------------------------------------
     
    23852341    R = 'Unknown' 
    23862342 
    2387   print string.ljust('| Reason:', textwidth1) + R
     2343  log.critical(string.ljust('| Reason:', textwidth1) + R)
    23882344   
    23892345# -----------------------------------------------------------------------------
     
    24062362  s = '+' + '-'*N + CR
    24072363
    2408   print s + '| ' + line + CR + s,
     2364  log.critical(s + '| ' + line + CR + s)
    24092365
    24102366  textwidth3 = N
     
    24192375  s = '+' + '-'*N + CR   
    24202376     
    2421   print s     
     2377  log.critical(s)
    24222378     
    24232379# -----------------------------------------------------------------------------
     
    25082464  import string
    25092465   
    2510   print string.ljust(msg, textwidth4) + ' - OK'
     2466  log.critical(string.ljust(msg, textwidth4) + ' - OK' )
    25112467 
    25122468  #raise StandardError
     
    25212477  """
    25222478 
    2523   print 'ERROR (caching.test): %s' %msg
    2524   print 'Please send this code example and output to '
    2525   print 'Ole.Nielsen@anu.edu.au'
    2526   print
    2527   print
     2479  log.critical('ERROR (caching.test): %s' % msg)
     2480  log.critical('Please send this code example and output to ')
     2481  log.critical('Ole.Nielsen@anu.edu.au')
     2482  log.critical()
     2483  log.critical()
    25282484 
    25292485  raise StandardError
  • anuga_core/source/anuga/caching/test_caching.py

    r7309 r7317  
    5050
    5151def retrieve_cache(Dummy, verbose=False):
    52   if verbose: print 'Check that cache is there'
     52  if verbose: print('Check that cache is there')
    5353 
    5454  X = cache(Dummy, args=(9,10), test=1,
     
    386386        f2 = call(5, 7)
    387387
    388         #print myhash(f1)
    389         #print myhash(f2)     
    390 
    391388        # Check that hash value of callable objects don't change
    392389        # FIXME (Ole): The hash values do appear to change when OS
     
    411408        bc2 = get_bytecode(f2)
    412409       
    413         #print 'bc1', bc1
    414         #print 'bc2', bc2
    415        
    416410        msg = 'Byte code should be different'
    417411        assert bc1 != bc2, msg
     
    631625       
    632626        T4 = cache(f, (a,b,c,N), {'x':x, 'y':y}, test=1)
    633         #print 'T4', T4
    634627        assert T4 is None, "Option 'test' when cache absent failed"
    635628
Note: See TracChangeset for help on using the changeset viewer.