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

Last change on this file since 2274 was 2274, checked in by wehart, 10 years ago

Added the 'include' command, which can include other *.dat files. This works
like the AMPL include command, with the exception that it must be terminated by
a semicolon.

Reworked the AMPL datafile plugin to simple execute an include command
on a *.dat file. This rework has the advantage that filename/lineno information
is now propigated throught he AMPL command processing.

File size: 1.7 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 coopr.pyomo.base.plugin import DataManagerRegistration
15
16class SheetTable(TableData):
17
18    def __init__(self):
19        TableData.__init__(self)
20
21    def open(self):
22        if self.filename is None:
23            raise IOError, "No filename specified"
24        if not os.path.exists(self.filename):
25            raise IOError, "Cannot find file '%s'" % self.filename
26        self.sheet = None
27        if self._data is not None:
28            self.sheet = self._data
29        else:
30            try:
31                self.sheet = ExcelSpreadsheet(self.filename)
32            except pyutilib.ApplicationError:
33                raise
34
35    def read(self):
36        if self.sheet is None:
37            return
38        tmp = self.sheet.get_range(self.options.range, raw=True)
39        if type(tmp) in (int,long,float):
40            self._info = ["param",self.options.param,":=",tmp]
41        else:
42            self._set_data(tmp[0], tmp[1:])
43        return True
44
45    def close(self):
46        if self._data is None and not self.sheet is None:
47            del self.sheet
48
49
50DataManagerRegistration("xls", SheetTable, "Manage IO with Excel XLS files.")
51
Note: See TracBrowser for help on using the repository browser.