source: coopr.pyomo/trunk/coopr/pyomo/data/text.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.5 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
12import re
13from TableData import TableData
14from coopr.pyomo.base.plugin import DataManagerRegistration
15
16
17class TextTable(TableData):
18
19    def __init__(self):
20        TableData.__init__(self)
21
22    def open(self):
23        if self.filename is None:
24            raise IOError, "No filename specified"
25        if not os.path.exists(self.filename):
26            raise IOError, "Cannot find file '%s'" % self.filename
27        self.INPUT = open(self.filename, 'r')
28
29    def close(self):
30        self.INPUT.close()
31       
32    def read(self):
33        tmp=[]
34        for line in self.INPUT:
35            line=line.strip()
36            tokens = re.split("[\t ]+",line)
37            if tokens != ['']:
38                tmp.append(tokens)
39        if len(tmp) == 0:
40            raise IOError, "Empty *.tab file"
41        elif len(tmp) == 1:
42            self._info = ["param",self.options.param,":=",tmp[0][0]]
43        else:
44            self._set_data(tmp[0], tmp[1:])
45        return True
46
47
48
49DataManagerRegistration("tab", TextTable, "Manage IO with ASCI tables.")
50
Note: See TracBrowser for help on using the repository browser.