Flat file database

A

Art Decco

Is there any Python module designed to simplify the use of a plain text file
as a flat file database?

I realize that's a bit vague, but I'm just wondering about the best approach
for creating a little database-backed CGI app using Python on a server with
no real database available. The server belongs to the ISP, and I don't get
to install "real" software, but they do have a cgi-bin directory for me, and
they do have a relatively recent Python, so I've got Python cgi apps working
via "#!/usr/local/bin/python". I can get the apps to write and read plain
text files right in the cgi-bin directory, too, so I can use that as a
simple, flat file database.

Now, the question is, do I write all the database
access/update/sort/search/etc. features myself, or is there some Python
module that has implemented some useful functions that I should build on.
I'm pretty new to Python (though not to programming), so if there's a
standard way most skilled Pythonistas would approach something like this,
I'd like to know what it is. And if there's something that goes beyond flat
file and has some relational support as well, all the better.

Thanks for any suggestions.
 
P

Paul Rubin

Art Decco said:
I realize that's a bit vague, but I'm just wondering about the best approach
for creating a little database-backed CGI app using Python on a server with
no real database available. The server belongs to the ISP, and I don't get
to install "real" software, but they do have a cgi-bin directory for me, and
they do have a relatively recent Python, so I've got Python cgi apps working
via "#!/usr/local/bin/python". I can get the apps to write and read plain
text files right in the cgi-bin directory, too, so I can use that as a
simple, flat file database.

Most web hosts offer you access to a database, typically MySQL. Use
that if you can. Implementing what you're asking is harder than it
sounds. What happens if two people connect to your application both
try to update the database at the same time?
 
A

Art Decco

Paul Rubin said:
Most web hosts offer you access to a database, typically MySQL. Use
that if you can. Implementing what you're asking is harder than it
sounds. What happens if two people connect to your application both
try to update the database at the same time?

Actually, I've done it before in Perl. Perl makes multithreaded access to
files pretty easy (on Unix).

But this case is much easier. It's just for personal use. I'll probably be
the only user, so a minimal backup is all I need to do for data integrity. I
don't need record locking or even file locking. Of course that may make my
case so unusual that no module exists, but it's worth asking before I once
again reinvent the concept of a simple flat-file database as I, and most
other programmers, have done so many times before.

And I don't think MySQL is available without switching to a more expensive
type of account. This application, essentially a fancy Christmas card list,
just isn't worth all that trouble, but it would be nice if someone knows of
a Python module that makes simple text file management a little easier.
 
R

remco

Art Decco said:
Is there any Python module designed to simplify the use of a plain text file
as a flat file database?

I realize that's a bit vague, but I'm just wondering about the best approach
for creating a little database-backed CGI app using Python on a server with
no real database available. The server belongs to the ISP, and I don't get
to install "real" software, but they do have a cgi-bin directory for me, and
they do have a relatively recent Python, so I've got Python cgi apps working
via "#!/usr/local/bin/python". I can get the apps to write and read plain
text files right in the cgi-bin directory, too, so I can use that as a
simple, flat file database.

Now, the question is, do I write all the database
access/update/sort/search/etc. features myself, or is there some Python
module that has implemented some useful functions that I should build on.
I'm pretty new to Python (though not to programming), so if there's a
standard way most skilled Pythonistas would approach something like this,
I'd like to know what it is. And if there's something that goes beyond flat
file and has some relational support as well, all the better.

Thanks for any suggestions.

You could see if the Berkley db is available (bsddb module).
The Berkley DB is normally builtin. It behaves like a dictionary, and is
therefore
simple and straightforward.

Cheers,
Remco Boerma
 
P

Paul Rubin

Art Decco said:
And I don't think MySQL is available without switching to a more expensive
type of account. This application, essentially a fancy Christmas card list,
just isn't worth all that trouble, but it would be nice if someone knows of
a Python module that makes simple text file management a little easier.

I don't understand then why you want such a module. If there's just a
few dozen lines in the file, then read it into memory, change whatever
you want, and write it out again.

Personally for these kinds of database applications though, I usually
use the anydbm module.
 
A

Aahz

Is there any Python module designed to simplify the use of a plain text file
as a flat file database?

Why do you want plain-text? I've got a half-assed module for searching
a formatted plain-text database, but the whole point of it is that it
does *not* have a write/update interface -- it's designed to be
maintained with a text editor. For your purpose, why not just pickle a
Python dict?
 
S

Skip Montanaro

Art> Is there any Python module designed to simplify the use of a plain
Art> text file as a flat file database?

Several others have mentioned various possibilities. If Python
2.3 is available to you, you might consider the csv module. You can edit
the files with Excel or another spreadsheet. In your code, you can use the
csv.DictReader class to pull out the fields of interest:

# just off the top of my head!
def select(where, csvfile):
"return rows from csvfile which overlap with dictionary 'where'."

results = []
for row in csv.DictReader(csvfile):
if matches(where, row):
results.append(row)
return results

Coding of the matches function is left as an exercise for the reader. <wink>

Skip
 
J

John Roth

Art Decco said:
Is there any Python module designed to simplify the use of a plain text file
as a flat file database?

I realize that's a bit vague, but I'm just wondering about the best approach
for creating a little database-backed CGI app using Python on a server with
no real database available. The server belongs to the ISP, and I don't get
to install "real" software, but they do have a cgi-bin directory for me, and
they do have a relatively recent Python, so I've got Python cgi apps working
via "#!/usr/local/bin/python". I can get the apps to write and read plain
text files right in the cgi-bin directory, too, so I can use that as a
simple, flat file database.

Python 2.3 has a CSV module to read and write files in Comma
Separated Variable format. This might do the job for you if you can
stand to read the file and convert it to in-storage objects, and then write
out a new version if it's updated.

Another possibility along the same lines is the pickle module: that can
put out an entire tree of objects, and then read them back in. That's
been around for a while. That will be availible for any release of
Python that your ISP is likely to have installed.

As far as relational support, I'd suggest hitting Google to see what
you can turn up.

John Roth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top