Also adding a bit here:

My Student (this one is mapped) class looks like this:

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

students_table = Table('studs', metadata,
        Column('ee_id', Integer, primary_key=True),
        Column('name', String),
        Column('stream_id', String),
        Column('allocated_rank', Integer),
        Column('allocated_proj_ref', Integer, ForeignKey('projs.proj_id')),
        Column('overall_proby', Float)
)

mapper(Student, students_table)

Now the reason I wanted to use copy was because I was going to be
changing certain data around -- specifically, self.allocated_project,
self.allocated_proj_ref and self.allocated_rank.

You suggested defining, or rather redefining copy such that:

> def copy(self):
>   copy = MyMappedClass()
>   copy.attr1 = self.attr1
>   copy.attr2 = self.attr2
>   return copy

Now does this look okay?

def copied(self):
    copied = Student()
    copied.allocated_project = self.allocated_project
    copied.allocated_proj_ref = self.allocated_proj_ref
    copied.allocated_rank = self.allocated_rank
    return copied

Basically what I'm copying are dictionaries.

Thus the "students" dictionary looks like this:

students[ee_id] = Student(ee_id, name...)

So BEFORE I was just doing
students_copied = copy.copy(students)

How would I use the copy function I created?

students_copied = students.copied()?

And so if I used this version... my objects won't change (which was
why I wanted to use deepcopy) when I make changes to the copied
dictionary?

Thanks

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