""" $URL: svn+ssh://svn.mems-exchange.org/repos/trunk/qp/lib/util.py $ $Id: util.py 28957 2006-10-23 13:42:43Z dbinger $ """ from binascii import hexlify from commands import getoutput from datetime import datetime import os import sys def import_object(name): """ Import and return the object with the given name. """ i = name.rfind('.') if i != -1: module_name = name[:i] object_name = name[i+1:] __import__(module_name) try: return getattr(sys.modules[module_name], object_name) except AttributeError: pass __import__(name) return sys.modules[name] # Define randbytes(), one way or another. if hasattr(os, 'urandom'): # available in Python 2.4 and also works on win32 def randbytes(bytes): """Return bits of random data as a hex string.""" return hexlify(os.urandom(bytes)) elif os.path.exists('/dev/urandom'): # /dev/urandom is just as good as /dev/random for cookies (assuming # SHA-1 is secure) and it never blocks. def randbytes(bytes): """Return bits of random data as a hex string.""" return hexlify(open("/dev/urandom").read(bytes)) else: # this is much less secure. import sha, time class _PRNG: def __init__(self): self.state = sha.new(str(time.time() + time.clock())) self.count = 0 def _get_bytes(self): self.state.update('%s %d' % (time.time() + time.clock(), self.count)) self.count += 1 return self.state.hexdigest() def randbytes(self, bytes): """Return bits of random data as a hex string.""" s = "" chars = 2*bytes while len(s) < chars: s += self._get_bytes() return s[:chars] randbytes = _PRNG().randbytes def trace(f): def trace(*args, **kwargs): start = datetime.utcnow() name = f.func_name s = '%s(' % name for arg in args: s += "%r, " % arg for k, v in kwargs.items(): s += "%s=%r, " % (k, v) if args or kwargs: s = s[:-2] s += ')' print print s f(*args, **kwargs) duration = datetime.utcnow() - start print '%s finished in %s' % (s, duration) return trace def get_memory_usage(): output = getoutput("ps -p %s -orsz,vsz" % os.getpid()) rsz, vsz = output.split()[-2:] return int(rsz), int(vsz)