def test_flattener(self): tup = (1, 0, (0, 1), (2, 3)) self.assertEqual((1, 0, 0, 1, 2, 3), flatten_tuple(tup)) li = [0] self.assertEqual((0,), flatten_tuple(li)) ex = [(1, 0), [2, 3]] self.assertEqual((1, 0, 2, 3), flatten_tuple(ex))
def _get_iter(self, _slice, key): if key.__class__ not in (tuple, list): key = (key, ) return _IndexedComponent_slice_iter(_slice, _fill_in_known_wildcards( flatten_tuple(key), look_in_index=True), iter_over_index=True)
def _get_iter(self, _slice, key, get_if_not_present=False): # Construct a slice iter with `_fill_in_known_wildcards` # as `advance_iter`. This reuses all the logic from the slice # iter to walk the call/iter stacks, but just jumps to a # particular component rather than actually iterating. # This is how this object does lookups. if key.__class__ not in (tuple, list): key = (key, ) return _IndexedComponent_slice_iter( _slice, _fill_in_known_wildcards(flatten_tuple(key), get_if_not_present=get_if_not_present))
def p_statement(p): '''statement : SET WORD COLONEQ datastar SEMICOLON | SET WORDWITHLBRACKET args RBRACKET COLONEQ datastar SEMICOLON | SET WORD COLON itemstar COLONEQ datastar SEMICOLON | PARAM items COLONEQ datastar SEMICOLON | TABLE items COLONEQ datastar SEMICOLON | LOAD items SEMICOLON | STORE items SEMICOLON | INCLUDE WORD SEMICOLON | INCLUDE QUOTEDSTRING SEMICOLON | DATA SEMICOLON | END SEMICOLON ''' #print "STATEMENT",len(p), p[1:] stmt = p[1] if stmt == 'set': if p[2][-1] == '[': # Just turn off the flatten_list and see what happens p[0] = ['set', p[2][:-1], '['] + list( flatten_tuple([p[i] for i in range(3, len(p) - 1)])) else: p[0] = list(flatten_tuple([p[i] for i in range(1, len(p) - 1)])) elif stmt == 'param': p[0] = list(flatten_tuple([p[i] for i in range(1, len(p) - 1)])) elif stmt == 'include': p[0] = [p[i] for i in range(1, len(p) - 1)] elif stmt == 'load': p[0] = [p[1]] + p[2] elif stmt == 'store': p[0] = [p[1]] + p[2] elif stmt == 'table': p[0] = [p[1]] + [p[2]] + [p[4]] else: # Not necessary, but nice to document how statement could end up None p[0] = None
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 = list(flatten_tuple(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 = list(flatten_tuple(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))