nti.externalization.persistence: Pickling and ZODB support

Classes and functions for dealing with persistence (and pickling) in an external context.

Note

Importing this module monkey-patches the class persistent.wref.WeakRef to directly implement toExternalObject() and toExternalOID(). It’s not clear why we don’t simply register class adapters for those things, but we don’t.

getPersistentState(obj)[source]

For a persistent.Persistent object, returns one of the constants from the persistent module for its state: persistent.CHANGED and persistent.UPTODATE being the most useful.

If the object is not Persistent and doesn’t implement a getPersistentState method, this method will be pessimistic and assume the object has been persistent.CHANGED.

setPersistentStateChanged(obj)[source]

Explicitly marks a persistent object as changed.

class PersistentExternalizableDictionary(**kwargs)[source]

Bases: persistent.mapping.PersistentMapping, nti.externalization.datastructures.ExternalizableDictionaryMixin

Dictionary mixin that provides toExternalDictionary() to return a new dictionary with each value in the dict having been externalized with toExternalObject().

Changed in version 1.0: No longer extends nti.zodb.persistentproperty.PersistentPropertyHolder. If you have subclasses that use writable properties and which should bypass the normal attribute setter implementation, please mixin this superclass (first) yourself.

toExternalDictionary(*args, **kwargs)[source]

Produce the standard external dictionary for this object.

Uses _ext_replacement.

class PersistentExternalizableList(initlist=None)[source]

Bases: persistent.list.PersistentList

List mixin that provides toExternalList() to return a new list with each element in the sequence having been externalized with toExternalObject().

Changed in version 1.0: No longer extends nti.zodb.persistentproperty.PersistentPropertyHolder. If you have subclasses that use writable properties and which should bypass the normal attribute setter implementation, please mixin this superclass (first) yourself.

values()[source]

For compatibility with zope.generations.utility, this object defines a values method which does nothing but return itself. That makes these objects transparent and suitable for migrations.

class PersistentExternalizableWeakList(initlist=None)[source]

Bases: nti.externalization.persistence.PersistentExternalizableList

Stores persistent.Persistent objects as weak references, invisibly to the user. Any weak references added to the list will be treated the same.

Weak references are resolved on access; if the referrant has been deleted, then that access will return None.

Changed in version 1.0: No longer extends nti.zodb.persistentproperty.PersistentPropertyHolder. If you have subclasses that use writable properties and which should bypass the normal attribute setter implementation, please mixin this superclass (first) yourself.

append(item)[source]

S.append(object) – append object to the end of the sequence

count(value) → integer -- return number of occurrences of value[source]
extend(other)[source]

S.extend(iterable) – extend sequence by appending elements from the iterable

index(value) → integer -- return first index of value.[source]

Raises ValueError if the value is not present.

insert(i, item)[source]

S.insert(index, object) – insert object before index

pop([index]) → item -- remove and return item at index (default last).[source]

Raise IndexError if list is empty or index is out of range.

remove(item)[source]

S.remove(value) – remove first occurrence of value. Raise ValueError if the value is not present.

NoPickle(cls)[source]

A class decorator that prevents an object from being pickled. Useful for ensuring certain objects do not get pickled and thus avoiding ZODB backward compatibility concerns.

Warning

Subclasses of such decorated classes are also not capable of being pickled, without appropriate overrides of __reduce_ex__ and __getstate__. A “working” subclass, but only for ZODB, looks like this:

@NoPickle
class Root(object):
    pass

class CanPickle(Persistent, Root):
    pass

Warning

This decorator emits a warning when it is used directly on a Persistent subclass, as that rather defeats the point (but it is still allowed for backwards compatibility).