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

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

Bug fix with error checking.

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
15import pyutilib.common
16
17
18class SheetTable(TableData):
19
20    alias("xls", "Manage IO with Excel XLS files.")
21
22    def __init__(self):
23        TableData.__init__(self)
24
25    def open(self):
26        if self.filename is None:
27            raise IOError, "No filename specified"
28        if not os.path.exists(self.filename):
29            raise IOError, "Cannot find file '%s'" % self.filename
30        self.sheet = None
31        if self._data is not None:
32            self.sheet = self._data
33        else:
34            try:
35                self.sheet = ExcelSpreadsheet(self.filename)
36            except pyutilib.common.ApplicationError:
37                raise
38
39    def read(self):
40        if self.sheet is None:
41            return
42        tmp = self.sheet.get_range(self.options.range, raw=True)
43        if type(tmp) in (int,long,float):
44            if not self.options.param is None:
45                self._info = ["param",self.options.param,":=",tmp]
46            elif len(self.options.symbol_map) == 1:
47                self._info = ["param",self.options.symbol_map[self.options.symbol_map.keys()[0]],":=",tmp]
48            else:
49                raise IOError, "Data looks like a parameter, but multiple parameter names have been specified: %s" % str(self.options.symbol_map)
50        elif len(tmp) == 0:
51            raise IOError, "Empty range '%s'" % self.options.range
52        else:
53            self._set_data(tmp[0], tmp[1:])
54        return True
55
56    def close(self):
57        if self._data is None and not self.sheet is None:
58            del self.sheet
59
60
Note: See TracBrowser for help on using the repository browser.