def _process_table(cmd, _model, _data, _default, options=None): #print("TABLE %s" % cmd) # _options = {} _set = OrderedDict() _param = OrderedDict() _labels = [] _cmd = cmd[1] _cmd_len = len(_cmd) name = None i = 0 while i < _cmd_len: try: #print("CMD i=%s cmd=%s" % (i, _cmd[i:])) # # This should not be error prone, so we treat errors # with a general exception # # # Processing labels # if _cmd[i] == ':': i += 1 while i < _cmd_len: _labels.append(_cmd[i]) i += 1 continue # # Processing options # name = _cmd[i] if i + 1 == _cmd_len: _param[name] = [] _labels = ['Z'] i += 1 continue if _cmd[i + 1] == '=': if type(_cmd[i + 2]) is list: _set[name] = _cmd[i + 2] else: _options[name] = _cmd[i + 2] i += 3 continue # This should be a parameter declaration if not type(_cmd[i + 1]) is tuple: raise IOError if i + 2 < _cmd_len and _cmd[i + 2] == '=': _param[name] = (_cmd[i + 1], _cmd[i + 3][0]) i += 4 else: _param[name] = _cmd[i + 1] i += 2 except: raise IOError("Error parsing table options: %s" % name) #print("_options %s" % _options) #print("_set %s" % _set) #print("_param %s" % _param) #print("_labels %s" % _labels) # options = Options(**_options) for key in options: if not key in ['columns']: raise ValueError("Unknown table option '%s'" % key) # ncolumns = options.columns if ncolumns is None: ncolumns = len(_labels) if ncolumns == 0: if not (len(_set) == 1 and len(_set[_set.keys()[0]]) == 0): raise IOError( "Must specify either the 'columns' option or column headers" ) else: ncolumns = 1 else: ncolumns = int(ncolumns) # data = cmd[2] Ldata = len(cmd[2]) # cmap = {} if len(_labels) == 0: for i in range(ncolumns): cmap[i + 1] = i for label in _param: ndx = cmap[_param[label][1]] if ndx < 0 or ndx >= ncolumns: raise IOError("Bad column value %s for data %s" % (str(ndx), label)) cmap[label] = ndx _param[label] = _param[label][0] else: i = 0 for label in _labels: cmap[label] = i i += 1 #print("CMAP %s" % cmap) # #print("_param %s" % _param) #print("_set %s" % _set) for sname in _set: # Creating set sname cols = _set[sname] tmp = [] for col in cols: if not col in cmap: raise IOError( "Unexpected table column '%s' for index set '%s'" % (col, sname)) tmp.append(cmap[col]) if not sname in cmap: cmap[sname] = tmp cols = flatten(tmp) # _cmd = ['set', sname, ':='] i = 0 while i < Ldata: row = [] #print("COLS %s NCOLS %d" % (cols, ncolumns)) for col in cols: #print("Y %s %s" % (i, col)) row.append(data[i + col]) if len(row) > 1: _cmd.append(tuple(row)) else: _cmd.append(row[0]) i += ncolumns #print("_data %s" % _data) _process_set(_cmd, _model, _data) # #print("CMAP %s" % cmap) _i = 0 if ncolumns == 0: raise IOError for vname in _param: _i += 1 # create value vname cols = _param[vname] tmp = [] for col in cols: #print("COL %s" % col) if not col in cmap: raise IOError( "Unexpected table column '%s' for table value '%s'" % (col, vname)) tmp.append(cmap[col]) #print("X %s %s" % (len(cols), tmp)) cols = flatten(tmp) #print("X %s" % len(cols)) #print("VNAME %s %s" % (vname, cmap[vname])) if vname in cmap: cols.append(cmap[vname]) else: cols.append(ncolumns - 1 - (len(_param) - _i)) #print("X %s" % len(cols)) # _cmd = ['param', vname, ':='] i = 0 while i < Ldata: #print("HERE %s %s %s" % (i, cols, ncolumns)) for col in cols: _cmd.append(data[i + col]) i += ncolumns #print("HERE %s" % _cmd) #print("_data %s" % _data) _process_param(_cmd, _model, _data, None, ncolumns=len(cols))
def _process_table(cmd, _model, _data, _default, options=None): #print "TABLE", cmd # options = Options(**cmd[1]) for key in options: if not key in ['columns']: raise ValueError("Unknown table option '%s'" % key) # ncolumns=options.columns if ncolumns is None: ncolumns = len(cmd[4]) if ncolumns == 0: if not (len(cmd[3]) == 1 and len(cmd[3][cmd[3].keys()[0]]) == 0): raise IOError("Must specify either the 'columns' option or column headers") else: ncolumns=1 else: ncolumns = int(ncolumns) # data = cmd[5] Ldata = len(cmd[5]) # cmap = {} if len(cmd[4]) == 0: for i in range(ncolumns): cmap[str(i+1)] = i for label in cmd[3]: ndx = int(cmd[3][label][1])-1 if ndx < 0 or ndx >= ncolumns: raise IOError("Bad column value %s for data %s" % (str(ndx), label)) cmap[label] = ndx cmd[3][label] = cmd[3][label][0] else: i = 0 for label in cmd[4]: cmap[label] = i i += 1 # for sname in cmd[2]: # Creating set sname cols = cmd[2][sname] tmp = [] for col in cols: if not col in cmap: raise IOError("Unexpected table column '%s' for index set '%s'" % (col, sname)) tmp.append(cmap[col]) cmap[sname] = tmp cols = tmp # _cmd = ['set', sname, ':='] i = 0 while i < Ldata: row = [] for col in cols: row.append( data[i+col] ) if len(row) > 1: _cmd.append( tuple(row) ) else: _cmd.append( row[0] ) i += ncolumns _process_set(_cmd, _model, _data) # _i=0 #print "HERE ncol", ncolumns if ncolumns == 0: raise IOError for vname in cmd[3]: _i += 1 # create value vname cols = cmd[3][vname] tmp = [] for col in cols: if not col in cmap: raise IOError("Unexpected table column '%s' for table value '%s'" % (col, vname)) tmp.append(cmap[col]) cols = flatten(tmp) if vname in cmap: cols.append(cmap[vname]) else: cols.append( ncolumns-1 - (len(cmd[3])-_i) ) # _cmd = ['param', vname, ':='] i = 0 while i < Ldata: #print "HERE", i, cols, ncolumns for col in cols: _cmd.append( data[i+col] ) i += ncolumns _process_param(_cmd, _model, _data, None, ncolumns=len(cols))
def _process_table(cmd, _model, _data, _default, options=None): #print("TABLE %s" % cmd) # _options = {} _set = OrderedDict() _param = OrderedDict() _labels = [] _cmd = cmd[1] _cmd_len = len(_cmd) name = None i = 0 while i < _cmd_len: try: #print("CMD i=%s cmd=%s" % (i, _cmd[i:])) # # This should not be error prone, so we treat errors # with a general exception # # # Processing labels # if _cmd[i] == ':': i += 1 while i < _cmd_len: _labels.append(_cmd[i]) i += 1 continue # # Processing options # name = _cmd[i] if i+1 == _cmd_len: _param[name] = [] _labels = ['Z'] i += 1 continue if _cmd[i+1] == '=': if type(_cmd[i+2]) is list: _set[name] = _cmd[i+2] else: _options[name] = _cmd[i+2] i += 3 continue # This should be a parameter declaration if not type(_cmd[i+1]) is tuple: raise IOError if i+2 < _cmd_len and _cmd[i+2] == '=': _param[name] = (_cmd[i+1], _cmd[i+3][0]) i += 4 else: _param[name] = _cmd[i+1] i += 2 except: raise IOError("Error parsing table options: %s" % name) #print("_options %s" % _options) #print("_set %s" % _set) #print("_param %s" % _param) #print("_labels %s" % _labels) # options = Options(**_options) for key in options: if not key in ['columns']: raise ValueError("Unknown table option '%s'" % key) # ncolumns=options.columns if ncolumns is None: ncolumns = len(_labels) if ncolumns == 0: if not (len(_set) == 1 and len(_set[_set.keys()[0]]) == 0): raise IOError("Must specify either the 'columns' option or column headers") else: ncolumns=1 else: ncolumns = int(ncolumns) # data = cmd[2] Ldata = len(cmd[2]) # cmap = {} if len(_labels) == 0: for i in range(ncolumns): cmap[i+1] = i for label in _param: ndx = cmap[_param[label][1]] if ndx < 0 or ndx >= ncolumns: raise IOError("Bad column value %s for data %s" % (str(ndx), label)) cmap[label] = ndx _param[label] = _param[label][0] else: i = 0 for label in _labels: cmap[label] = i i += 1 #print("CMAP %s" % cmap) # #print("_param %s" % _param) #print("_set %s" % _set) for sname in _set: # Creating set sname cols = _set[sname] tmp = [] for col in cols: if not col in cmap: raise IOError("Unexpected table column '%s' for index set '%s'" % (col, sname)) tmp.append(cmap[col]) if not sname in cmap: cmap[sname] = tmp cols = flatten(tmp) # _cmd = ['set', sname, ':='] i = 0 while i < Ldata: row = [] #print("COLS %s NCOLS %d" % (cols, ncolumns)) for col in cols: #print("Y %s %s" % (i, col)) row.append( data[i+col] ) if len(row) > 1: _cmd.append( tuple(row) ) else: _cmd.append( row[0] ) i += ncolumns #print("_data %s" % _data) _process_set(_cmd, _model, _data) # #print("CMAP %s" % cmap) _i=0 if ncolumns == 0: raise IOError for vname in _param: _i += 1 # create value vname cols = _param[vname] tmp = [] for col in cols: #print("COL %s" % col) if not col in cmap: raise IOError("Unexpected table column '%s' for table value '%s'" % (col, vname)) tmp.append(cmap[col]) #print("X %s %s" % (len(cols), tmp)) cols = flatten(tmp) #print("X %s" % len(cols)) #print("VNAME %s %s" % (vname, cmap[vname])) if vname in cmap: cols.append(cmap[vname]) else: cols.append( ncolumns-1 - (len(_param)-_i) ) #print("X %s" % len(cols)) # _cmd = ['param', vname, ':='] i = 0 while i < Ldata: #print("HERE %s %s %s" % (i, cols, ncolumns)) for col in cols: _cmd.append( data[i+col] ) i += ncolumns #print("HERE %s" % _cmd) #print("_data %s" % _data) _process_param(_cmd, _model, _data, None, ncolumns=len(cols))