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.orm.session.Session>
      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.orm.session.Session>
      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