Japanse New-Girl Monkey Network
Technobabble Special of the Day
The desire to write again has brought up an old dilemma. In order to do what I would like to do to streamline the writing process, I'm a bit stuck in terms of software that appropriately produces the effect I want. I could a) search the web for something that someone has already written that most closely matches what I want to do and tweak it to my needs or b) write some script that does it myself. I usually opt for b), foolishly believing that it'll take less time and effort, and, being sort of a secretly conceited geek personality type, would prefer something exclusively of my own that is patterned after my own thought processes, thus continually forcing me to re-invent the wheel. Are you bored yet? Let me elaborate.
What I want to do is come up with an (online) writer's journal that will 1) let me group articles into writing projects and 2) allow those articles to be "typed"-- e.g. one is a freewrite, one is a draft, one is a link to a source of information or inspiration, one is a review or reflection on some other author's work that relates to the project, etc. The idea is to be flexible enough to group pieces of a larger whole together without entirely being stuck in the category/ subcategory paradigm (am I making sense here?).
What I've done is create a database model that I hope will support this concept I have in my head. Now I'm trying to write some Python scripts to manipulate the data. After having written literally heaps of database-accessing code, I've noticed that most database calls go through a pretty repetitive set of steps, so I've attempted a sort of object-oriented approach with a basic database "object" class (meant to represent a generic table in the db with the db fields as attributes) that does your standard selects, inserts, updates, and deletes. Then I write classes that inherit from this that represent each of my individual db tables (like an article class for the articles table, a project class for the projects table, and so on and so forth).
Well, I was having weird problems with the inheritance, which I finally figured out a solution to today, and so am quite humbly posting my solution here for (hopefully) the benefit of other stumbling, blind Python neophytes like myself or the criticisms of those who are much more programmy than I. The main reason I'm posting this today was that, despite many hopeful Google searches, a scan of Useless Python, and many readings through the Python docs, I couldn't really find a good solution to my problem. Now I know you're bored. Don't worry, I'll put up something interesting tomorrow-- maybe some pictures or something to make up for all this junk.
First of all, here's the scenario-- my current web server has Python 1.5 (so maybe I just ran into a bug that was fixed in a later version, excuse my cluelessness), but that's what I'm stuck with for the moment. I created a package for all my database object classes in my cgi-bin folder so that cgi scripts that actually do cgi stuff can get at the objects for grabbing stuff out of the db. In __init__.py I write my base class, which only needs to know a few pieces of information to be able to create an object. First, it needed to know the name of the table to look up. Second, it needed to know the names of the fields to be selected, updated, whatever. And lastly, it needed to know which field was the primary key so that it wouldn't do something silly like change it arbitrarily. I also wanted to be able to pass in a specific key so that I could select a particular entry in the DB, and I also wanted to be able to pass in a set of values in case I was inserting a new entry or updating an entry. So, following the example for creating a class in Python with private fields, I wrote:
class DatabaseObject:
__fields = []
__idfield = ''
__table = ''
def __init__(self,Fields=[],IDField='',Table='',ID=None,Values={}):
self.__fields = Fields
self.__idfield = IDField
self.__table = Table
...
And then I had a bunch of other stuff which checked the ID and the Values and set the attributes of the object accordingly (so that all the field names in Fields became attributes of the object, and the corresponding value in Values would be used to set the attribute value).
Well, that was giving me errors when I wrote a new class that inherited from DatabaseObject. Apparently, when I did that, self.__fields and friends did not exist. My discovery for a fix was:
class DatabaseObject:
def __init__(self,Fields=[],IDField='',Table='',ID=None,Values={}):
self.__dict__['__fields'] = Fields
self.__dict__['__idfield'] = IDField
self.__dict__['__table'] = Table
...
And from there on out in the code I always had to refer to the private variables using self.__dict__['__blah'] rather than self.__blah. Anyone smarter and wiser than me care to enlighten me why only the clunky way worked and the other one didn't? I'm still feeling somewhat hazy on the way Python handles inheritance, anyway, so a lecture would be welcomed...