Re: [Tutor] Saving class instances

2009-07-13 Thread Daniel Woodhouse
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

On Mon, Jul 13, 2009 at 5:21 PM, Thomas Scrace  wrote:

> 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
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is my style OK in this elementary student exercise?

2009-07-04 Thread Daniel Woodhouse
I had a look at your code, and refactored it a bit.  See my modifications at
http://python.pastebin.com/m50acb143 You'll noticed I removed one of your
functions completely (it was unnecessary) and the get_dict() function is not
so heavily nested.

Some ideas:
raw_input returns a string, so we can safely assume its a string. Whether or
not its a valid name for a student is a different matter, and if you wish to
check such things you may wish to look at the regular expression module
(re).  I wouldn't use eval as you did in the case, instead I did this to
check the score is a valid int:
try:
val = int(val)
except ValueError:
print 'Invalid score'

ValueError will catch invalid integers and we can ask the user to input
again.  Generally we should only capture Exceptions that we are expecting,
though there are exceptions to this (no pun intended)...
A lot of your code in get_dict() was repeated (basically the same operations
to get the key and the value). You should try to avoid duplication if
possible, either by putting the code in a seperate function, or just
collecting everything at once as I have done.

Regards,
Daniel Woodhouse


On Sat, Jul 4, 2009 at 3:26 PM, Angus Rodgers  wrote:

>
> Fear not, I won't post all my exercises to the Tutor list!  But
> when I seem to be doing something a little elaborate, it seems a
> good idea to ask if I'm doing anything silly.  In this exercise:
>
> <http://python.pastebin.com/d7e245e0f>   (retention: 1 day)
>
> my /conscious/ worries are:
>
>  (i) trapping the exception StandardError may, for all I know,
> be a daft thing to do;
>
>  (ii) I'm not bothering to be consistent in my use of single and
> double string quotes (but I don't know how much difference this
> makes, to readability and/or reliability);
>
> (iii) some of my lines of code extend beyond 79 characters (but
> again, I don't know how much this matters);
>
>  (iv) correlatively with (iii), my indentation perhaps looks a
> little extreme (perhaps suggesting a redesign, I don't know);
>
>  (v) I'm bound to be doing other silly things that I don't even
> know about ("unknown unknowns").
>
> Any comments? (I don't expect the Spanish Inquisition!)
>
> --
> Angus Rodgers
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Needing Help

2009-06-30 Thread Daniel Woodhouse
You can use something like pastebin.com if you don't want to post huge
chunks of code.  You could also just tell us what problem occurred, was
there a traceback (error message)?

On Tue, Jun 30, 2009 at 8:06 PM, Bob Rea  wrote:

> I am jsut beginning to learn python form a book. I have run
> into a problem running a script from the book. I want to
> ask for help. Shoudl I put the whole script into my email
> or put it somewhere on the web for you to look at instead.
> Not sure how this works, script is 68 lines.
>
> --
> Bob Rea
> mailto:gapet...@stsams.org
> http://www.petard.us
> http://www.petard.us/blog
> http://www.petard.us/gallery
>
> Where is Bill Stringfellow
>now that we really need him?
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor