From 17d242213dc94b0e4bd8460500d71f6aa533365a Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Wed, 25 Jul 2012 09:40:37 -0400 Subject: [PATCH] * Added some useful functions from my pw2g_bench_MnO.py scriptlet collection: - wpylib.math.roundup() - wpylib.shell_tools.dirname2() * file_exists_nonempty() is moved to wpylib.shell_tools. --- file/file_utils.py | 4 ---- math/__init__.py | 7 +++++++ shell_tools.py | 12 ++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/file/file_utils.py b/file/file_utils.py index 76bceee..5e59235 100644 --- a/file/file_utils.py +++ b/file/file_utils.py @@ -81,10 +81,6 @@ def open_input_file(fname, superize=0): # Miscellaneous functions -def file_exists_nonempty(fn): - return os.path.isfile(fn) and os.stat(fn).st_size > 0 - - def glob_files(filespec): '''Processes a glob string, or does nothing (pass-on only) if an iterable object diff --git a/math/__init__.py b/math/__init__.py index 1da8f54..5150dce 100644 --- a/math/__init__.py +++ b/math/__init__.py @@ -42,3 +42,10 @@ def epsilon(dtype): small2 = small small = dtype(small / 2) return small2 + + +def roundup(value, unit): + """Rounds up a value to the next integer multiple of a unit.""" + return numpy.ceil(float(value) / float(unit)) * unit + + diff --git a/shell_tools.py b/shell_tools.py index 476f3c3..59253f3 100644 --- a/shell_tools.py +++ b/shell_tools.py @@ -26,6 +26,18 @@ def mcd(subdir): mkdir("-p", subdir) os.chdir(subdir) +def dirname2(path): + """Returns the directory part of a path. + The difference from os.path.dirname is that if the directory + part is empty, it is converted to '.' (the current directory).""" + d = os.path.dirname(path) + if d == '': d = '.' + return d + +def file_exists_nonempty(path): + """Determines whether a given path is a regular file of + nonzero size.""" + return os.path.isfile(path) and os.stat(path).st_size > 0 def provide_link(dest, src): """Checks if file `dest' exists. If it does not, provide for it by means