changeset 363:9e84e8a20a75

Added to misc.file
author Joseph Turian <turian@gmail.com>
date Thu, 03 Jul 2008 17:52:11 -0400
parents 0e3af3c53ac7
children 417355f15282
files common/__init__.py common/file.py common/mytime.py common/time.py
diffstat 4 files changed, 40 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/common/__init__.py	Wed Jul 02 16:10:00 2008 -0400
+++ b/common/__init__.py	Thu Jul 03 17:52:11 2008 -0400
@@ -1,5 +1,7 @@
 import file
 import floateq
+#import gzipstring
 import memory
 import misc
-import time
+import mytime
+import str
--- a/common/file.py	Wed Jul 02 16:10:00 2008 -0400
+++ b/common/file.py	Thu Jul 03 17:52:11 2008 -0400
@@ -10,3 +10,28 @@
         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.
+    """
+    files = []
+    for root, dirs, files in os.walk(dir):
+        #sys.stderr.write("Walking %s...\n" % root)
+        for f in files:
+            files.append(os.path.join(root, f))
+    if shuffle:
+        import random
+        random.shuffle(files)
+    return files
+
+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)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/mytime.py	Thu Jul 03 17:52:11 2008 -0400
@@ -0,0 +1,12 @@
+
+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
+
--- a/common/time.py	Wed Jul 02 16:10:00 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
-