# HG changeset patch # User Joseph Turian # Date 1215143739 14400 # Node ID 54384b1eb2b93a42dd745c44f286f1d4e4a24680 # Parent 3d5a483fe73247c8d79ce96ce7368a4fb938dc95# Parent a04ce1e6ea540067176e99206818e340eda01d4c merge diff -r a04ce1e6ea54 -r 54384b1eb2b9 common/__init__.py --- a/common/__init__.py Thu Jul 03 18:48:32 2008 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -import file -import floateq -#import gzipstring -import memory -import misc -import mytime -import str diff -r a04ce1e6ea54 -r 54384b1eb2b9 common/file.py --- a/common/file.py Thu Jul 03 18:48:32 2008 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,39 +0,0 @@ -import gzip, bz2 -import os, os.path, sys - -def myopen(filename, mode="r", bufsize=-1): - """ - open(), detecting .gz and .bz2 file suffixes - """ - if filename[-3:] == ".gz": - return gzip.open(filename, mode, bufsize) - elif filename[-4:] == ".bz2": - return bz2.open(filename, mode, bufsize) - else: - return open(filename, mode, bufsize) - -def find_files(dir, shuffle=False): - """ - Find all files in dir by recursively directory walking. - @param shuffle: Randomly shuffle the files before returning them. - """ - all = [] - assert os.path.isdir(dir) - for root, dirs, files in os.walk(dir): - #sys.stderr.write("Walking %s...\n" % root) - for f in files: - all.append(os.path.join(root, f)) - if shuffle: - import random - random.shuffle(all) - return all - -def ensuredir(dir): - """ - Create dir if it does not exist (including all parents). - Do nothing if it does. - """ - if not os.path.exists(dir): - sys.stderr.write("Creating directory: %s\n" % dir) - os.makedirs(dir) - assert os.path.isdir(dir) diff -r a04ce1e6ea54 -r 54384b1eb2b9 common/floateq.py --- a/common/floateq.py Thu Jul 03 18:48:32 2008 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -# -# Determine if floating point numbers are very close -########### - -import math - -DEFAULT_SANITY_CHECK_EPSILON = 1e-6 - -def floateq(a, b, epsilon=DEFAULT_SANITY_CHECK_EPSILON): - """ - Compare two floats, with some epsilon tolerance. - """ - return absolute_relative_error(a, b) < epsilon - -def absolute_relative_error(a, b, epsilon=DEFAULT_SANITY_CHECK_EPSILON): - return abs(a - b) / (abs(a) + abs(b) + epsilon) - -def double_epsilon_multiplicative_eq(a, b, epsilon=DEFAULT_SANITY_CHECK_EPSILON): - """ - Determine if doubles are equal to within a multiplicative factor of - L{epsilon}. - @note: This function should be preferred over - L{double_epsilon_additive_eq}, unless the values to be compared may - have differing signs. - @precondition: sign(a) == sign(b) - @rtype: bool - """ - if a == b: return True - if a == 0 and b == 0: return True - assert a != 0 - assert b != 0 - assert sign(a) == sign(b) - if a > b: d = a / b - else: d = b / a - assert d >= 1 - return True if d <= 1 + SANITY_CHECK_EPSILON else False - -def double_epsilon_additive_eq(a, b): - """ - Determine if doubles are equal to within an additive factor of - L{SANITY_CHECK_EPSILON}. - @note: Prefer L{double_epsilon_multiplicative_eq} to this function - unless the values to be compared may have differing signs. - """ - if a == b: return True - if a == 0 and b == 0: return True - assert sign(a) != sign(b) # Should use SANITY_CHECK_EPSILON - d = math.fabs(a - b) - return d <= SANITY_CHECK_EPSILON diff -r a04ce1e6ea54 -r 54384b1eb2b9 common/memory.py --- a/common/memory.py Thu Jul 03 18:48:32 2008 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -""" -Determine memory usage of a program:: - m0 = memory() - ... - m1 = memory(m0) -@note: From U{http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286222/index_txt} -@warning: Not portable. -""" - -import os - -_proc_status = '/proc/%d/status' % os.getpid() - -_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0, - 'KB': 1024.0, 'MB': 1024.0*1024.0} - -def _VmB(VmKey): - '''Private. - ''' - global _proc_status, _scale - # get pseudo file /proc//status - try: - t = open(_proc_status) - v = t.read() - t.close() - except: - return 0.0 # non-Linux? - # get VmKey line e.g. 'VmRSS: 9999 kB\n ...' - i = v.index(VmKey) - v = v[i:].split(None, 3) # whitespace - if len(v) < 3: - return 0.0 # invalid format? - # convert Vm value to bytes - return float(v[1]) * _scale[v[2]] - - -def memory(since=0.0): - '''Return memory usage in bytes. - ''' - return _VmB('VmSize:') - since - - -def resident(since=0.0): - '''Return resident memory usage in bytes. - ''' - return _VmB('VmRSS:') - since - - -def stacksize(since=0.0): - '''Return stack size in bytes. - ''' - return _VmB('VmStk:') - since diff -r a04ce1e6ea54 -r 54384b1eb2b9 common/misc.py --- a/common/misc.py Thu Jul 03 18:48:32 2008 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -def runcmd(args, input=None): - """ - Split args into a list, run this command, and return its output. - Raise RuntimeError if the command does not return 0. - @note: This function will not work if args contains pipes | - @param input: If this exists, it will be fed as stdin - """ - import subprocess -# print args - import string - if input == None: stdin = None - else: stdin = subprocess.PIPE - proc = subprocess.Popen(string.split(args), stdout=subprocess.PIPE, stdin=stdin) -# proc = subprocess.Popen(string.split(args), stdout=subprocess.PIPE) - output = proc.communicate(input=input)[0] - if proc.returncode != 0: - import exceptions - raise exceptions.RuntimeError - return output - -def homedir(): - import os - return os.environ["HOME"] -def utilsdir(): - import os - return os.environ["UTILS"] - -def sign(i, assertions=True): - """ - + or - 1 - @precondition: i != 0 - """ - if assertions: - assert i != 0 - else: - if i == 0: return 0 - - return +1 if i > 0 else -1 - -def unique_elements_list_intersection(list1,list2): - """ - Return the unique elements that are in both list1 and list2 - (repeated elements in listi will not be duplicated in the result). - This should run in O(n1+n2) where n1=|list1|, n2=|list2|. - """ - return list(set.intersection(set(list1),set(list2))) diff -r a04ce1e6ea54 -r 54384b1eb2b9 common/mytime.py --- a/common/mytime.py Thu Jul 03 18:48:32 2008 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ - -import time -#http://www.daniweb.com/code/snippet368.html -def print_timing(func): - def wrapper(*arg): - t1 = time.time() - res = func(*arg) - t2 = time.time() - print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0) - return res - return wrapper - diff -r a04ce1e6ea54 -r 54384b1eb2b9 common/str.py --- a/common/str.py Thu Jul 03 18:48:32 2008 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -def percent(a, b): - """ - Return percentage string of a and b, e.g.: - "1 of 10 (10%)" - """ - assert a <= b - assert a >= 0 - assert b > 0 - return "%s of %s (%.2f%%)" % (a, b, 100.*a/b)