> Sounds good. Just beware that deepcopy will try to make copies of all
> the objects referenced by your StudentUnmapped objects (assuming you
> didn't define __deepcopy__), so you may end up copying projects,
> supervisors, etc.

Good point. I'm deepcopying my students, projects and supervisors
dictionaries. But yes you're right, all of them have a reference to
other objects.

§§[Q1:] How will deepcopying the objects referenced by my
StudentUnmapped object affect me?

I also tried another structure for elegance...

class Student(object):
        def __init__(self, ee_id, name, stream_id, overall_proby):
                self.ee_id = ee_id
                self.name = name
                self.stream_id = stream_id
                self.preferences = collections.defaultdict(set)
                self.allocated_project = None
                self.allocated_proj_ref = None
                self.allocated_rank = None
                self.own_project_id = None
                self.own_project_sup = None
                self.overall_proby = overall_proby

        def __repr__(self):
                return str(self)

        def __str__(self):
                return "%s %s %s: %s (OP: %s)" %(self.ee_id, self.name,
self.allocated_rank, self.allocated_project, self.overall_proby)


class StudentDBRecord(Student):
        def __init__(self, student):
                super(StudentDBRecord, self).__init__(student.ee_id,
                                                                                
student.name,
                                                                                
student.stream_id,
                                                                                
student.preferences,
                                                                                
student.allocated_project,
                                                                                
student.allocated_proj_ref,
                                                                                
student.allocated_rank,
                                                                                
student.own_project_id,
                                                                                
student.own_project_sup,
                                                                                
student.overall_proby)

mapper(StudentDBRecord, students_table, properties={'proj_id' :
relation(Project)})

Basically, the theory was I'd do all my algorithm stuff on the Student
objects and then after I've found an optimal solution I'll push those
onto the StudentDBRecord table for persistence...

However I ended up getting the following error:

#####

File "Main.py", line 25, in <module>
    prefsTableFile = 'Database/prefs-table.txt')
  File "/Users/Azfar/Dropbox/Final Year Project/SPAllocation/
DataReader.py", line 158, in readData
    readProjectsFile(projectsFile)
  File "/Users/Azfar/Dropbox/Final Year Project/SPAllocation/
DataReader.py", line 66, in readProjectsFile
    supervisors[ee_id] = Supervisor(ee_id, name, original_quota,
loading_limit)
  File "<string>", line 4, in __init__
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
state.py", line 71, in initialize_instance
    fn(self, instance, args, kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
mapper.py", line 1829, in _event_on_init
    instrumenting_mapper.compile()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
mapper.py", line 687, in compile
    mapper._post_configure_properties()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
mapper.py", line 716, in _post_configure_properties
    prop.init()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
interfaces.py", line 408, in init
    self.do_init()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
properties.py", line 714, in do_init
    self._get_target()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
properties.py", line 726, in _get_target
    self.mapper = mapper.class_mapper(self.argument, compile=False)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
util.py", line 564, in class_mapper
    raise exc.UnmappedClassError(class_)
sqlalchemy.orm.exc.UnmappedClassError: Class 'ProjectParties.Student'
is not mapped

#####

§§[Q2:] What's that all about? Something wrong with the inheritence?

+++

> I would recommend using a database
> sequence or GUIDs to ensure that each call to monteCarloBasic gets a
> unique value for this column.

As another key sequence different from the simple "ident ==
row_number" I'm currently using right? I'll look into that.

+++

The thread business is indeed going over my head :S.

> In this way, monteCarloBasic returns its
>      results as a set of objects that are not attached to any session
>      (either because they are unmapped or are transient
>      
> <http://www.sqlalchemy.org/docs/reference/orm/sessions.html#sqlalchemy...>
>      instances), which the UI thread uses to update the database. How
>      you pass data from worker threads to the UI thread is dependent on
>      your GUI toolkit.

My GUI toolkit is Tkinter?

On Jun 7, 4:01 pm, Conor <[email protected]> wrote:
> On 06/06/2010 02:58 PM, Az wrote:
>
> > Hi Conor,
>
> > Basically I sat down and made some decisions and changes.
>
> > I've created an actual copy of the Student class as in I've now got
> > two classes, Student and StudentUnmapped. The Unmapped one has the
> > same attributes as the mapped one, except for being... well, unmapped.
> > Now I can a) use deepcopy and b) change the objects without worry.
> > resetData() will act on the unmapped dictionary as well so the mapped
> > object remains safe and unchanged.
>
> Sounds good. Just beware that deepcopy will try to make copies of all
> the objects referenced by your StudentUnmapped objects (assuming you
> didn't define __deepcopy__), so you may end up copying projects,
> supervisors, etc.
>
>
>
> > Sorry for beating around the bush with questions that were a bit non-
> > SQLA.
>
> > Let's get back to some SQLA questions:
>
> > 1. The only changes I'd push onto the mapped object would be... after
> > running my MC, I get a bunch of probabilities -- those I want to
> > persist. How do I modify the field in a table I've already
> > "session.commit()"-ed using the following function. This happens
> > pretty much after I've finished reading in the dictionaries
> > completely. After that I just add each thing to the relevant table.
> > But I'd want to update some attributes of student because I want to be
> > able to have in the database for access later.
>
> > def addToTable():
> >         """Very simple SQLAlchemy function that populates the Student,
> > Project
> >         and Supervisor tables."""
>
> >         for student in students.itervalues():
> >                 session.add(student)
> >                 session.flush()
>
> >         for project in projects.itervalues():
> >                 session.add(project)
> >                 session.flush()
>
> >         for supervisor in supervisors.itervalues():
> >                 session.add(supervisor)
> >                 session.flush()
>
> >         session.commit()
>
> It sounds like you want to a) INSERT students/projects/supervisors that
> don't yet exist in the database, and b) UPDATE
> students/projects/supervisors that do exist in the database. If so, I
> think you want to use session.merge instead of session.add.
>
> > 2. Say I've now got a physical database and I've run my Monte-Carlo
> > multiple times. I think I'd either want to a) have the original M-C
> > sessions be overwritten or b) create another set of data, perhaps
> > using the data to differentiate the two. How can I do this? Can I
> > query each one separately? Or am I better off just with an overwrite?
>
> You can indeed append the new set of data to the existing data. You
> would just need another column in SimAllocation to distinguish between
> different calls to monteCarloBasic. I would recommend using a database
> sequence or GUIDs to ensure that each call to monteCarloBasic gets a
> unique value for this column.
>
> > 3. Finally, regarding the GUI. If each function indicates a separate
> > "thread", then in that case, yes with my GUI I'd be passing the
> > session from thread to thread since I'm no longer just running Main.py
> > but rather, the constituent functions one by one. How do I deal with
> > this? The reason I used the database was because of persistence and I
> > definitely want my data to persist between threads (and after I've
> > closed my program) so I can use them for all manner of useful
> > calculations, queries and output.
>
> Just to be clear, by "thread" I mean actual system threads spawned by
> the the thread or threading module. If this is indeed what you want,
> then you probably have a UI thread and a worker thread that runs
> monteCarloBasic. Since you should not share a single session object
> between threads, you can:
>
>    1. Change monteCarloBasic to not rely on sessions (including their
>       persistent
>       
> <http://www.sqlalchemy.org/docs/reference/orm/sessions.html#sqlalchemy...>
>       objects) at all (you would have to make copies of your students,
>       projects, and supervisors before hading them over to
>       monteCarloBasic). You are already sort-of on this track by using
>       StudentUnmapped objects. In this way, monteCarloBasic returns its
>       results as a set of objects that are not attached to any session
>       (either because they are unmapped or are transient
>       
> <http://www.sqlalchemy.org/docs/reference/orm/sessions.html#sqlalchemy...>
>       instances), which the UI thread uses to update the database. How
>       you pass data from worker threads to the UI thread is dependent on
>       your GUI toolkit.
>    2. Change monteCarloBasic to create its own session from the
>       sessionmaker object. This will let monteCarloBasic read and write
>       from/to the database, but you will have to arrange for your UI
>       thread session to expire_all or close itself appropriately so it
>       can see the new data.
>
> Again, this thread business is probably overkill for your project, so
> you may want to avoid it altogether.
>
> -Conor

-- 
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.

Reply via email to