source: coopr.pyomo/trunk/coopr/pyomo/data/TableData.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: 4.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
11__all__ = ['TableData']
12
13from pyutilib.misc import Options
14from process_data import _process_data
15
16class TableData(object):
17    """
18    An object that imports data from a table in an external data source.
19    """
20
21    def __init__(self):
22        """
23        Constructor
24        """
25        self._info=None
26        self._data=None
27
28    def initialize(self, filename, **kwds):
29        self.filename = filename
30        self.options = Options(**kwds)
31
32    def open(self, filename):
33        """
34        Open the table
35        """
36        pass
37
38    def read(self):
39        """
40        Read data from the table
41        """
42        pass
43
44    def close(self):
45        """
46        Close the table
47        """
48        pass
49
50    def process(self, model, data, default):
51        """
52        Return the data that was extracted from this table
53        """
54        return _process_data(self._info, model, data, default, self.filename)
55
56    def clear(self):
57        """
58        Clear the data that was extracted from this table
59        """
60        self._info = None
61
62    def _set_data(self, headers, rows):
63        #print "_SET_DATA"
64        if self.options.symbol_map is None:
65            mapped_headers = map(str,list(headers))
66        else:
67            mapped_headers = [self.options.symbol_map[tuple(header)] if tuple(header) in self.options.symbol_map else header for header in map(str,list(headers))]
68        #print "X",mapped_headers,self.options.symbol_map,self.options.index,self.options.format
69
70        if not self.options.set is None:
71           if self.options.format is None:
72              self._info = ["set",self.options.set,":="]
73              for row in rows:
74                self._info = self._info + list(row)
75           elif self.options.format == "set_array":
76              self._info = ["set",self.options.set, ":"]
77              self._info = self._info + list(mapped_headers[1:])
78              self._info = self._info + [":="]
79              for row in rows:
80                self._info = self._info + list(row)
81           else:
82              raise ValueError, "Unknown set format: "+self.options.format
83
84        elif self.options.index is not None and self.options.param is None:
85           #print "Z1"
86           self._info = ["param",":",self.options.index,":"]
87           self._info = self._info + mapped_headers[self.options.nindex:]
88           self._info.append(":=")
89           for row in rows:
90             self._info = self._info + list(row)
91
92        elif self.options.index is not None and self.options.param is not None:
93           #print "Z2"
94           self._info = ["param",":",self.options.index,":"]
95           if type(self.options.param) is str:
96              self._info = self._info + [self.options.param]
97           elif type(self.options.param) is tuple:
98              self._info = self._info + list(self.options.param)
99           else:
100              self._info = self._info + self.options.param
101           self._info.append(":=")
102           for row in rows:
103             self._info = self._info + list(row)
104
105        elif self.options.param is not None and self.options.format is not None:
106           #print "Z3"
107           if self.options.format == "transposed_array":
108              self._info = ["param",self.options.param,"(tr)",":"] + mapped_headers[1:]
109           elif self.options.format == "array":
110              self._info = ["param",self.options.param,":"] + mapped_headers[1:]
111           else:
112              raise ValueError, "Unknown parameter format: "+self.options.format
113           self._info.append(":=")
114           for row in rows:
115             self._info = self._info + list(row)
116
117        else:
118           #print "Z4"
119           if len(mapped_headers) == 1:
120              self._info = ["set",mapped_headers[0],":="]
121              for row in rows:
122                self._info = self._info + list(row)
123           else:
124              self._info = ["param",":"]
125              if self.options.param is None:
126                 self._info = self._info + mapped_headers[1:]
127              elif type(self.options.param) is str:
128                 self._info = self._info + [self.options.param]
129              else:
130                 self._info = self._info + list(self.options.param)
131              self._info.append(":=")
132              for row in rows:
133                self._info = self._info + list(row)
134
Note: See TracBrowser for help on using the repository browser.