[mlpack-svn] r15243 - in mlpack/conf/jenkins-conf: . benchmark benchmark/util

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Jun 17 11:59:37 EDT 2013


Author: marcus
Date: 2013-06-17 11:59:36 -0400 (Mon, 17 Jun 2013)
New Revision: 15243

Added:
   mlpack/conf/jenkins-conf/benchmark/
   mlpack/conf/jenkins-conf/benchmark/util/
   mlpack/conf/jenkins-conf/benchmark/util/__init__.py
   mlpack/conf/jenkins-conf/benchmark/util/loader.py
Log:
Add class to load modules and scripts

Added: mlpack/conf/jenkins-conf/benchmark/util/__init__.py
===================================================================
Added: mlpack/conf/jenkins-conf/benchmark/util/loader.py
===================================================================
--- mlpack/conf/jenkins-conf/benchmark/util/loader.py	                        (rev 0)
+++ mlpack/conf/jenkins-conf/benchmark/util/loader.py	2013-06-17 15:59:36 UTC (rev 15243)
@@ -0,0 +1,49 @@
+'''
+  @file loader.py
+  @author Marcus Edel
+
+  Class to load modules and scripts.
+'''
+
+import imp
+import os
+
+
+class Loader(object):
+
+  # Import a module from a path.
+  @staticmethod
+  def ImportModuleFromPath(path):
+
+    if hasattr(os, 'getcwdu'):
+      # Returns a Unicode object represantation.
+      realPath = os.path.realpath(os.getcwdu())
+    else:
+      realPath = os.path.realpath(os.path.curdir)
+      
+    destinationPath = os.path.dirname(path)
+
+    if destinationPath == '':
+      destinationPath = '.'
+      
+    # Remove the .py suffix.
+    scriptName = os.path.basename(path)
+
+    if scriptName.endswith('.py'):
+      modName = scriptName[:-3]
+    else:
+      modName = scriptName
+      
+    os.chdir(destinationPath)
+    fileHandle = None
+    try:
+      tup = imp.find_module(modName, ['.'])
+      module = imp.load_module(modName, *tup)
+      fileHandle = tup[0]
+    finally:
+      os.chdir(realPath)
+      if fileHandle is not None:
+        fileHandle.close()
+
+    # Return module name.
+    return module
\ No newline at end of file




More information about the mlpack-svn mailing list