# HG changeset patch # User Paul Boddie # Date 1203798303 -3600 # Node ID 3cc8cd3d0a1453d9441e3f286d43fea8b2f87e12 # Parent 8a0028b6ef9db6330c9f3e4228a6b6136229fc13 Introduced a separate method for assigning instance attribute positions. diff -r 8a0028b6ef9d -r 3cc8cd3d0a14 micropython/inspect.py --- a/micropython/inspect.py Sat Feb 23 21:11:02 2008 +0100 +++ b/micropython/inspect.py Sat Feb 23 21:25:03 2008 +0100 @@ -386,44 +386,59 @@ if not instattr.has_key(name): instattr[name] = set([-1]) # position not yet defined + # Cache the attributes by converting the positioned attributes into + # a dictionary. + if not instattr: self.all_instattr = {} else: - positions = instattr.items() - instarray = [None] * len(positions) - - # Get the positions in ascending order of list size, with lists - # of the same size ordered according to their smallest position - # value. - - positions.sort(self._cmp_positions) - #print self.name, positions - - # Get the names in position order. - - held = [] - - for name, pos in positions: - pos = list(pos) - if pos[0] != -1 and instarray[pos[0]] is None: - instarray[pos[0]] = name, Attr(pos[0], None, name) - else: - if pos[0] != -1 or len(pos) > 1: - self.instattr_relocated.add(name) - held.append((name, pos)) - - for i, attr in enumerate(instarray): - if attr is None: - name, pos = held.pop() - instarray[i] = name, Attr(i, None, name) - - self.all_instattr = dict(instarray) + self.all_instattr = dict(self._get_position_list(instattr)) self.all_instattr_names = self.all_instattr.keys() return self.all_instattr + def _get_position_list(self, instattr): + + """ + Return the instance attributes, 'instattr', as a list indicating the + positions of the attributes in the final instance structure. + """ + + positions = instattr.items() + instarray = [None] * len(positions) + + # Get the positions in ascending order of list size, with lists + # of the same size ordered according to their smallest position + # value. + + positions.sort(self._cmp_positions) + #print self.name, positions + + # Get the names in position order. + + held = [] + + for name, pos in positions: + pos = list(pos) + if pos[0] != -1 and instarray[pos[0]] is None: + instarray[pos[0]] = name, Attr(pos[0], None, name) + else: + if pos[0] != -1 or len(pos) > 1: + self.instattr_relocated.add(name) + held.append((name, pos)) + + for i, attr in enumerate(instarray): + if attr is None: + name, pos = held.pop() + instarray[i] = name, Attr(i, None, name) + + return instarray + def _cmp_positions(self, a, b): + + "Compare name plus position list operands 'a' and 'b'." + name_a, list_a = a name_b, list_b = b if len(list_a) < len(list_b):