| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Tools.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # File for general helper functions
00004 #
00005 # Created by: dandwyer@caltech.edu 2009/07/21
00006 
00007 def realpath( pathname ):
00008     '''Return the absolute path with environment variables and
00009     symbolic links expanded.'''
00010     from os import path
00011     return path.normpath( path.realpath( path.expandvars(pathname) ) )
00012 
00013 def mapify(string):
00014     '''Parse string, return dictionary.  The string can be either in
00015     the form of a Python dictionary, in which case it is simply
00016     eval'ed or it can be of the simpler form of key/value pairs
00017     separated by a colon (":") and with pairs separated with spaces.
00018     This simpler form assumes all keys and values are themselves to be
00019     interpreted as strings unless they begin with an "[" in which case
00020     they will be eval'ed.'''
00021     if "{" in string and "}" in string: 
00022         return eval(string)
00023 
00024     ret = {}
00025     kvs = string.split()
00026     for kv in kvs:
00027         k,v = kv.split(":")
00028         if v[0] == '[': 
00029             v = eval(v)         # turn into list object
00030         ret[k] = v
00031         continue
00032     return ret
00033 
00034 def unitify(string, default_unit=None):
00035     '''Evaluate the given string in a context with units defined.  If
00036     default_unit is given it will be implicitly multiplied 
00037     
00038     Examples:
00039 
00040     1 == unitify("1")
00041     1.1 == unitify("1.1")
00042     1000000000.0 == unitify("second")
00043     1000000000.0 == unitify("1*second")
00044     1000000000.0 == unitify("1","second")
00045     1000000000.0 == unitify("1",units.second)
00046 
00047     '''
00048 
00049     import GaudiKernel.SystemOfUnits as units
00050 
00051     try:                        # check if it is a pure number
00052         num = eval(string,{},{})
00053     except NameError:           # must not be
00054         return eval(string,units.__dict__)
00055     if default_unit:
00056         return eval ("%s*%s"%(num,default_unit),units.__dict__)
00057     return num
00058 
00059 
00060     
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:13:00 2011 for DybPython by doxygen 1.4.7