[ZODB-Dev] import problem ? (code snippet inside)
db at zetamonks.com
db at zetamonks.com
Tue Mar 30 11:56:54 EST 2004
Hi,
I am a newbie with python and ZODB. I have a problem with
importing a module, I can't understand why the following code
is not working. (code is very ugly ;)
Using ZODB 3.2.1c + Python 2.3.2 on a redhat 9.0 and freebsd 4.9
See interactive session capture below the following code snippet
for problem details.
Thanks for your help,
--
Daniel
[dbl at euler]$ cat user.py
#!/usr/bin/env python2.3
from ZODB import FileStorage, DB
from Persistence import Persistent
def connect():
storage = FileStorage.FileStorage('/tmp/datas.fs')
db = DB(storage)
conn = db.open()
dbroot = conn.root()
if not dbroot.has_key('users'):
from BTrees.OOBTree import OOBTree
dbroot['users'] = OOBTree()
db = dbroot['users']
return conn
def get_user(name):
"retourne un objet user ou None si inexistant."
conn = connect()
r = conn.root()
u = None
if r['users'].has_key(name):
u = r['users'][name]
return u
class User(Persistent):
def __init__(self, name = None, passwd = None):
self.name = name
self.passwd = passwd
def check_passwd(self, passwd):
return self.passwd == passwd
if __name__ == "__main__":
n = raw_input("name: ")
p = raw_input("passwd: ")
q = raw_input("create y/n? ")
if q=="y":
print "creating..."
u = User(n, p)
dbroot = connect().root()
dbroot["users"][u.name]=u
get_transaction().commit()
else:
print "aborted!"
# set vim: syn=python ts=4 sw=4 et ai hls tw=79:
[dbl at euler]$ ./user.py
name: db
passwd: mypass
create y/n? y
creating...
[dbl at euler]$ python2.3
Python 2.3.2 (#1, Oct 6 2003, 10:07:16)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import user
>>> u=user.get_user('db')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "user.py", line 22, in get_user
if r['users'].has_key(name):
File "/home/db/src/zodb/ZODB3-3.2c1/build/lib.linux-i686-2.3/ZODB/Connection.py", line 564, in setstate
File "/home/db/src/zodb/ZODB3-3.2c1/build/lib.linux-i686-2.3/ZODB/Connection.py", line 603, in _set_ghost_state
File "/home/db/src/zodb/ZODB3-3.2c1/build/lib.linux-i686-2.3/ZODB/Connection.py", line 200, in _persistent_load
File "/home/db/src/zodb/ZODB3-3.2c1/build/lib.linux-i686-2.3/ZODB/Connection.py", line 162, in __getitem__
File "/home/db/src/zodb/ZODB3-3.2c1/build/lib.linux-i686-2.3/ZODB/DB.py", line 128, in _classFactory
AttributeError: 'module' object has no attribute 'User'
>>>
[dbl at euler]$ python2.3
Python 2.3.2 (#1, Oct 6 2003, 10:07:16)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from user import *
>>> u=get_user('db')
>>> u.name
'db'
More information about the ZODB-Dev
mailing list