Changeset 5072


Ignore:
Timestamp:
Feb 26, 2008, 9:06:19 AM (16 years ago)
Author:
ole
Message:

Implemented function to obtain path for a python package and refactored two
unit tests to use it when reading in data files. This now works no matter
what the current working directory is.

Location:
anuga_core/source/anuga/utilities
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/utilities/system_tools.py

    r5048 r5072  
    203203
    204204    return crcval
     205
     206def get_pathname_from_package(package):
     207    """Get pathname of given package (provided as string)
     208
     209    This is useful for reading files residing in the same directory as
     210    a particular module. Typically, this is required in unit tests depending
     211    on external files.
     212
     213    The given module must start from a directory on the pythonpath
     214    and be importable using the import statement.
     215
     216    Example
     217    path = get_pathname_from_package('anuga.utilities')
     218
     219    """
     220
     221    exec('import %s as x' %package)
     222
     223    path = x.__path__[0]
     224   
     225    return path
     226
     227    # Alternative approach that has been used at times
     228    #try:
     229    #    # When unit test is run from current dir
     230    #    p1 = read_polygon('mainland_only.csv')
     231    #except:
     232    #    # When unit test is run from ANUGA root dir
     233    #    from os.path import join, split
     234    #    dir, tail = split(__file__)
     235    #    path = join(dir, 'mainland_only.csv')
     236    #    p1 = read_polygon(path)
     237       
     238           
     239
     240
  • anuga_core/source/anuga/utilities/test_polygon.py

    r5050 r5072  
    66from math import sqrt, pi
    77from anuga.utilities.numerical_tools import ensure_numeric
     8from anuga.utilities.system_tools import get_pathname_from_package
    89
    910from polygon import *
     
    6061        from os import sep, getenv
    6162
    62        
    63         try:
    64             # When unit test is run from current dir
    65             p1 = read_polygon('mainland_only.csv')
    66         except:
    67             # When unit test is run from ANUGA root dir
    68             from os.path import join, split
    69             dir, tail = split( __file__)
    70             path = join(dir, 'mainland_only.csv')
    71             p1 = read_polygon(path)
    72        
    73            
     63
     64        # Get path where this test is run
     65        path = get_pathname_from_package('anuga.utilities')
     66
     67        # Form absolute filename and read
     68        filename = path + sep +  'mainland_only.csv'
     69        p1 = read_polygon(filename)       
     70       
    7471       
    7572        f = Polygon_function( [(p1, 10.0)] )
  • anuga_core/source/anuga/utilities/test_system_tools.py

    r5048 r5072  
    109109
    110110        # Get path where this test is run
    111         import anuga.utilities as u
    112         path = u.__path__[0]
     111        path = get_pathname_from_package('anuga.utilities')
     112       
    113113        filename = path + sep +  'crc_test_file.png'
    114114
Note: See TracChangeset for help on using the changeset viewer.