source: coopr.pyomo/trunk/coopr/pyomo/data/sheet.py @ 2592

Last change on this file since 2592 was 2592, checked in by wehart, 11 years ago

Rework of tests to use new plugin factory mechanism
supported by pyutilib.component.core

File size: 2.1 KB
Line 
1#  _________________________________________________________________________
2#
3#  Coopr: A COmmon Optimization Python Repository
4#  Copyright (c) 2008 Sandia Corporation.
5#  This software is distributed under the BSD License.
6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7#  the U.S. Government retains certain rights in this software.
8#  For more information, see the Coopr README.txt file.
9#  _________________________________________________________________________
10
11import os.path
12from TableData import TableData
13from pyutilib.excel import ExcelSpreadsheet
14from pyutilib.component.core import alias
15
16
17class SheetTable(TableData):
18
19    alias("xls", "Manage IO with Excel XLS files.")
20
21    def __init__(self):
22        TableData.__init__(self)
23
24    def open(self):
25        if self.filename is None:
26            raise IOError, "No filename specified"
27        if not os.path.exists(self.filename):
28            raise IOError, "Cannot find file '%s'" % self.filename
29        self.sheet = None
30        if self._data is not None:
31            self.sheet = self._data
32        else:
33            try:
34                self.sheet = ExcelSpreadsheet(self.filename)
35            except pyutilib.ApplicationError:
36                raise
37
38    def read(self):
39        if self.sheet is None:
40            return
41        tmp = self.sheet.get_range(self.options.range, raw=True)
42        if type(tmp) in (int,long,float):
43            if not self.options.param is None:
44                self._info = ["param",self.options.param,":=",tmp]
45            elif len(self.options.symbol_map) == 1:
46                self._info = ["param",self.options.symbol_map[self.options.symbol_map.keys()[0]],":=",tmp]
47            else:
48                raise IOError, "Data looks like a parameter, but multiple parameter names have been specified: %s" % str(self.options.symbol_map)
49        elif len(tmp) == 0:
50            raise IOError, "Empty range '%s'" % self.options.range
51        else:
52            self._set_data(tmp[0], tmp[1:])
53        return True
54
55    def close(self):
56        if self._data is None and not self.sheet is None:
57            del self.sheet
58
59
Note: See TracBrowser for help on using the repository browser.