[Tutor] associating two objects without ORM and processing a text file
Dave Angel
davea at davea.name
Mon Feb 11 06:56:55 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 take it from this that this is a class assignment, and that neither
speed nor enormous data capacity nor concurrent operation is a
requirement. But your professor may have other requirements that you
haven't told us about.
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
Is this in order to save space, since each Book instance can then have a
reference to an Author object, rather than a reference to a string
containing the Author's name.
If you deem this class useful, then don't you also want one for Genre ?
>
> # Create class for reading and writing to the file
> class Booksfile:
> methods: ??
Why ? Are you transliterating this from Java ?
>
>
> * 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?
If performance doesn't matter, then create one list of Book instances,
and search it for whichever characteristics you like.
> * 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?
Neither, a pair of regular functions will do fine. One that loads when
you start the program, and another that saves the file when you exit.
If you really expect to update the file incrementally, deleting entries
in it, then think hard about your decision to roll your own database. A
text file isn't random access.
>
> 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.
>
>
The real question is where you might proceed after meeting these first
requirements. For example, if you expect the list to grow to a few
hundred million entries, then you'll need to be very careful about
layout. And starting and exiting the program would be very slow, since
you can do nothing till all the data has been loaded in and converted to
objects.
Or perhaps you'll want to have another file with additional author data,
associated with the first by carefully spelling the author's name the
same in all cases. In that case, having a separate Author class makes
great sense. So if you expect to grow in that direction, then you
should create the class now, even though it has only one attribute.
--
DaveA
More information about the Tutor
mailing list