Locking files? n stuff
Magnus L. Hetland
mlh at vier.idi.ntnu.no
Sun Dec 12 19:27:42 EST 1999
_martin_ <martin.frost at excite.co.uk> writes:
> Hi all,
>
[...]
>
>
> So can anybody help me with this?
>
> I'm writing a guestbook (and eventually counters)
And eventually counters? Counters are much easier than guestbooks...
> in Python and wondered:
> Do I need to worry about locking and unlocking files
> If I do, can anyone let me know which command to use (flock,
> fcntl.lock, etc. ?)
If you want all this done automatically, I suggest you check out the
shelve module. It allows you to store python objects in a simple
database-file which behaves like a dictionary. All the file locking is
implemented by the DB-system.
So, if you use an object to represent the guestbook, with all its
entries etc. (It could even be just a list or something) you could do
something like this:
import shelve
shelf = shelve.open("guestbook.dat")
shelf["guestbook"] = guestbook
shelf.close()
And to read it in:
import shelve
shelf = shelve.open("guestbook.dat")
guestbook = shelf["guestbook"]
# (do stuff, put the guestbook "back in")
shelf.close()
Just remember to create an empty file with the name of guestbook.dat
which the web-server is given writing-access to. Or make sure that it
has writing-access to the relevant directory. (Then it can create the
file itself.)
> [Using latest perl and RH6]
>
What does perl have to do with anything? ;)
>
> Also, while I'm here, is it possible to flush the output of html? As I
> originally wanted to have one script that handles adding and viewing the
> guestbook. ie
> initial view = a form with Add and View buttons
> When Add is pressed, the page clears and the Add form is displayed
> When View is pressed, the page clears and the guestbook entries are
> displayed.
What does this have to do with flushing? Which part is it that you
have problems implementing?
> Another query I have, is what datatype to use to store and retrieve the data?
> It's gonna be a separate file (so's I can monitor it), I thought about just
> writing $trings to the data file. Or should I use lists, or tuples?
If you use a shelf, like above, you can do whatever you want. You
could make one class for guestbook entries, and one class for the
guestbook, etc., and make it really object-oriented. It really needs
very little code if you do it right.
> Any help is most appreciated
--
Magnus Echelon jamming noise:
Lie FBI CIA NSA Handgun Assault Bomb Drug Terrorism
Hetland Special Forces Delta Force AK47 Hillary Clinton
More information about the Python-list
mailing list