# HG changeset patch # User paulb@localhost.localdomain # Date 1182110704 -7200 # Node ID ff6cb84e2fc7d8d33b82ba7288be429393a7c6ce # Parent 9afff516a03ad59a9854bcc6c168a9bed0d829db Changed the definitions of __hash__ for Structure-derived classes and for Instance, relying on the Comparable definition. Changed the generation of dictionary keys for the Structure instance attribute 'instances', using the actual objects rather than their 'id'. Added utility methods to _Class for obtaining instance and instance attribute details; simplified the Summariser implementation using these methods. diff -r 9afff516a03a -r ff6cb84e2fc7 simplify/annotate.py --- a/simplify/annotate.py Sat Jun 16 01:57:38 2007 +0200 +++ b/simplify/annotate.py Sun Jun 17 22:05:04 2007 +0200 @@ -1436,6 +1436,9 @@ def has_key(self, name): return self.names.has_key(name) + def keys(self): + return self.names.keys() + def items(self): return self.names.items() diff -r 9afff516a03a -r ff6cb84e2fc7 simplify/simplified/data.py --- a/simplify/simplified/data.py Sat Jun 16 01:57:38 2007 +0200 +++ b/simplify/simplified/data.py Sun Jun 17 22:05:04 2007 +0200 @@ -37,6 +37,32 @@ def full_name(self): return "class %s" % self._full_name + def get_instance_attribute_names(self): + + "Return all attribute names used by the instances of this class." + + names = set() + for instance in self.instances.values(): + for name in instance.namespace.keys(): + names.add(name) + return names + + def get_names_to_instances(self): + + """ + Return a tuple containing a mapping from names to instances, and a list + of sorted instance names. + """ + + d = {} + names = [] + for instance in self.instances.values(): + name = instance.full_name() + names.append(name) + d[name] = instance + names.sort() + return d, names + class SingleInstanceClass(_Class): "A Python class." @@ -72,7 +98,7 @@ self.attributes_for_instances = {} def _get_key(self, node): - return id(getattr(node, "original", None)) # self.module.original + return getattr(node, "original", None) # self.module.original def has_instance(self, node): return self.instances.has_key(self._get_key(node)) @@ -117,7 +143,7 @@ def _get_key(self, node): if self.namespace.has_key("__atomic__"): - return id(self) + return self else: return MultipleInstanceClass._get_key(self, node) @@ -138,9 +164,9 @@ def _get_key(self, node): if self.namespace.has_key("__atomic__"): - return id(self) + return self else: - return id(node) + return node def has_instance(self, node): requesting_instance = getattr(node, "instance", None) @@ -174,14 +200,6 @@ else: raise ValueError, "__class__" - def __eq__(self, other): - # NOTE: Single instance: all instances are the same - # NOTE: Multiple instances: all instances are different - return self.full_name() == other.full_name() - - def __hash__(self): - return id(self) - class Constant: "A constant initialised with a type name for future processing." @@ -202,12 +220,12 @@ self.context = context self.type = type + def __repr__(self): + return "Attribute(%s, %s)" % (repr(self.context), repr(self.type)) + def __eq__(self, other): return hasattr(other, "type") and other.type == self.type or other == self.type - def __repr__(self): - return "Attribute(%s, %s)" % (repr(self.context), repr(self.type)) - def __hash__(self): return id(self.type) diff -r 9afff516a03a -r ff6cb84e2fc7 simplify/simplified/program.py --- a/simplify/simplified/program.py Sat Jun 16 01:57:38 2007 +0200 +++ b/simplify/simplified/program.py Sun Jun 17 22:05:04 2007 +0200 @@ -95,6 +95,9 @@ self.types = set() self.annotated = 0 + def __hash__(self): + return id(self) + def __repr__(self): "Return a readable representation." diff -r 9afff516a03a -r ff6cb84e2fc7 simplify/viewer.py --- a/simplify/viewer.py Sat Jun 16 01:57:38 2007 +0200 +++ b/simplify/viewer.py Sun Jun 17 22:05:04 2007 +0200 @@ -197,7 +197,7 @@ def _write_classes(self, module): self.stream.write("\n") - for structure in self.structures: + for structure in self.module.simplifier.structures: self._write_class(structure) self.stream.write("
\n") @@ -214,7 +214,8 @@ # ...and all known attribute names. - structure_attributes = self.structure_attributes[structure] + structure_attributes = structure.get_instance_attribute_names() + for name in self.attribute_names: if name in structure_attributes: self.stream.write("%s\n" % self._text(name)) @@ -224,9 +225,12 @@ # Write instances for the class, along with type details for each attribute. - for instance in self.structures_to_instances[structure]: + names_to_instances, instance_names = structure.get_names_to_instances() + + for instance_name in instance_names: + instance = names_to_instances[instance_name] self.stream.write("\n") - self.stream.write("%s\n" % self._text(instance.full_name())) + self.stream.write("%s\n" % self._text(instance_name)) for name in self.attribute_names: self.stream.write("\n") values = instance.namespace.get(name, []) @@ -240,50 +244,15 @@ def _init_details(self): names = set() - self.structures_to_instances = {} - self.structure_attributes = {} - names_to_structures = {} # Visit all structures. for structure in self.module.simplifier.structures: - - # Map all instance names to instances. - - names_to_instances = {} - attribute_names = set() - - for instance in structure.instances.values(): - for name, values in instance.namespace.items(): - if not name in attribute_names: - attribute_names.add(name) - if not name in names: - names.add(name) - names_to_instances[instance.full_name()] = instance - - # Record the instances in name order for the structure. - - instance_names = names_to_instances.keys() - instance_names.sort() - sorted_instances = [names_to_instances[name] for name in instance_names] - self.structures_to_instances[structure] = sorted_instances - - # Record the attributes used in all instances of the structure. - - self.structure_attributes[structure] = attribute_names - - # Record the name to structure mapping. - - names_to_structures[structure.full_name()] = structure + names.update(structure.get_instance_attribute_names()) self.attribute_names = list(names) self.attribute_names.sort() - structure_names = names_to_structures.keys() - structure_names.sort() - - self.structures = [names_to_structures[name] for name in structure_names] - # Browser classes. class Browser(ASTVisitor, Writer):