source: coopr.pyomo/trunk/coopr/pyomo/data/text.py @ 2289

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

Setting up 'import' tests for the other simple examples.
These seem to work, but the internal code is too ugly. I'm
going to clean that up next to help explain what is going on...

File size: 1.9 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            if not self.options.param is None:
43                self._info = ["param",self.options.param,":=",tmp[0][0]]
44            elif len(self.options.symbol_map) == 1:
45                self._info = ["param",self.options.symbol_map[self.options.symbol_map.keys()[0]],":=",tmp[0][0]]
46            else:
47                raise IOError, "Data looks like a parameter, but multiple parameter names have been specified: %s" % str(self.options.symbol_map)
48        else:
49            self._set_data(tmp[0], tmp[1:])
50        return True
51
52
53
54DataManagerRegistration("tab", TextTable, "Manage IO with ASCI tables.")
55
Note: See TracBrowser for help on using the repository browser.