Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
ZODB
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
ZODB
Commits
50687847
Commit
50687847
authored
Apr 16, 2004
by
Jeremy Hylton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use transaction package instead of get_transaction().
Add explanation for __getattr__ and friends.
parent
456b5d9c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
21 deletions
+51
-21
doc/guide/prog-zodb.tex
doc/guide/prog-zodb.tex
+51
-21
No files found.
doc/guide/prog-zodb.tex
View file @
50687847
...
...
@@ -222,6 +222,8 @@ it with data, insert it into the \class{BTree} instance, and commit
this transaction.
\begin{verbatim}
# Create new User instance
import transaction
newuser = User()
# Add whatever attributes you want to track
...
...
@@ -233,18 +235,18 @@ newuser.first_name = 'Andrew' ; newuser.last_name = 'Kuchling'
userdb[newuser.id] = newuser
# Commit the change
get
_
transaction()
.commit()
transaction
.commit()
\end{verbatim}
When you import the ZODB package, it adds a new function,
\function
{
get
_
transaction()
}
, to Python's collection of built-in
functions.
\function
{
get
_
transaction()
}
returns a
\class
{
Transaction
}
object, which has two important methods:
\method
{
commit()
}
and
\method
{
abort()
}
.
\method
{
commit()
}
writes any modified objects
to disk, making the changes permanent, while
\method
{
abort()
}
rolls
The
\module
{
transaction
}
module defines a few top-level functions for
working with transactions.
\method
{
commit()
}
writes any modified
objects to disk, making the changes permanent.
\method
{
abort()
}
rolls
back any changes that have been made, restoring the original state of
the objects. If you're familiar with database transactional
semantics, this is all what you'd expect.
semantics, this is all what you'd expect.
\method
{
get()
}
returns a
\class
{
Transaction
}
object that has additional methods like
\method
{
status()
}
, to check the current state of the transaction, and
\method
{
note()
}
, to add a note to the transaction metadata.
Because the integration with Python is so complete, it's a lot like
having transactional semantics for your program's variables, and you
...
...
@@ -257,7 +259,7 @@ can experiment with transactions at the Python interpreter's prompt:
>>> newuser.first
_
name = 'Bob' # Change first name
>>> newuser.first
_
name # Verify the change
'Bob'
>>>
get
_
transaction().abort()
# Abort transaction
>>>
transaction.abort()
# Abort transaction
>>> newuser.first
_
name # The value has changed back
'Andrew'
\end{verbatim}
...
...
@@ -340,7 +342,7 @@ would then look like this:
\begin{verbatim}
def add
_
friend(self, friend):
self.friends.append(otherUser)
self.
_
p
_
changed =
1
self.
_
p
_
changed =
True
\end{verbatim}
Alternatively, you could use a ZODB-aware list or mapping type that
...
...
@@ -355,17 +357,45 @@ and may make it into a future upstream release of Zope.
\subsubsection
{
\method
{__
getattr
__}
,
\method
{__
delattr
__}
, and
\method
{__
setattr
__}}
% XXX This section could be out-of-date. I've got to remember how we
% decided to do this before the beta release.
Recent versions of ZODB allow writing persistent classes that have
\method
{__
getattr
__}
,
\method
{__
delattr
__}
, or
\method
{__
setattr
__}
methods. The one minor complication is that the machinery for
automatically detecting changes to the object is disabled while the
\method
{__
getattr
__}
,
\method
{__
delattr
__}
, or
\method
{__
setattr
__}
method is executing. This means that if the object is modified, the
object should be marked as dirty by setting the object's
\member
{_
p
_
changed
}
method to true.
ZODB allows persistent classes to have hook methods like
\method
{__
getattr
__}
and
\method
{__
setattr
__}
. There are four special
methods that control attribute access; the rules for each are a little
different.
The
\method
{__
getattr
__}
method works pretty much the same for
persistent classes as it does for other classes. No special handling
is needed. If an object is a ghost, then it will be activated before
\method
{__
getattr
__}
is called.
The other methods are more delicate. They will override the hooks
provided by
\class
{
Persistent
}
, so user code must call special methods
to invoke those hooks anyway.
The
\method
{__
getattribute
__}
method will be called for all attribute
access; it overrides the attribute access support inherited from
\class
{
Persistent
}
. A user-defined
\method
{__
getattribute
__}
must always give the
\class
{
Persistent
}
base
class a chance to handle special attribute, as well as
\member
{__
dict
__}
or
\member
{__
class
__}
. The user code should
call
\method
{_
p
_
getattr
}
, passing the name of the attribute as the
only argument. If it returns True, the user code should call
\class
{
Persistent
}
's
\method
{__
getattribute
__}
to get the value. If
not, the custom user code can run.
A
\method
{__
setattr
__}
hook will also override the
\class
{
Persistent
}
\method
{__
setattr
__}
hook. User code must treat it much like
\method
{__
getattribute
__}
. The user-defined code must call
\method
{_
p
_
setattr
}
first to all
\class
{
Persistent
}
to handle special
attributes;
\method
{_
p
_
setattr
}
takes the attribute name and value.
If it returns True,
\class
{
Persistent
}
handled the attribute. If not,
the user code can run. If the user code modifies the object's state,
it must assigned to
\member
{_
p
_
changed
}
.
A
\method
{__
delattr
__}
hooks must be implemented the same was as a the
last two hooks. The user code must call
\method
{_
p
_
delattr
}
, passing
the name of the attribute as an argument. If the call returns True,
\class
{
Persistent
}
handled the attribute; if not, the user code can
run.
\subsection
{
Writing Persistent Classes
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment