Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28385
| Message-ID | <1454469.9HWcpyvaek@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2012-09-04 04:25 +0200 |
| Subject | Re: simple client data base |
| Newsgroups | comp.lang.python |
| References | <lae9489ct99mp704um93sdqlatofb2i8gq@4ax.com> |
| Followup-To | comp.lang.python |
Followups directed to: comp.lang.python
Mark R Rivet wrote:
> Hello all, I am learning to program in python. I have a need to make a
> program that can store, retrieve, add, and delete client data such as
> name, address, social, telephone number and similar information. This
> would be a small client database for my wife who has a home accounting
> business.
>
> I have been reading about lists, tuples, and dictionary data
> structures in python and I am confused as to which would be more
> appropriate for a simple database.
>
> I know that python has real database capabilities but I'm not there
> yet and would like to proceed with as simple a structure as possible.
>
> Can anyone give me some idea's or tell me which structure would be
> best to use?
>
> Maybe its a combination of structures? I need some help.
The data types that would choose are defined by your requirements and how
well a data type meets your requirements.
I can imagine: In a database you would want quick access to data. You would
want to access fields primarily by name. You would also want to filter data
by various criteria.
Therefore, it appears to me that it would be best if your records were
dictionaries or dictionary-like objects, and your recordsets were lists of
records, like so
#!/usr/bin/env python3
from datetime import date
data = [
{
'lastname': 'Doe',
'firstname': 'John',
'sn': '123-451-671-890',
'birthdata': date(2000, 1, 2)
},
{
'lastname': 'Doe',
'firstname': 'Jane',
'sn': '409-212-582-452',
'birthdata': date(2001, 2, 3)
},
]
- You could quickly access the second record with data[1].
- You could access the 'lastname' field of the second record with
data[1]['lastname']
- You could get a list of records where the person is born before 2001 CE
with filter(lambda record: record['birthdate'] < date(2001, 1, 1), data)
The advantage of dictionaries over dictionary-like objects is that they are
easily extensible and that the memory footprint is probably lower (CMIIW);
the disadvantage is slightly more complicated syntax and that you have to
keep track of the keys. Therefore, you might want to consider instantiating
a Record class instead; in its simplest form:
class Record(object):
def __init__(self, lastname, firstname, sn=None, birthdate=None):
self.lastname = lastname
self.firstname = firstname
self.sn = str(sn)
self.birthdate = birthdate
data = [
Record(lastname='Doe', firstname='John', sn='123-451-671-890',
birthdate=date(2000, 1, 2)),
Record(lastname='Doe', firstname='Jane', sn='409-212-582-452',
birthdate=date(2001, 2, 3))
]
- You could access the 'lastname' property of the second record with
data[1].lastname
- You get a list of records where the person is born before 2001 CE with
list(filter(lambda record: record.birthdate < date(2001, 1, 1), data))
(in Python 2.x without list())
However, if you want your program to manipulate the data *persistently*. as
it will probably be needed for business, you will need to also store it
somewhere else than in the volatile memory in which these data structures
are usually stored. The most simple way would be to store and parse the
string representation of the objects.
Production-quality implementations of those and other concepts already
exist, of course, but using something finished and polished does not provide
as much learning experience.
HTH
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
simple client data base Mark R Rivet <markrrivet@aol.com> - 2012-09-03 10:12 -0400
Re: simple client data base "Martin P. Hellwig" <martin.hellwig@gmail.com> - 2012-09-03 07:27 -0700
Re: simple client data base Chris Angelico <rosuav@gmail.com> - 2012-09-04 00:28 +1000
Re: simple client data base Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-03 15:37 +0100
Re: simple client data base Peter Otten <__peter__@web.de> - 2012-09-03 16:50 +0200
Re: simple client data base Mark R Rivet <markrrivet@aol.com> - 2012-09-08 15:22 -0400
Re: simple client data base Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-08 17:42 -0400
Re: simple client data base Wolfgang Keller <feliphil@gmx.net> - 2012-09-03 18:03 +0200
Re: simple client data base Walter Hurry <walterhurry@lavabit.com> - 2012-09-03 19:02 +0000
Re: simple client data base Wolfgang Keller <feliphil@gmx.net> - 2012-09-04 15:17 +0200
Re: simple client data base Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-04 04:25 +0200
Re: simple client data base Mark R Rivet <markrrivet@aol.com> - 2012-09-08 15:40 -0400
Re: simple client data base Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-11 13:28 +0200
Re: simple client data base Ramchandra Apte <maniandram01@gmail.com> - 2012-09-05 05:57 -0700
Re: simple client data base Mark R Rivet <markrrivet@aol.com> - 2012-09-08 15:42 -0400
Re: simple client data base Bryan <bryanjugglercryptographer@yahoo.com> - 2012-09-06 01:57 -0700
Re: simple client data base Mark R Rivet <markrrivet@aol.com> - 2012-09-08 15:47 -0400
Re: simple client data base Jason Friedman <jason@powerpull.net> - 2012-09-08 14:05 -0600
Re: simple client data base Paul Rubin <no.email@nospam.invalid> - 2012-09-08 13:11 -0700
Re: simple client data base Ian W <mymixedmess@gmail.com> - 2012-09-08 15:32 -0500
Re: simple client data base Walter Hurry <walterhurry@lavabit.com> - 2012-09-08 20:39 +0000
Re: simple client data base Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-09-08 21:03 +0000
Re: simple client data base Bryan <bryanjugglercryptographer@yahoo.com> - 2012-09-09 09:06 -0700
Re: simple client data base Tim Chase <python.list@tim.thechases.com> - 2012-09-09 13:27 -0500
Re: simple client data base Paul Rubin <no.email@nospam.invalid> - 2012-09-09 11:55 -0700
csiph-web