Created
May 4, 2012 01:31
-
-
Save orf/2591081 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #models.py | |
| from sqlalchemy import Column, String, Integer, create_engine, Table, ForeignKey | |
| from sqlalchemy.dialects import postgresql | |
| from sqlalchemy.orm import sessionmaker, ScopedSession | |
| from sqlalchemy.ext.declarative import declarative_base | |
| engine = create_engine("postgresql+psycopg2://arraybench:password@localhost:5432/arraybench", echo=False) | |
| Base = declarative_base() | |
| Session = ScopedSession(sessionmaker(bind=engine)) | |
| class Actor(Base): | |
| __tablename__ = "actors" | |
| id = Column(Integer, primary_key=True) | |
| name = Column(String(100)) | |
| def __init__(self, name): | |
| self.name = name | |
| #movies = Column(postgresql.ARRAY(Integer)) | |
| class Movie(Base): | |
| __tablename__ = "movies" | |
| id = Column(Integer, primary_key=True) | |
| title = Column(String) | |
| def __init__(self, title): | |
| self.title = title | |
| #actors = Column(postgresql.ARRAY(Integer)) | |
| association_table = Table('association', Base.metadata, | |
| Column('actor_id', Integer, ForeignKey('actors.id')), | |
| Column('movie_id', Integer, ForeignKey('movies.id')) | |
| ) | |
| Base.metadata.create_all(engine) | |
| #populate.py | |
| import string | |
| import random | |
| import models | |
| import sys | |
| movies_count = int(sys.argv[1]) | |
| actors_count = int(sys.argv[2]) | |
| print "Populating movies to %s and actors to %s"%(movies_count, actors_count) | |
| def random_string(max_size): | |
| size = random.randint(50,max_size) | |
| return "".join([random.choice(string.ascii_letters+" ") for i in xrange(size)]) | |
| session = models.Session() | |
| print " * Populating movies" | |
| for i in xrange(movies_count): | |
| session.add(models.Movie(random_string(100))) | |
| print " * Populating actors" | |
| for i in xrange(actors_count): | |
| session.add(models.Actor(random_string(100))) | |
| print " - Comitting..." | |
| session.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment