Hi there,

I'm just trying to sort out some code implementation utilising
SQLAlchemy that should store some output for a Monte-Carlo simulation
I'm running.

The simulation works thus:

I have a dictionary of students, with their preferred projects and a
Monte-Carlo simulation that takes said dictionary, randomises the
students and tries to assign them their best choice project (i.e. ones
with the highest preference rank on their list). That constitutes a
single session.

This is repeated for say, N sessions. Thus the "session_id" will start
from 0/1 and remain static until the next session wherein it will
increment itself until it hits N-1/N (which is the last session), and
the simulation terminates.

I'm not sure how I'm going to insert the rows but two ways are:

1. At the end of each session, for each student in the dictionary
(loop), add the necessary info (shown below) and insert row
2. For each project assignment (or non-assignment: this is a valid
case), insert row.

I've got two tables (students_table and session_allocation) which have
very similar mappings (code below). The students_table was fairly easy
but is only effective for a single session, which is why I created the
session_allocation table to take into account a session identifier.

The kind of output I'm looking for is in the form:

———
Unique/Surrogate ID || Session_id || Student_id || Assigned_Project_id
|| Assigned_Project_Rank
———

So, questions:

1. How does one get an auto incrementing unique key going?

2. The session_id is not part of the Student class (where the
Student_id, Assigned_Project_id and Assigned_Project_rank mappings
come from). This should be a parameter of the Monte-Carlo class (yet
to be created). Is it possible to map just the session_id from that
and the other details from the student class? As you can guess, with
each session, certain parts of the Student object are overwritten
(namely the ones mentioned as columns).



# Begin code
class Student(object):
    def __init__(self, sid, name, stream_id):
        self.sid = sid
        self.name = name
        self.preferences = collections.defaultdict(set)
        self.allocated_proj_ref = None
        self.allocated_rank = 0

    def __repr__(self):
        return str(self)

    def __str__(self):
        return "%s %s %s %s" %(self.sid, self.name,
self.allcated_proj_ref, self.allocated_project)


# SQLAlchemy database code
engine = create_engine('sqlite:///:memory:', echo=False)
metadata = MetaData()

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

session_allocation = Table('sesh_alloc', metadata,
    Column('ident', Integer, primary_key=True),
    Column('sesh_id', Integer),
    Column('ee_id', Integer),
    Column('allocated_proj_ref', Integer,
ForeignKey('projs.proj_id')),
    Column('allocated_rank', Integer)
)

metadata.create_all(engine)



-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to