[Tutor] associating two objects without ORM and processing a text file

Mitya Sirenef msirenef at lightbird.net
Mon Feb 11 06:28:40 CET 2013

On 02/11/2013 12:14 AM, 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
 > # Create
 > Author:
 > attribute(s): name
 > # Create class for reading and writing to the file
 > class Booksfile:
 > methods: ??
 > * 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?
 > * 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?
 > I am not planning to use SQLite or any database and would like to use 
text file only. Appreciate any help on designing such application.

Book.author should be author instance,

     def books(self):
         return [b for b in books if b.author==self]

Get lists by genre etc in a similar way.

To do file processing, look at the standard csv module:



Lark's Tongue Guide to Python: http://lightbird.net/larks/

The doer alone learneth.  Friedrich Nietzsche

More information about the Tutor mailing list