I'm trying to get namedtuple to work with SQLalchemy, but to no avail .. Web search is not very covered, and I am new to Python and SQLalchemy, so I'm not sure if I'm chasing windmills: (The basic idea is that I have named element i.e.
Point=namedtuple('Point',['x','y'])
which basically creates the Point class (tuple), if I'm right. At first, this works fine, and I can create objects such as:
p=Point(3,4)
But after I create the engine, etc., and the call box, I can not create any objects without receiving this error:
Traceback (most recent call last): File "<pyshell#62>", line 1, in <module> f=Point(3,4) TypeError: __init__() takes exactly 1 argument (3 given)
Any ideas why this is happening? Does anyone know how to get namedtuple to work with sqlalchemy? Of course, I can define my own Point class, but I'm obsessed with doing the namedtuple job now. I am using Python 2.7, SQLalchemy 0.6.6 (sqlite engine)
Example:
I am trying something like this:
from sqlalchemy import * from sqlalchemy.orm import * from collections import namedtuple Point=namedtuple('Point',['x','y'],verbose=True) p=Point(3,4) db=create_engine('sqlite:///pointtest.db') metadata=MetaData() pointxy=Table('pointxy',metadata, Column('no',Integer,primary_key=True), Column('x',Integer), Column('y',Integer), sqlite_autoincrement=True) metadata.create_all(db) m=mapper(Point, pointxy) Session=sessionmaker(bind=db) session=Session() f=Point(3,4)
The basic idea is that I want a named collection of things that can be easily stored in a database. So:
class Bunch: __init__ = lambda self, **kw: setattr(self, '__dict__', kw)
will not work with sqlalchemy (i think). I can create a Bunch class, but I wonβt know in advance how many ints I want to keep in my collection. I will install it before creating my database. Hope I make sense.