Re: [Tutor] Saving class instances
"Thomas Scrace" wrote 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. Again, you might find the database topic ijn my tuorial a useful starting point for using SqlLite from Python... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Saving class instances
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
Re: [Tutor] Saving class instances
"Thomas Scrace" wrote 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, 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. You can also pickle them or even use shelve for random access retrieval later (see the pickle and shelve module docs) If you have lots of CDs - like thousands! - you might be better off with a database because then you can do sophisticated searches. But OTOH iTunes works pretty well just using a folder structure! :-) 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. For simpler programs things like Pickle and Shelve are probably the simplest and best solutions. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Saving class instances
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
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