def parse(tokens): if tokens[0] == '{': ret = OrderedDict() tokens = tokens[1:] while tokens[0] != '}': key = tokens[0] tokens = tokens[1:] tokens = tokens[1:] # : value, tokens = parse(tokens) if tokens[0] == ',': tokens = tokens[1:] ret[key] = value tokens = tokens[1:] return ret, tokens elif tokens[0] == '[': ret = [] tokens = tokens[1:] while tokens[0] != ']': value, tokens = parse(tokens) if tokens[0] == ',': tokens = tokens[1:] ret.append(value) tokens = tokens[1:] return ret, tokens else: return tokens[0], tokens[1:]
def parse(tokens): if tokens[0] == "{": ret = OrderedDict() tokens = tokens[1:] while tokens[0] != "}": key = tokens[0] tokens = tokens[1:] tokens = tokens[1:] # : value, tokens = parse(tokens) if tokens[0] == ",": tokens = tokens[1:] ret[key] = value tokens = tokens[1:] return ret, tokens elif tokens[0] == "[": ret = [] tokens = tokens[1:] while tokens[0] != "]": value, tokens = parse(tokens) if tokens[0] == ",": tokens = tokens[1:] ret.append(value) tokens = tokens[1:] return ret, tokens else: return tokens[0], tokens[1:]
def readLine(self, line): ''' Reads a single line from the stanza, extracting the key-value pair ''' if line.startswith('#') or line == '': OrderedDict.append(self, line) else: raKey = line.split(' ', 1)[0].strip() raVal = '' if (len(line.split(' ', 1)) == 2): raVal = line.split(' ', 1)[1].strip() #if raKey in self: #raise KeyError(raKey + ' already exists') self[raKey] = raVal
def read(self, filePath, key=None): ''' Reads an rafile stanza by stanza, and internalizes it. Don't override this for derived types, instead override readStanza. ''' self._filename = filePath file = open(filePath, 'r') scopes = list() #entry = None stanza = list() keyValue = '' reading = 1 while reading: line = file.readline() # get the line if line == '': reading = 0 # if its a comment or whitespace we append it to the list representation and ignore in dict #line = line.strip() if len(stanza) == 0 and (line.strip().startswith('#') or (line.strip() == '' and reading)): OrderedDict.append(self, line) continue if line.strip() != '': stanza.append(line) elif len(stanza) > 0: if keyValue == '': keyValue, name, entry = self.readStanza(stanza, key) else: testKey, name, entry = self.readStanza(stanza, key) if entry != None and keyValue != testKey: raise KeyError('Inconsistent Key ' + testKey) if entry != None: if name != None or key == None: if name in self: print KeyError('Duplicate Key ' + name) self[name] = entry stanza = list() file.close()
def find_unique_points(explored_parameters): """Takes a list of explored parameters and finds unique parameter combinations. If parameter ranges are hashable operates in O(N), otherwise O(N**2). :param explored_parameters: List of **explored** parameters :return: List of tuples, first entry being the parameter values, second entry a list containing the run position of the unique combination. """ ranges = [param.f_get_range(copy=False) for param in explored_parameters] zipped_tuples = list(zip(*ranges)) try: unique_elements = OrderedDict() for idx, val_tuple in enumerate(zipped_tuples): if val_tuple not in unique_elements: unique_elements[val_tuple] = [] unique_elements[val_tuple].append(idx) return compat.listitems(unique_elements) except TypeError: logger = logging.getLogger('pypet.find_unique') logger.error('Your parameter entries could not be hashed, ' 'now I am sorting slowly in O(N**2).') unique_elements = [] for idx, val_tuple in enumerate(zipped_tuples): matches = False for added_tuple, pos_list in unique_elements: matches = True for idx2, val in enumerate(added_tuple): if not explored_parameters[idx2]._equal_values( val_tuple[idx2], val): matches = False break if matches: pos_list.append(idx) break if not matches: unique_elements.append((val_tuple, [idx])) return unique_elements
def find_unique_points(explored_parameters): """Takes a list of explored parameters and finds unique parameter combinations. If parameter ranges are hashable operates in O(N), otherwise O(N**2). :param explored_parameters: List of **explored** parameters :return: List of tuples, first entry being the parameter values, second entry a list containing the run position of the unique combination. """ ranges = [param.f_get_range(copy=False) for param in explored_parameters] zipped_tuples = list(zip(*ranges)) try: unique_elements = OrderedDict() for idx, val_tuple in enumerate(zipped_tuples): if val_tuple not in unique_elements: unique_elements[val_tuple] = [] unique_elements[val_tuple].append(idx) return compat.listitems(unique_elements) except TypeError: logger = logging.getLogger('pypet.find_unique') logger.error('Your parameter entries could not be hashed, ' 'now I am sorting slowly in O(N**2).') unique_elements = [] for idx, val_tuple in enumerate(zipped_tuples): matches = False for added_tuple, pos_list in unique_elements: matches = True for idx2, val in enumerate(added_tuple): if not explored_parameters[idx2]._equal_values(val_tuple[idx2], val): matches = False break if matches: pos_list.append(idx) break if not matches: unique_elements.append((val_tuple, [idx])) return unique_elements
def append(self, item): OrderedDict.append(self, item)