[Tutor] Saving class instances

2009-07-13 Thread Thomas Scrace

Hi everyone,

I am new to Python (and to programming) and, now that I have worked  
through most of Learning Python, I have set myself the exercise of  
writing a little text-based program to catalogue the contents of my CD  
collection.


I have written enough code to allow me to create instances of an Album  
class with attributes like name, artist, year etc.  However, I am at a  
loss as to how to save these instances so that they can be retrieved  
the next time I run the program.  I assume I need to write them to a  
file somehow, and while I know how to write and read to and from  
files, I do not know how to write instances.


I am sure this has an obvious and easy answer but I just cannot find  
it anywhere!


If anybody could help I would be most grateful.

Tom
On 13 Jul 2009, at 14:44, tutor-requ...@python.org wrote:


Send Tutor mailing list submissions to
tutor@python.org

To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
tutor-requ...@python.org

You can reach the person managing the list at
tutor-ow...@python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."


Today's Topics:

  1. Re: for statement with addition ... (Rommel Asibal)
  2. Re: for statement with addition ... (Kent Johnson)
  3. Xampp & Python (Rommel Asibal)
  4. Re: thesaurus (Dave Angel)
  5. Re: for statement with addition ... (Dave Angel)
  6. Re: for statement with addition ... (Markus Hubig)


--

Message: 1
Date: Mon, 13 Jul 2009 05:22:28 -0600
From: Rommel Asibal 
To: Markus Hubig 
Cc: tutor@python.org
Subject: Re: [Tutor] for statement with addition ...
Message-ID:

Content-Type: text/plain; charset="iso-8859-1"

Markus,

That looks like a typo.  remove it and it should work.

On Mon, Jul 13, 2009 at 4:50 AM, Markus Hubig   
wrote:



Hi @all,

within diveintopython I often found a for-statement like this:

f for f in bla:
   print f

So what actually is the first f for ... is it just to declare f  
before

starting the for loop? I can't find any information on python.org
and it's hard to google this kinda stuff.

- Markus

--
---"it's like this"--
even samurai have teddy bears
and even teddy bears get drunk

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



-- next part --
An HTML attachment was scrubbed...
URL: 


--

Message: 2
Date: Mon, 13 Jul 2009 07:37:45 -0400
From: Kent Johnson 
To: Markus Hubig 
Cc: tutor@python.org
Subject: Re: [Tutor] for statement with addition ...
Message-ID:
<1c2a2c590907130437y4f19d357r811d0c956b53e...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

On Mon, Jul 13, 2009 at 6:50 AM, Markus Hubig wrote:

Hi @all,

within diveintopython I often found a for-statement like this:

f for f in bla:
??? print f

So what actually is the first f for ... is it just to declare f  
before

starting the for loop? I can't find any information on python.org
and it's hard to google this kinda stuff.


That is not Python, maybe a copy/paste artifact? Omit the initial 'f
'. Can you point to an example on the web site or in the printed book?

Kent


--

Message: 3
Date: Mon, 13 Jul 2009 05:56:46 -0600
From: Rommel Asibal 
To: Python Tutor List 
Subject: [Tutor] Xampp & Python
Message-ID:

Content-Type: text/plain; charset="iso-8859-1"

Hello All,

I am trying to go through the "Python Power!" book and i am at the  
part
where i need to setup a web server.  I am thinking of using XAMPP  
and have
checked its site and wanted to try using mod_python.  I am using  
Python 3.1
but from initial looks it seems mod_python doesnt support it, could  
someone
verify if that is correct?  Does that mean i have to go to python  
2.6 for it

to work?


Thanks!
-- next part --
An HTML attachment was scrubbed...
URL: 


--

Message: 4
Date: Mon, 13 Jul 2009 07:57:54 -0400
From: Dave Angel 
To: Pete Froslie 
Cc: Alan Gauld , tutor@python.org, Dave
Angel 
Subject: Re: [Tutor] thesaurus
Message-ID: <4a5b2142.7080...@ieee.org>
Content-Type: text/plain; charset=UTF-8; format=flowed

Pete Froslie wrote:

The url function btw:

def url():


  fin = open("journey_test.txt", "r")
  response = re.split(r"[/|/,\n, , ,:\"\"\.?,)(\-\<>\[\]'\r']",
fin.read())
  thesaurus = API_URL + response[word_number] + '/'  #API_URL is
established at the start of the code
  return thesaurus


yes. Essentially, it grab

Re: [Tutor] Saving class instances

2009-07-13 Thread Thomas Scrace
I think I will give both approaches a go, since this is just a  
learning exercise anyway.  Thanks very much for your help.


Oh, and sorry for accidentally quoting the whole digest last time.   
Won't happen again!


Tom

On 13 Jul 2009, at 17:53, tutor-requ...@python.org wrote:

I think you are better off using a database in this situation,  
sqlite3 is a

good choice since no extra setup is required. See
http://docs.python.org/library/sqlite3.html

And to answer your question, the python pickle module can save class
instances, see http://docs.python.org/library/pickle.html


Regards,
Daniel Woodhouse


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Saving class instances

2009-07-14 Thread Thomas Scrace


On 13 Jul 2009, at 22:04, "Alan Gauld"   
wrote:



That's one way and you can find an example and some advice
on how to handle subclassing in the OOP topic of my tutor.



Wow; thanks!  That tutorial  was really useful, I will have to check  
out the rest

of the site now.


I am sure this has an obvious and easy answer but I just cannot find
it anywhere!


Actually no. Saving and retrieving object instances (known as
persisting objects in OOP speak) was one of the biggest challenges
facing the OOP paradigm when it got started in the real world. Many
different solutions have evolved from flat text files to dedicated  
Object

databases, and everyting in between, but it remains a task frought
with difficulty, especially on large scale projects.



Good to know I wasn't being totally dense.  Now that I have got the  
pickle thing

under my belt I am going to have a go at sqllite.

Thanks again everybody.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor