[Tutor] Saving class instances

Thomas Scrace t.scrace at gmail.com
Mon Jul 13 16:21:19 CEST 2009


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-request at python.org wrote:

> Send Tutor mailing list submissions to
> 	tutor at 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-request at python.org
>
> You can reach the person managing the list at
> 	tutor-owner at 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 <rumzster at gmail.com>
> To: Markus Hubig <mhubig at gmail.com>
> Cc: tutor at python.org
> Subject: Re: [Tutor] for statement with addition ...
> Message-ID:
> 	<de6a4e6f0907130422x2e1e5bf6n3fcfff4d4739e579 at mail.gmail.com>
> 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 <mhubig at gmail.com>  
> 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 at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.python.org/pipermail/tutor/attachments/20090713/ad0174f6/attachment-0001.htm 
> >
>
> ------------------------------
>
> Message: 2
> Date: Mon, 13 Jul 2009 07:37:45 -0400
> From: Kent Johnson <kent37 at tds.net>
> To: Markus Hubig <mhubig at gmail.com>
> Cc: tutor at python.org
> Subject: Re: [Tutor] for statement with addition ...
> Message-ID:
> 	<1c2a2c590907130437y4f19d357r811d0c956b53e085 at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> On Mon, Jul 13, 2009 at 6:50 AM, Markus Hubig<mhubig at gmail.com> 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 <rumzster at gmail.com>
> To: Python Tutor List <tutor at python.org>
> Subject: [Tutor] Xampp & Python
> Message-ID:
> 	<de6a4e6f0907130456t491ea967i40ab083aebdda88d at mail.gmail.com>
> 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: <http://mail.python.org/pipermail/tutor/attachments/20090713/5925e053/attachment-0001.htm 
> >
>
> ------------------------------
>
> Message: 4
> Date: Mon, 13 Jul 2009 07:57:54 -0400
> From: Dave Angel <davea at ieee.org>
> To: Pete Froslie <froslie at gmail.com>
> Cc: Alan Gauld <alan.gauld at btinternet.com>, tutor at python.org,	Dave
> 	Angel <davea at ieee.org>
> Subject: Re: [Tutor] thesaurus
> Message-ID: <4A5B2142.7080206 at 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 grabs each word from a text file and combines  
>> it with
>> the other stuff to create a url string that I send through an API  
>> to an
>> online thesaurus. I was attempting to strip it this way as a weak  
>> method for
>> cleaning out the words I don't want searched from the text file.
>>
>> Along with the the other functions the code currently scans a text  
>> file and
>> replaces each of its words with one of their synonyms.. slightly  
>> legible
>> gibberish, but that's what I'm interested in for now. It is a  
>> project to
>> help me start learning pyhton as well being intended to take a  
>> different
>> form in an artwork I'm working on.
>>
>> thanks.
>> petef
>>
>>
> BTW, constants like API_URL are fine uses for globals.
>
> I can't get my head around this url() function definition, or how it
> would be called.  It appears that your program looks like:
>
> Open the file and find out how many words are in it.
> for each word,
>    Open the file again, parse the whole thing yet again, to figure out
> the nth word.  Then throw away the parsing, plus any information about
> where in the file the word was found, just keeping a generated URL
>    use the URL to access a website, looking up a replacement word.
>    Re-open the file, parse it till you find something resembling the
> word involved, then substitute the synonym, and write it back out.
>
>
> So if you have a file with 1000 words in it, you'll open it and  
> parse it
> 2001 times.  And part of your symptoms result from the second parsing
> trying to replace words that aren't really words.
>
> If I were you, I'd simplify the problem, and solve the simplified
> problem in a pretty way, keeping in mind what the complete problem is.
> Then expand it incrementally from there.
>
> For example, instead of a website, you can build a map of synonyms  
> (see
> my example earlier).  Just a dozen should be fine for testing.
> Instead of a file with arbitrary text, you could use a simple string  
> of
> words, without punctuation, capitalization or other complexities.
>
> Now, write a set of functions that solve that problem, and gradually  
> add
> in the complexities of the original problem.
>
>
> And try to do each operation once, both for efficiency and to avoid
> errors when it's slightly different the two times you might do it.    
> To
> me that means the final program should have this kind of flow:
>
> Open the file, and start parsing.
>      if the next sequence is punctuation and white space, copy it to
> the output.
>      if the next sequence is a word, extract it, call synonym() on it,
> and copy that to the output
>     continue till the infile is done
> Perhaps copy the outfile on top of the infile.
>
> See the suggestions for functions I made much earlier in this thread.
>
>
> DaveA
>
>
> ------------------------------
>
> Message: 5
> Date: Mon, 13 Jul 2009 08:08:22 -0400
> From: Dave Angel <davea at ieee.org>
> To: Markus Hubig <mhubig at gmail.com>
> Cc: tutor at python.org
> Subject: Re: [Tutor] for statement with addition ...
> Message-ID: <4A5B23B6.7030707 at ieee.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> 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
>>
>>
> Please give us a reference, as to exactly where you saw that.  It  
> gives
> a syntax error in Python 2.62, as I expected it would.  That's not  
> how a
> for statement works.
>
>
> On the other hand, if you enclose that phrase in square brackets, it
> makes a list comprehension.
>
> mylist = [f for f in bla]
>
> A list comprehension builds a new list, where the first pieces says  
> what
> goes into the list, and the second part describes how to generate it.
> In this case, if bla is already a list, this just copies the list.   
> But
> consider
> mylist = [f*f  for f in bla]
>
> That builds a new list from the old, where each item is the square of
> the corresponding old item.
>
> List comprehension is something you can look up in the help.   Also  
> look
> up generator expression, which uses similar syntax.
>
>
> DaveA
>
>
> ------------------------------
>
> Message: 6
> Date: Mon, 13 Jul 2009 15:44:09 +0200
> From: Markus Hubig <mhubig at gmail.com>
> To: tutor at python.org
> Subject: Re: [Tutor] for statement with addition ...
> Message-ID:
> 	<1a3190fd0907130644n7ac6be52y5e672abdfbbf9088 at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> On Mon, Jul 13, 2009 at 2:08 PM, Dave Angel <davea at ieee.org> wrote:
>
>> 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
>>>
>>>
>>>
>> Please give us a reference, as to exactly where you saw that.  It  
>> gives a
>> syntax error in Python 2.62, as I expected it would.  That's not  
>> how a for
>> statement works.
>>
>>
>> On the other hand, if you enclose that phrase in square brackets,  
>> it makes
>> a list comprehension.
>>
>> mylist = [f for f in bla]
>>
>> A list comprehension builds a new list, where the first pieces says  
>> what
>> goes into the list, and the second part describes how to generate  
>> it.  In
>> this case, if bla is already a list, this just copies the list.  But
>> consider
>> mylist = [f*f  for f in bla]
>>
>> That builds a new list from the old, where each item is the square  
>> of the
>> corresponding old item.
>>
>> List comprehension is something you can look up in the help.   Also  
>> look up
>> generator expression, which uses similar syntax.
>>
>>
>> DaveA
>>
>
> Yes your right, the examples are enclosed in square brackets ...
> so this is the explanation I was looking for, thank you.
> And now I also found the explanation in
> diveintopython<http://diveintopython.org/native_data_types/mapping_lists.html 
> >!
> ;-)
>
> - Markus
>
> -- 
> -------------------"it's like this"------
> even samurai have teddy bears
> and even teddy bears get drunk
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.python.org/pipermail/tutor/attachments/20090713/88308fba/attachment.htm 
> >
>
> ------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 65, Issue 56
> *************************************



More information about the Tutor mailing list