[Yt-svn] yt-commit r1461 - trunk/yt

mturk at wrangler.dreamhost.com mturk at wrangler.dreamhost.com
Sun Sep 27 13:25:53 PDT 2009


Author: mturk
Date: Sun Sep 27 13:25:51 2009
New Revision: 1461
URL: http://yt.enzotools.org/changeset/1461

Log:
Reorganized and commented the funcs.py module.



Modified:
   trunk/yt/funcs.py

Modified: trunk/yt/funcs.py
==============================================================================
--- trunk/yt/funcs.py	(original)
+++ trunk/yt/funcs.py	Sun Sep 27 13:25:51 2009
@@ -29,40 +29,11 @@
 from math import floor, ceil
 from yt.logger import ytLogger as mylog
 
-def signal_print_traceback(signo, frame):
-    print traceback.print_stack(frame)
-
-def signal_problem(signo, frame):
-    raise RuntimeError()
-
-try:
-    signal.signal(signal.SIGUSR1, signal_print_traceback)
-    mylog.debug("SIGUSR1 registered for traceback printing")
-    signal.signal(signal.SIGUSR2, signal_problem)
-    mylog.debug("SIGUSR2 registered for RuntimeError")
-except ValueError:  # Not in main thread
-    pass
-
-def paste_traceback(exc_type, exc, tb):
-    sys.__excepthook__(exc_type, exc, tb)
-    import xmlrpclib, cStringIO
-    p = xmlrpclib.ServerProxy(
-            "http://paste.enzotools.org/xmlrpc/",
-            allow_none=True)
-    s = cStringIO.StringIO()
-    traceback.print_exception(exc_type, exc, tb, file=s)
-    s = s.getvalue()
-    ret = p.pastes.newPaste('pytb', s, None, '', '', True)
-    print
-    print "Traceback pasted to http://paste.enzotools.org/show/%s" % (ret)
-    print
-
-if "--paste" in sys.argv:
-    sys.excepthook = paste_traceback
-if "--rpdb" in sys.argv:
-    sys.excepthook = rpdb.rpdb_excepthook
-    del sys.argv[sys.argv.index("--rpdb")]
+# Some compatibility functions.  In the long run, these *should* disappear as
+# we move toward newer python versions.  Most were implemented to get things
+# running on DataStar.
 
+# If we're running on python2.4, we need a 'wraps' function
 def blank_wrapper(f):
     return lambda a: a
 
@@ -71,6 +42,24 @@
 except ImportError:
     wraps = blank_wrapper
 
+# We need to ensure that we have a defaultdict implementation
+
+class __defaultdict(dict):
+    def __init__(self, func):
+        self.__func = func
+        dict.__init__(self)
+    def __getitem__(self, key):
+        if not self.has_key(key):
+            self.__setitem__(key, self.__func())
+        return dict.__getitem__(self, key)
+
+try:
+    from collections import defaultdict
+except ImportError:
+    defaultdict = __defaultdict
+
+# Some functions for handling sequences and other types
+
 def iterable(obj):
     """
     Grabbed from Python Cookbook / matploblib.cbook
@@ -80,16 +69,45 @@
     return True
 
 def ensure_list(obj):
+    # This makes sure that we have a list of items
     if obj == None:
         return [obj]
     if not isinstance(obj, types.ListType):
         return [obj]
     return obj
 
+def just_one(obj):
+    # If we have an iterable, sometimes we only want one item
+    if hasattr(obj,'flat'):
+        return obj.flat[0]
+    elif iterable(obj):
+        return obj[0]
+    return obj
+
+# Taken from
+# http://www.goldb.org/goldblog/2008/02/06/PythonConvertSecsIntoHumanReadableTimeStringHHMMSS.aspx
+def humanize_time(secs):
+    """
+    Takes *secs* and returns a nicely formatted string
+    """
+    mins, secs = divmod(secs, 60)
+    hours, mins = divmod(mins, 60)
+    return '%02d:%02d:%02d' % (hours, mins, secs)
+
+#
+# Some function wrappers that come in handy once in a while
+#
+
 def time_execution(func):
     """
     Decorator for seeing how long a given function takes, depending on whether
     or not the global 'yt.timefunctions' config parameter is set.
+
+    This can be used like so:
+
+    @time_execution
+    def some_longrunning_function(...):
+
     """
     @wraps(func)
     def wrapper(*arg, **kw):
@@ -104,6 +122,29 @@
     else:
         return func
 
+def print_tb(func):
+    @wraps(func)
+    def run_func(*args, **kwargs):
+        traceback.print_stack()
+        return func(*args, **kwargs)
+    return run_func
+
+def rootonly(func):
+    @wraps(func)
+    def donothing(*args, **kwargs):
+        return
+    from yt.config import ytcfg
+    if ytcfg.getint("yt","__parallel_rank") > 0: return donothing
+    return func
+
+def deprecate(func):
+    @wraps(func)
+    def run_func(*args, **kwargs):
+        warnings.warn("%s has been deprecated and may be removed without notice!" \
+                % func.func_name, DeprecationWarning, stacklevel=2)
+        func(*args, **kwargs)
+    return run_func
+
 def pdb_run(func):
     @wraps(func)
     def wrapper(*args, **kw):
@@ -121,6 +162,13 @@
 """
 
 def insert_ipython(num_up=1):
+    """
+    Placed inside a function, this will insert an IPython interpreter at that
+    current location.  This will enabled detailed inspection of the current
+    exeuction environment, as well as (optional) modification of that environment.
+    *num_up* refers to how many frames of the stack get stripped off, and
+    defaults to 1 so that this function itself is stripped off.
+    """
     from IPython.Shell import IPShellEmbed
     stack = inspect.stack()
     frame = inspect.stack()[num_up]
@@ -133,7 +181,14 @@
             local_ns = loc, global_ns = glo)
     del ipshell
 
+
+#
+# Our progress bar types and how to get one
+#
+
 class DummyProgressBar(object):
+    # This progressbar gets handed if we don't
+    # want ANY output
     def __init__(self, *args, **kwargs):
         return
     def update(self, *args, **kwargs):
@@ -142,6 +197,8 @@
         return
 
 class ParallelProgressBar(object):
+    # This is just a simple progress bar
+    # that prints on start/stop
     def __init__(self, title, maxval):
         self.title = title
         mylog.info("Starting '%s'", title)
@@ -167,13 +224,6 @@
     def finish(self):
         self._pbar.Destroy()
 
-def just_one(obj):
-    if hasattr(obj,'flat'):
-        return obj.flat[0]
-    elif iterable(obj):
-        return obj[0]
-    return obj
-
 def get_pbar(title, maxval):
     from yt.config import ytcfg
     if ytcfg.getboolean("yt","inGui"):
@@ -199,54 +249,62 @@
                           maxval=maxval).start()
     return pbar
 
-# Taken from
-# http://www.goldb.org/goldblog/2008/02/06/PythonConvertSecsIntoHumanReadableTimeStringHHMMSS.aspx
-def humanize_time(secs):
-    mins, secs = divmod(secs, 60)
-    hours, mins = divmod(mins, 60)
-    return '%02d:%02d:%02d' % (hours, mins, secs)
-
-class __defaultdict(dict):
-    def __init__(self, func):
-        self.__func = func
-        dict.__init__(self)
-    def __getitem__(self, key):
-        if not self.has_key(key):
-            self.__setitem__(key, self.__func())
-        return dict.__getitem__(self, key)
-
-import traceback
-def print_tb(func):
-    @wraps(func)
-    def run_func(*args, **kwargs):
-        traceback.print_stack()
-        return func(*args, **kwargs)
-    return run_func
-
-try:
-    from collections import defaultdict
-except ImportError:
-    defaultdict = __defaultdict
-
-def rootonly(func):
-    @wraps(func)
-    def donothing(*args, **kwargs):
-        return
-    from yt.config import ytcfg
-    if ytcfg.getint("yt","__parallel_rank") > 0: return donothing
-    return func
-
 def only_on_root(func, *args, **kwargs):
+    """
+    This function accepts a *func*, a set of *args* and *kwargs* and then only
+    on the root processor calls the function.  All other processors get "None"
+    handed back.
+    """
     from yt.config import ytcfg
     if not ytcfg.getboolean("yt","__parallel"):
         return func(*args,**kwargs)
     if ytcfg.getint("yt","__parallel_rank") > 0: return
     return func(*args, **kwargs)
 
-def deprecate(func):
-    @wraps(func)
-    def run_func(*args, **kwargs):
-        warnings.warn("%s has been deprecated and may be removed without notice!" \
-                % func.func_name, DeprecationWarning, stacklevel=2)
-        func(*args, **kwargs)
-    return run_func
+#
+# Our signal and traceback handling functions
+#
+
+def signal_print_traceback(signo, frame):
+    print traceback.print_stack(frame)
+
+def signal_problem(signo, frame):
+    raise RuntimeError()
+
+# We use two signals, SIGUSR1 and SIGUSR2.  In a non-threaded environment,
+# we set up handlers to process these by printing the current stack and to
+# raise a RuntimeError.  The latter can be used, inside pdb, to catch an error
+# and then examine the current stack.
+try:
+    signal.signal(signal.SIGUSR1, signal_print_traceback)
+    mylog.debug("SIGUSR1 registered for traceback printing")
+    signal.signal(signal.SIGUSR2, signal_problem)
+    mylog.debug("SIGUSR2 registered for RuntimeError")
+except ValueError:  # Not in main thread
+    pass
+
+# This is a traceback handler that knows how to paste to the pastebin.
+def paste_traceback(exc_type, exc, tb):
+    sys.__excepthook__(exc_type, exc, tb)
+    import xmlrpclib, cStringIO
+    p = xmlrpclib.ServerProxy(
+            "http://paste.enzotools.org/xmlrpc/",
+            allow_none=True)
+    s = cStringIO.StringIO()
+    traceback.print_exception(exc_type, exc, tb, file=s)
+    s = s.getvalue()
+    ret = p.pastes.newPaste('pytb', s, None, '', '', True)
+    print
+    print "Traceback pasted to http://paste.enzotools.org/show/%s" % (ret)
+    print
+
+# If we recognize one of the arguments on the command line as indicating a
+# different mechanism for handling tracebacks, we attach one of those handlers
+# and remove the argument from sys.argv.
+if "--paste" in sys.argv:
+    sys.excepthook = paste_traceback
+    del sys.argv[sys.argv.index("--paste")]
+if "--rpdb" in sys.argv:
+    sys.excepthook = rpdb.rpdb_excepthook
+    del sys.argv[sys.argv.index("--rpdb")]
+



More information about the yt-svn mailing list