[Tutor] associating two objects without ORM and processing a text file
neubyr
neubyr at gmail.com
Tue Feb 12 02:33:04 CET 2013
On Mon, Feb 11, 2013 at 6:58 PM, Steven D'Aprano <steve at pearwood.info>wrote:
> On 11/02/13 16:14, neubyr wrote:
>
>> I have a text file with each line in following format:
>>
>> Book Name, Author Name, Genre, Publication Date
>>
>> I would like to perform following queries on this file:
>> * Get all books written by an author
>> * Remove all books of an author
>> * Get information about a book (pretty print matching line!)
>> * Get books of particular genre
>>
>> Also, I would like to add and delete entries in this file. I am not
>> planning to use any database for this purpose and would like to get better
>> grasp on file parsing and classes/OOP. I need some help in creating
>> classes
>> and following are my initial thoughts:
>>
>> # Create a class for Book object
>> class Book:
>> atributes: name, author_name, genre, publication-date
>>
>
>
> You could use a class. But since Books don't have any behaviour, a simple
> struct or record would be better than a class:
>
>
> from collections import namedtuple
> Book = namedtuple("Book", "name author genre date")
>
> lotr = Book("The Hobbit", "J.R.R. Tolkien", "Fantasy", "1937")
>
>
> This has the advantage of simplicity. But if you need to add behaviour to
> the
> Book class, e.g. validation of the fields, you should be able to inherit
> from
> a named tuple. Untested:
>
>
> class Book(namedtuple("Book", "name author genre date")):
> @property
> def genre(self):
> return super(Book, self).genre
> @genre.setter(self, value):
> super(Book, self).genre = value.title() # 'fantasy' -> 'Fantasy'
>
>
>
> # Create
>> Author:
>> attribute(s): name
>>
>
>
> As Alan suggested, a waste of time. Since the Author has no behaviour and
> only a single field, why not just use a string?
>
>
>
>
> # Create class for reading and writing to the file
>> class Booksfile:
>> methods: ??
>>
>
> Why should this be a class? This is not Java.
>
> http://steve-yegge.blogspot.**com.au/2006/03/execution-in-**
> kingdom-of-nouns.html<http://steve-yegge.blogspot.com.au/2006/03/execution-in-kingdom-of-nouns.html>
>
>
> Just write a function that reads a file and returns a list of Books.
>
> Or perhaps I should say:
>
>
> Programmer().getwriter().**write(MakeCallable(FileReader)**
> .setmethod("read",
> return_type=list, return_item_values=Book)
>
>
>
>
> * How do I associate/relate Book and Author classes so that it will help
>> me
>> in getting information like 'get list of books written by an author'? Data
>> attribute?
>>
>
> You want to map authors to books. Whenever you want a mapping, use a dict:
>
>
> data = {
> 'J.R.R. Tolkien': [Book("The Hobbit"), Book("The Lord of the Rings")],
> 'Tom Clancy': [Book("The Hunt for Red October")],
> 'Terry Pratchett': [Book('Small Gods'), Book('Night Watch'),
> Book('Nation')],
> 'Stephenie Meyer': [
> Book('Something about abusive boyfriends but that's okay because
> they sparkle')],
>
> }
>
>
>
> * Should I create a new Booksfile object for reading, writing and deleting
>> entries in the file OR add corresponding methods to the book object
>> itself?
>>
>
> Heavens no. Why should the book know about the library catalog it is
> listed in?
> Your book class should be responsible for the book, and nothing but the
> book.
>
>
>
>
> --
> Steven
>
>
Thanks Steven!
I have used namedtuple like approach in few Ruby programs (not the same
problem) using Structs, but it didn't strike me for this exercise [1]. I am
going to try this approach soon.
I haven't added any validation methods for fields yet, but I am planning to
add them soon - e.g. validate alphabets or alphanumeric characters etc. It
may bring up new questions from my side, but I am sure you all will be glad
to help.. :)
1. http://www.ruby-doc.org/core-1.9.3/Struct.html
-N
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130211/5e80df20/attachment-0001.html>
More information about the Tutor
mailing list