Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Length.py 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2001, 2002 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE
  12. #
  13. ##############################################################################
  14. import persistent
  15. class Length(persistent.Persistent):
  16. """BTree lengths are often too expensive to compute.
  17. Objects that use BTrees need to keep track of lengths themselves.
  18. This class provides an object for doing this.
  19. As a bonus, the object support application-level conflict
  20. resolution.
  21. It is tempting to to assign length objects to __len__ attributes
  22. to provide instance-specific __len__ methods. However, this no
  23. longer works as expected, because new-style classes cache
  24. class-defined slot methods (like __len__) in C type slots. Thus,
  25. instance-defined slot fillers are ignored.
  26. """
  27. # class-level default required to keep copy.deepcopy happy -- see
  28. # https://bugs.launchpad.net/zodb/+bug/516653
  29. value = 0
  30. def __init__(self, v=0):
  31. self.value = v
  32. def __getstate__(self):
  33. return self.value
  34. def __setstate__(self, v):
  35. self.value = v
  36. def set(self, v):
  37. "Set the length value to v."
  38. self.value = v
  39. def _p_resolveConflict(self, old, s1, s2):
  40. return s1 + s2 - old
  41. def change(self, delta):
  42. "Add delta to the length value."
  43. self.value += delta
  44. def __call__(self, *args):
  45. "Return the current length value."
  46. return self.value