I have the following two classes :
class DbQuery:
def __init__(self,query):
self.query = query
class DbResultSet:
def __init__(self,query):
self.result = []
And these classes are used for SQL queries and results, attributes are used to simulate enum-like functionality. A query looks as follows :
q_forum_heirarchy =\
type( "RtDbQuery",
DbQuery( """SELECT level, id, parent_id, name
FROM forum_heirarchy_node
WHERE forum_id = :f_id
AND level != 0
ORDER BY level ASC;"""),
LEVEL=0, ID=1, PARENT_ID=2, NAME=3 )
Is there some way for me to copy the attributes from DbQuery into DbResultset ? I don’t want to have to embed the query in the resultset because then I would have to write db_resultset.query.LEVEL instead of db_resultset.LEVEL to get to the enum ordinals.
I see a __get_attribute__ method but I suppose this is indexed by attribute_name.
edit:
The code is invalid python, it doesn’t change the gist of what I was asking. I discovered psycopg2(database library) can do namedtuples() (python objects with the performance characters of enums), so I can’t be bothered to update it 😀
I think that you should use
__getattr__method. This method is used by lookup mechanism when no class attributes are found.