[Tutor] How does Shelve work?
S A
buc40@bemail.org
Fri, 6 Sep 2002 19:41:29 -0700
This is a multi-part message in MIME format...
--oMTJpMNmSdfKIvBP55bemGKwA
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
> Ibraheem Umaru-Mohammed <umarumohammed@btinternet.com> S A <buc40@bemail.org>Cc: tutor@python.org
> Re: [Tutor] How does Shelve work?Date: Fri, 6 Sep 2002 12:18:06 +0100
>
>Does the following help at all?
>
>,---- [ shelve module example - foo.py ]
>| #!/usr/bin/env python
>| import shelve
>|
>| handle = shelve.open("mydb")
>| # save objects
>| handle[1]="one"
>| handle[2]="two"
>| handle[3]="three"
>| handle[4]="four"
>| handle[5]="five"
>|
>| # retrieve objects
>| print "Stored the following to database: "
>| for item in handle.keys():
>| print item, "=>", handle[item]
>| handle.close()
>`----
>
>You may also want to look at the following, although it wasn't all that
helpful:
>
>,---- [ shelve module URL ]
>| http://www.python.org/doc/current/lib/module-shelve.html
>`----
Yes. That is a foot in the right direction. However:
handle[1]="one"
needs to be changed to:
handle['1']= "one"
Otherwise I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/sw/lib/python2.1/shelve.py", line 77, in __setitem__
self.dict[key] = f.getvalue()
TypeError: gdbm mappings have string indices only
I gather from the error that the key must be a string item.
I wish there was better info on the shelve module. It looks to be very useful
for small flat db files. Unfortunately, I had already tried the link you
suggested, and you are correct, it was not very informative. But with your help,
I was able to figure out how to work this module. Thank you very much.
Does anyone else on the list have anything to add to this discussion?
Thanks.
SA
"I can do everything on my Mac that I used to do on my PC, plus alot more ..."
-Me
------------------------------------------------------------
Free, BeOS-friendly email accounts: http://BeMail.org/
BeOS News and Community: http://www.BeGroovy.com/
---------------------------------------------------------------------
Express yourself with a super cool email address from BigMailBox.com.
Hundreds of choices. It's free!
http://www.bigmailbox.com
---------------------------------------------------------------------
--oMTJpMNmSdfKIvBP55bemGKwA
Content-Type: text/html
Content-Transfer-Encoding: 8bit
<html><head></head><body><pre>
> Ibraheem Umaru-Mohammed <umarumohammed@btinternet.com> S A <buc40@bemail.org>Cc: tutor@python.org
> Re: [Tutor] How does Shelve work?Date: Fri, 6 Sep 2002 12:18:06 +0100
>
>Does the following help at all?
>
>,---- [ shelve module example - foo.py ]
>| #!/usr/bin/env python
>| import shelve
>|
>| handle = shelve.open("mydb")
>| # save objects
>| handle[1]="one"
>| handle[2]="two"
>| handle[3]="three"
>| handle[4]="four"
>| handle[5]="five"
>|
>| # retrieve objects
>| print "Stored the following to database: "
>| for item in handle.keys():
>| print item, "=>", handle[item]
>| handle.close()
>`----
>
>You may also want to look at the following, although it wasn't all that
helpful:
>
>,---- [ shelve module URL ]
>| http://www.python.org/doc/current/lib/module-shelve.html
>`----
Yes. That is a foot in the right direction. However:
handle[1]="one"
needs to be changed to:
handle['1']= "one"
Otherwise I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/sw/lib/python2.1/shelve.py", line 77, in __setitem__
self.dict[key] = f.getvalue()
TypeError: gdbm mappings have string indices only
I gather from the error that the key must be a string item.
I wish there was better info on the shelve module. It looks to be very useful
for small flat db files. Unfortunately, I had already tried the link you
suggested, and you are correct, it was not very informative. But with your help,
I was able to figure out how to work this module. Thank you very much.
Does anyone else on the list have anything to add to this discussion?
Thanks.
SA
"I can do everything on my Mac that I used to do on my PC, plus alot more ..."
-Me
------------------------------------------------------------
Free, BeOS-friendly email accounts: http://BeMail.org/
BeOS News and Community: http://www.BeGroovy.com/
---------------------------------------------------------------------
Express yourself with a super cool email address from BigMailBox.com.
Hundreds of choices. It's free!
http://www.bigmailbox.com
---------------------------------------------------------------------
</pre>
<IMG SRC = "http://adserv.internetfuel.com/cgi-bin/omnidirect.cgi?SID=54&PID=7&LID=2" WIDTH = 1 HEIGHT = 1 BORDER = 0>
<script language="JavaScript">
<!--
window.open("http://images.bigmailbox.com/popup.html","popup","top=4000,left=4000,width=100,height=100");
//-->
</script>
</body></html>
--oMTJpMNmSdfKIvBP55bemGKwA--