From 825143f4e633c4025ba39307760a3924769f2f02 Mon Sep 17 00:00:00 2001 From: Simon Poirier Date: Tue, 24 Nov 2009 00:30:17 +0000 Subject: creator+probes, not entirely working yet --- (limited to 'tutorius/properties.py') diff --git a/tutorius/properties.py b/tutorius/properties.py index aab6a0c..01cd2c0 100644 --- a/tutorius/properties.py +++ b/tutorius/properties.py @@ -19,6 +19,7 @@ TutoriusProperties have the same behaviour as python properties (assuming you also use the TPropContainer), with the added benefit of having builtin dialog prompts and constraint validation. """ +import uuid from copy import copy, deepcopy from .constraints import Constraint, \ @@ -58,6 +59,8 @@ class TPropContainer(object): self._props[attr_name] = propinstance.validate( copy(propinstance.default)) + self.__id = hash(uuid.uuid4()) + def __getattribute__(self, name): """ Process the 'fake' read of properties in the appropriate instance @@ -111,21 +114,23 @@ class TPropContainer(object): # Providing the hash methods necessary to use TPropContainers # in a dictionary, according to their properties def __hash__(self): - #Return a hash of properties (key, value) sorted by key - #We need to transform the list of property key, value lists into - # a tuple of key, value tuples - return hash(tuple(map(tuple,sorted(self._props.items(), cmp=lambda x, y: cmp(x[0], y[0]))))) + # many places we use containers as keys to store additional data. + # Since containers are mutable, there is a need for a hash function + # where the result is constant, so we can still lookup old instances. + return self.__id def __eq__(self, e2): - return isinstance(e2, type(self)) and self._props == e2._props + return self.__id == e2.__id or \ + (isinstance(e2, type(self)) and self._props == e2._props) # Adding methods for pickling and unpickling an object with # properties def __getstate__(self): - return self._props.copy() + return dict(id=self.__id, props=self._props.copy()) def __setstate__(self, dict): - self._props.update(dict) + self.__id = dict['id'] + self._props.update(dict['props']) class TutoriusProperty(object): """ -- cgit v0.9.1