# HG changeset patch # User paulb@localhost.localdomain # Date 1182119746 -7200 # Node ID 73351400ca6dff58043fa84a7081011275c109b8 # Parent ff6cb84e2fc7d8d33b82ba7288be429393a7c6ce Added a method which attempts to return distinct instances from those stored for a particular class. Improved the docstrings. diff -r ff6cb84e2fc7 -r 73351400ca6d simplify/simplified/data.py --- a/simplify/simplified/data.py Sun Jun 17 22:05:04 2007 +0200 +++ b/simplify/simplified/data.py Mon Jun 18 00:35:46 2007 +0200 @@ -28,7 +28,10 @@ class _Class(Structure, WithName): - "A Python class." + """ + The basis of a Python class. Classes with specific instantiation behaviour + should inherit from this class. + """ def __init__(self, *args, **kw): Structure.__init__(self, *args, **kw) @@ -37,6 +40,8 @@ def full_name(self): return "class %s" % self._full_name + # Utility methods. + def get_instance_attribute_names(self): "Return all attribute names used by the instances of this class." @@ -63,9 +68,32 @@ names.sort() return d, names + def get_distinct_instances(self): + + """ + Return a list of instances whose instance attribute types are distinct. + """ + + instances = [] + names_found = [] + + # Rather than use the instances directly, get them in name order in + # order to favour those earlier according to the sorting. + + names_to_instances, instance_names = self.get_names_to_instances() + + for instance_name in instance_names: + instance = names_to_instances[instance_name] + names = instance.namespace.names + if not names in names_found: + instances.append(instance) + names_found.append(names) + + return instances + class SingleInstanceClass(_Class): - "A Python class." + "A Python class producing only one instance." def __init__(self, *args, **kw): _Class.__init__(self, *args, **kw) @@ -90,7 +118,7 @@ class MultipleInstanceClass(_Class): - "A Python class." + "A Python class producing many instances." def __init__(self, *args, **kw): _Class.__init__(self, *args, **kw)