473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Introspection with classes

Me again! Same project as before, if you're playing along at home.

The Everything Engine model (which I am blatantly copying in my project)
is that everything in the engine is a 'node,' and that every node can be
stored to and read from the database. Since this is the backend for a
website, minimizing hits on the database is a worthy goal. Thus, it is
paramount that reading in any given subclass of the base 'Node' class
take (optimally) one query. (A simple, yet fairly simple-minded design
-- and my first stab at the problem -- would be to have each subclass
read in its own data after calling its parent's -- or parents'! -- read
method(s). This has the disadvantage of cluttering a lot of code with
database queries and making it quite difficult to create new subclasses.)

Here's a rough sketch of what I'm doing at the moment. I've invested
enough time into it at this point that I've totally lost track of
whether it's a good idea, and I would please like someone to tell me.

Every node has certain attributes. In this outline, let's say these are
the node's unique id number, its title, and the id number of its owner.

class Node:
dbInfo = { 'title' : ['node', 'title', ""]
'owner_id' : ['node', 'owner_id', None] }

def __init__(self):
self.default()

def default(self):
self.node_id = None
for k, v in self.dbInfo.items():
setattr(self, k, v[2])

def read(self, db, node_id):
# construct a database query from the table/column info in
# dbInfo

def commit(self, db):
# write the current values to the db

def insert(self, db):
# insert a new node using the current values

def nuke(self, db):
# remove the node from the db, then:
self.default()

So, simple enough. (The real version has signifigantly more stuff going
on, as well as a permission system, but I have elided these for the sake
of clarity.) By doing that dance with dbInfo, I can easily subclass Node.

Say we want a new node that holds some text. We just need to add a new
attribute like so:

class Document(Node):
dbInfo = {}
dbInfo.update(Node.dbInfo)
dbInfo['content'] = ['docs', 'text', ""]

.... and that's it. That's the whole Document class. (Well, the real
version has a render method that takes the content, parses it in
interesting ways, and writes it to the client, but that's simple, too.)
The shortness of this class is why this strikes me as a good idea.

The other advantage is I can make use of multiple inheritance. First,
say we have a class that just holds a dictionary:

class DictNode(Node):
dbInfo = {}
dbInfo.update(Node.dbInfo)
dbInfo['stuff'] = ['dictnode', 'dict', {}]

(Assume a mechanism is provided to automatically pickle the dictionary,
which there is.)

Now say I want a nodetype that's just a document with a dictionary
attached. This is almost exactly how the User class is implemented: the
document holds the user's "homenode," which is just a brief bio or
whatever the user wants to put in there, and the dictionary holds the
user's settings. They snap together like Legos. It looks just like this:

class User(Document, DictNode):
dbInfo = {}
dbInfo.update(Document.dbInfo)
dbInfo.update(DictNode.dbInfo)

If the User class wanted its own attributes (say, a password), they
would just get added to the end.

This is my third stab at the problem (the first was mentioned above, the
second was basically the same as this but dbInfo was an instance
variable instead of a class variable which, uh, was sort of dumb).

Coming from C++, I'm still wrapping my brain around Python's
introspection abilties. Can I generalize this more? Am I missing some
idiom or language feature that would benefit me? Where's my time machine?

-Kirk McDonald
Jan 31 '06 #1
0 1063

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vsevolod (Simon) Ilyushchenko | last post by:
Hi, Last year I have written a Perl module to serve as a backend to Macromedia Flash applications: http://www.simonf.com/amfperl I have just rewritten it in Python, which I have not used...
8
by: Mark English | last post by:
I'd like to write a Tkinter app which, given a class, pops up a window(s) with fields for each "attribute" of that class. The user could enter values for the attributes and on closing the window...
4
by: Benjamin Rutt | last post by:
I'm trying to learn about introspection in Python. my ultimate goal is to be able to build a module "text database" of all modules that are in the sys.path, by discovering all candidate modules...
0
by: Steven T. Hatton | last post by:
I suspect the core language is not the level at which introspection should be implemented in C++. That has been the choice of C#, and Java. Both of these languages made some trade-offs to...
4
by: Steven T. Hatton | last post by:
Has there been any substantial progress toward supporting introspection/reflection in C++? I don't intend to mean it should be part of the Standard. It would, nonetheless, be nice to have a...
1
by: Mansi | last post by:
Let's say I have the following class: class ccTestClass { public void doMethod() { } } If I have the name of the method stored in some variable x, such that String
2
by: greg.corson | last post by:
Hi, I'm in the process of building a complex system using a sort of "system services" model. I'm looking at putting together various services like serialization, networking, 2D/3D rendering,...
3
by: James Stroud | last post by:
Hello, I wanted to automagically generate an instance of a class from a dictionary--which might be generated from yaml or json. I came up with this: # automagical constructor def...
14
by: Dave Rahardja | last post by:
Is there a way to generate a series of statements based on the data members of a structure at compile time? I have a function that reverses the endianness of any data structure: /// Reverse...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.