473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

OO question

I want to make an addressbook and I'm new to OO programming, so I
wonder if this sounds reasonable.

I think of making a class Address which contains all data about one
person, that class can have UserDict as baseclass so I can access data
like object['name'], etc..
Then maybe I can have a class AddressBook which has a list that
contains all those Address objects.
Does that sound reasonable?
And then the real reason that I posted here is:
If I want to save all addresses to disk, I can have a method, say,
save() of AddressBook. But then what? What is a good object oriented
approach? Should each Address object take care of saving itself to the
file, with a method like writetofile(filename), or should the class
AddressBook take care of the saving and ask each object for its data?

Thanks!

Jan 1 '07 #1
11 1029
On Sun, 31 Dec 2006 19:47:12 -0800, fejkadress wrote:
I want to make an addressbook and I'm new to OO programming, so I
wonder if this sounds reasonable.

I think of making a class Address which contains all data about one
person, that class can have UserDict as baseclass so I can access data
like object['name'], etc..
If you are using Python 2.2 or greater, you can inherit from dict:

>>class Address(dict):
.... pass
....
>>fred = Address({"address": "123 smith street"})
fred['address']
'123 smith street'
But ask yourself, what benefit do you gain by subclassing dict? Why not
just use a dict?

If this is a learning exercise, then sure, go for it, subclass away! But
if this is meant to be real code, then consider what benefits and costs
using OO will give you, compared to a function-based approach.

(Please don't assume that this is a thinly veiled hint that OO is the
wrong approach. It isn't.)

Then maybe I can have a class AddressBook which has a list that
contains all those Address objects.
Does that sound reasonable?
Again, why create an AddressBook class? Why not just have a list of
Addresses?
And then the real reason that I posted here is:
If I want to save all addresses to disk, I can have a method, say,
save() of AddressBook. But then what? What is a good object oriented
approach? Should each Address object take care of saving itself to the
file, with a method like writetofile(filename), or should the class
AddressBook take care of the saving and ask each object for its data?


Here's one approach, assuming that AddressBook is merely a list of
Addresses and that the Address class knows how to write to an open file
object:

def save(list_of_addresses, filename):
f = file(filename, "w")
for address in list_of_addresses:
address.save(f)
f.close()

Here's a more pure OO approach to do the same thing:

class AddressBook(object):
def __init__(self, list_of_addresses):
self.addresses = list_of_addresses

def save(self, filename):
f = file(filename, "w")
for address in self.addresses:
address.save(f)
f.close()

Here's a third approach:

class AddressBook(object):
def __init__(self, list_of_addresses):
self.addresses = list_of_addresses
self.currentfile = None

def save_one_address(self, data):
data = do_something_with(data)
self.currentfile.write(data)

def save(self, filename):
self.currentfile = file(filename, "w")
for address in self.addresses:
self.save_one_address(address.export())
self.currentfile.close()
self.currentfile = None
--
Steven.

Jan 1 '07 #2

fe********@hushmail.com wrote:
I want to make an addressbook and I'm new to OO programming, so I
wonder if this sounds reasonable.

I think of making a class Address which contains all data about one
person, that class can have UserDict as baseclass so I can access data
In Python 2.4 or 2.5 you can subclass dict.
like object['name'], etc..
Then maybe I can have a class AddressBook which has a list that
contains all those Address objects.
Does that sound reasonable?
Yep.

class AdressBook(list):
def save(self, filename):
import pickle
...

And then the real reason that I posted here is:
If I want to save all addresses to disk, I can have a method, say,
save() of AddressBook. But then what? What is a good object oriented
approach? Should each Address object take care of saving itself to the
file, with a method like writetofile(filename), or should the class
AddressBook take care of the saving and ask each object for its data?
If you use the pickle module on the Addressbook then it will
automatically
save each Address instance.

- Paddy.

Jan 1 '07 #3

Steven D'Aprano wrote:
On Sun, 31 Dec 2006 19:47:12 -0800, fejkadress wrote:
I want to make an addressbook and I'm new to OO programming, so I
wonder if this sounds reasonable.

I think of making a class Address which contains all data about one
person, that class can have UserDict as baseclass so I can access data
like object['name'], etc..

If you are using Python 2.2 or greater, you can inherit from dict:

>class Address(dict):
... pass
...
>fred = Address({"address": "123 smith street"})
fred['address']
'123 smith street'
But ask yourself, what benefit do you gain by subclassing dict? Why not
just use a dict?

If this is a learning exercise, then sure, go for it, subclass away! But
if this is meant to be real code, then consider what benefits and costs
using OO will give you, compared to a function-based approach.

(Please don't assume that this is a thinly veiled hint that OO is the
wrong approach. It isn't.)

Then maybe I can have a class AddressBook which has a list that
contains all those Address objects.
Does that sound reasonable?

Again, why create an AddressBook class? Why not just have a list of
Addresses?
And then the real reason that I posted here is:
If I want to save all addresses to disk, I can have a method, say,
save() of AddressBook. But then what? What is a good object oriented
approach? Should each Address object take care of saving itself to the
file, with a method like writetofile(filename), or should the class
AddressBook take care of the saving and ask each object for its data?

Here's one approach, assuming that AddressBook is merely a list of
Addresses and that the Address class knows how to write to an open file
object:

def save(list_of_addresses, filename):
f = file(filename, "w")
for address in list_of_addresses:
address.save(f)
f.close()

Here's a more pure OO approach to do the same thing:

class AddressBook(object):
def __init__(self, list_of_addresses):
self.addresses = list_of_addresses

def save(self, filename):
f = file(filename, "w")
for address in self.addresses:
address.save(f)
f.close()

Here's a third approach:

class AddressBook(object):
def __init__(self, list_of_addresses):
self.addresses = list_of_addresses
self.currentfile = None

def save_one_address(self, data):
data = do_something_with(data)
self.currentfile.write(data)

def save(self, filename):
self.currentfile = file(filename, "w")
for address in self.addresses:
self.save_one_address(address.export())
self.currentfile.close()
self.currentfile = None
--
Steven.
Small addition.
If a class does a save then it should also do the load of the data too.

Jan 1 '07 #4
fe********@hushmail.com wrote:
I want to make an addressbook and I'm new to OO programming, so I
wonder if this sounds reasonable.

I think of making a class Address which contains all data about one
person, that class can have UserDict as baseclass so I can access data
like object['name'], etc..
Stop right there. Class Address contains all data about one *person*?
Please consider renaming that class Person, and having a separate class
Address.

Interruption for a reality check: A person may be related to zero, one
or many addresses. An address may be related to zero, one or many
persons. The relationship may take many forms e.g. "lives at", "once
lived at", "has been noticed loitering outside", ...

OK, now you can resume your normal programming ;-)

BTW, why object['name'] and not object.name?

Cheers,
John

Jan 1 '07 #5
On Sun, 31 Dec 2006 22:00:58 -0800, Paddy wrote:
> def save(self, filename):
self.currentfile = file(filename, "w")
for address in self.addresses:
self.save_one_address(address.export())
self.currentfile.close()
self.currentfile = None

Small addition.
If a class does a save then it should also do the load of the data too.
What, you've never heard of a write-only file?

--
Steven.

Jan 1 '07 #6
fe********@hushmail.com wrote:
If I want to save all addresses to disk, I can have a method, say,
save() of AddressBook. But then what? What is a good object oriented
approach? Should each Address object take care of saving itself to the
file, with a method like writetofile(filename), or should the class
AddressBook take care of the saving and ask each object for its data?
The OO religion likes to provide a big ego to objects. And be aware! All these egos then want to itch the programmer with question marks for years! And there are programming languages which draw more attention to what some unsolicited objects want, than to what the programmer wants. Python is not so.

Remember, you are the boss. You say: "I want to save all addresses to disk". I'd say: Just dump them to disk unless you feel a functional need in order to make it more indirect.
Robert
Jan 1 '07 #7
First I want to say thanks everyone for helping me!

John Machin wrote:
fe********@hushmail.com wrote:
I want to make an addressbook and I'm new to OO programming, so I
wonder if this sounds reasonable.

I think of making a class Address which contains all data about one
person, that class can have UserDict as baseclass so I can access data
like object['name'], etc..

Stop right there. Class Address contains all data about one *person*?
Please consider renaming that class Person, and having a separate class
Address.

Interruption for a reality check: A person may be related to zero, one
or many addresses. An address may be related to zero, one or many
persons. The relationship may take many forms e.g. "lives at", "once
lived at", "has been noticed loitering outside", ...
Lets say I have those two classes, Person and Address. How would one
implement the relationship between them? First, a Person can have one
or more addresses (or none), that could be represented as a list of
Addresses, right? But then, if I have an Address I want to see which
persons who live there, then I would have a list of Persons in each
Address.

Is this doubly-linked way of doing it a good way of doing it, or is
there
a better "OO way" I haven't learned yet?

Jan 2 '07 #8
On Jan 1, 8:09 pm, fejkadr...@hushmail.com wrote:
Lets say I have those two classes, Person and Address. How would one
implement the relationship between them? First, a Person can have one
or more addresses (or none), that could be represented as a list of
Addresses, right? But then, if I have an Address I want to see which
persons who live there, then I would have a list of Persons in each
Address.

Is this doubly-linked way of doing it a good way of doing it, or is
there
a better "OO way" I haven't learned yet?
Since you are making an address book and not a person book I would
start out by making a simple list of addresses. Each address could have
a "person" or a "name" field. You could then sort your list by the name
of each person associated with each address. If an address has no
person associated with it then you put it at the beginning (or end if
you want) of the list. I think this would most closely mimic how a
rolodex works. It doesn't really matter if you implement it as a list
of dictionaries or a list classes I believe they would be equivalent in
this case.

tim

Jan 2 '07 #9

Steven D'Aprano wrote:
On Sun, 31 Dec 2006 22:00:58 -0800, Paddy wrote:
def save(self, filename):
self.currentfile = file(filename, "w")
for address in self.addresses:
self.save_one_address(address.export())
self.currentfile.close()
self.currentfile = None
Small addition.
If a class does a save then it should also do the load of the data too.

What, you've never heard of a write-only file?
Ahh /dev/null ;-)

- Paddy.

Jan 2 '07 #10
On 1 Jan 2007 17:09:19 -0800,
fe********@hushmail.com wrote:
Lets say I have those two classes, Person and Address. How would one
implement the relationship between them? First, a Person can have one
or more addresses (or none), that could be represented as a list of
Addresses, right? But then, if I have an Address I want to see which
persons who live there, then I would have a list of Persons in each
Address.
Is this doubly-linked way of doing it a good way of doing it, or is
there a better "OO way" I haven't learned yet?
I don't know about "a better OO" way, but one way is to model the
various relationships as a list of (person, address) pairs:

persons = [ Person( ), Person( ), Person( ) ]
addresses = [ Address( ), Address( ), Address( ), Address( ) ]

livesat = ((person[ 1 ], address[ 1 ]),
(person[ 2 ], address[ 1 ]),
(person[ 2 ], address[ 2 ]))

worksat = ((person[ 1 ], address[ 3 ]),
(person[ 2 ], address[ 4 ]))

Then the list of people who live at address[ 1 ] is:

[ (person, address) for (p, a) in livesat if a is address[ 1 ] ]

But what about all the various relationships (lives at, works at, is
known to have vandalized, etc.)?

livesat = Relationship( "lives at" )
worksat = Relationship( "works at" )
vandalized = Relationship( "vandalized" )

information = ((person[ 1 ], address[ 1 ], livesat),
(person[ 2 ], address[ 1 ], livesat),
(person[ 2 ], address[ 2 ], livesat),
(person[ 1 ], address[ 3 ], worksat),
(person[ 2 ], address[ 4 ], worksat),
(person[ 1 ], address[ 4 ], vandalized))

Now the list of people who live at address[ 1 ] is:

[ (person, address) for (p, a, r) in information
if a is address[ 1 ] and r is livesat ]

(There are other ways, too, such as capturing one person and one address
right inside a Relationship; someone with a more comp-sci-theoretical
background than I have can probably tell you why or when one way might
be better than the other.)

The definitions of Person, Address, and Relationship, as well as the
user-interface(s) and the persistent storage mechanism(s), are left as
exercises to the interested reader. ;-)

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
Jan 2 '07 #11
fe********@hushmail.com a écrit :
First I want to say thanks everyone for helping me!

John Machin wrote:

>>fe********@hushmail.com wrote:
>>>I want to make an addressbook and I'm new to OO programming, so I
wonder if this sounds reasonable.

I think of making a class Address which contains all data about one
person, that class can have UserDict as baseclass so I can access data
like object['name'], etc..

Stop right there. Class Address contains all data about one *person*?
Please consider renaming that class Person, and having a separate class
Address.

Interruption for a reality check: A person may be related to zero, one
or many addresses. An address may be related to zero, one or many
persons. The relationship may take many forms e.g. "lives at", "once
lived at", "has been noticed loitering outside", ...


Lets say I have those two classes, Person and Address. How would one
implement the relationship between them? First, a Person can have one
or more addresses (or none), that could be represented as a list of
Addresses, right? But then, if I have an Address I want to see which
persons who live there, then I would have a list of Persons in each
Address.

Is this doubly-linked way of doing it a good way of doing it, or is
there a better "OO way" I haven't learned yet?
The most common solution so far for this kind of problems is to forget
about "domain model" OO modeling and switch to relational modeling
(usually using an SQL dbms). Else you end up rewriting an half-backed
buggy ad-hoc relational system... FWIW, using OO does not imply using an
OO domain model. You can restrict OO features to "technical" objects...

Now the good news is that there are Python packages like SQLAlchemy that
gives you kind of "best of both world" solutions (OO domain model +
relational support).

My 2 cents...
Jan 2 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.