Hi,
This is my first time posting so I'll try my best not to mess up!
First let me provide everyone with the necessary information.
I'm a beginner to both Python and SQLAlchemy.
Version Info
=========
OS: Mac OSX 10.5.8
Python: 2.6.4
SQLAlchemy: 0.5.8
=========
My Class
=======
+++
class Student(object):
def __init__(self, name, id):
self.id = id
self.name = name
self.preferences = collections.defaultdict(set)
self.own_proj_id = None
self.own_proj_sup = None
self.allocated_project = None
self.allocated_rank = 0
def __repr__(self):
return str(self)
def __str__(self):
return "%s %s" %(self.id, self.name)
+++
Background
=========
Now, I've got a function that reads in the necessary information from
a text database into these objects. The function more or less works
and I can easily access the information from the objects.
Before the SQLAlchemy code runs, the function will read in the
necessary info and store it into the Class. There is a dictionary
called `students` which stores this as such:
+++
students = {}
students[id] = Student(<all the info from the various "reader"
functions>)
+++
Afterwards, there is an "allocation" algorithm that will allocate
projects to student. It does that well enough. If a student already
has his/her own project, the own_proj_id and own_proj_sup fields have
numeric info. The allocated_project remains as None (but I suppose I
can fix that to reflect the own_proj_id). Now if a student is
unsuccessful in getting a project the following fields remain as None:
own_proj_id, own_proj_sup and allocated_project.
SQLAlchemy bit
=============
So after all this happens, I'd like to map my object to a database
table. Using the documentation, I've used the following code:
+++
from sqlalchemy import *
from sqlalchemy.orm import *
engine = create_engine('sqlite:///:memory:', echo=False)
metadata = MetaData()
students_table = Table('studs', metadata,
Column('id', Integer, primary_key=True),
Column('name', String)
)
metadata.create_all(engine)
mapper(Student, students_table)
Session = sessionmaker(bind=engine)
sesh = Session()
+++
Now after that, I was curious to see if I could print out all the
students from my students dictionary.
+++
for student in students.itervalues():
print student
+++
What do I get but an error:
+++
Traceback (most recent call last):
File "~/FYP_Tests/FYP_Tests.py", line 140, in <module>
print student
File "/~FYP_Tests/Parties.py", line 30, in __str__
return "%s %s" %(self.id, self.name)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
attributes.py", line 158, in __get__
return self.impl.get(instance_state(instance),
instance_dict(instance))
AttributeError: 'NoneType' object has no attribute 'get'
+++
I'm at a loss as to how to resolve this issue, if it is an issue. If
more information is required, please ask and I will provide it.
Kind regards,
Az
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.