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.

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()

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?

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.

If I can sort these three things out -- this entire project is wrapped
up :)

Thanks in advance!

Az

On Jun 6, 4:39 am, Conor <[email protected]> wrote:
> On 06/05/2010 08:06 PM, Az wrote:
>
>
>
> > Cheers!
>
> > Creating a new instance of my mapped class and settings, manually.
> > Gotcha. I think this will be an easier solution for me.
>
> > Nah, I'm not in a web framework.
>
> > Additional Q:
>
> > +++
>
> > Currently, my database is being stored in memory and it's fine like
> > that since a) my data isn't very expansive and b) I'm running the
> > program (python Main.py) from a command line where I can just comment
> > out various functions in my Main file. However, I'm now designing a
> > GUI for my code where I want to be able to call each function
> > manually. Now, all functions that reference the session will reference
> > "session" I defined as:
>
> > Session = sessionmaker(bind=engine)
> > session = Session()
>
> > Thus after I run my finish my monteCarloBasic (defined way up at the
> > top), I'd probably hit another button that would do what I did for
> > CODE 2, i.e.
>
> > def checkFor4545(trials):
> >     sid = 4545
> >     print sid
> >     project_id_list = list(students[sid].preferences)
> >     for project_id in project_id_list
> >         gotcha =
> > session.query(SimAllocation).filter(SimAllocation.student_id ==
> > sid).filter(PP.SimAllocation.alloc_proj == project_id).count()
> >         print project_id, gotcha/trials
>
> > This basically counts the number of times student 4545 got each of his
> > projects for entire Monte-Carlo simulation and prints the average over
> > the trials.
>
> > So basically when I'm in command line mode and go "python Main.py" my
> > Main looks like:
>
> > trials = 100
> > monteCarloBasic(trials)
> > checkFor4545(trials)
>
> > So those will run one after the other.
>
> > Now when I've got a GUI, I'll have the Monte-Carlo run separately and
> > then afterwards, hit a button that corresponds to
> > 'checkFor4545(trials)'. Now, the reason I used SQLAlchemy in the first
> > place (besides the general usefulness of SQL) is for data persistence.
> > Will 'checkFor4545(trials)' display the same output as it would if it
> > were run serially from Main.py? Will it reference the same "session"?
> > (Probably a question you'll want to slap your forehead over, but I
> > just want to verify I've got my understanding correct).
>
> I see nothing that indicates that they would NOT see the same session,
> but I do have some comments:
>
>     * GUIs usually run long tasks in background threads to keep the UI
>       responsive. If you were to do this, you would not want to use a
>       single global session, because sharing a session between threads
>       is a big no-no.
>     * I'm concerned what the call to resetData does. If it resets
>       student-project associations, then could it end up deleting the
>       temp_allocs you just added in that trial?
>     * What should happen if you run monteCarloBasic multiple times? It
>       seems like you would get duplicate primary keys on SimAllocation
>       rows after the 1st call.
>
> > Additionally, when I save to a physical database file, what happens
> > everytime I run monteCarloBasic(trials) (since it writes to the
> > database). Will it rewrite it every time? Or will it keep appending to
> > it?
>
> I don't see anything that would indicate rewriting the database in the
> code that you have shown (except maybe as a side-effect of your
> resetData function that I noted above). Also, you may get duplicate
> primary key errors like I mentioned above.
>
> -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