Exemplo n.º 1
0
def install():
    Base.metadata.create_all(Session().bind)

    data = [
        ("Chicago", "United States", ("60601", "60602", "60603", "60604")),
        ("Montreal", "Canada", ("H2S 3K9", "H2B 1V4", "H7G 2T8")),
        ("Edmonton", "Canada", ("T5J 1R9", "T5J 1Z4", "T5H 1P6")),
        ("New York", "United States", ("10001", "10002", "10003", "10004", "10005", "10006")),
        ("San Francisco", "United States", ("94102", "94103", "94104", "94105", "94107", "94108")),
    ]

    countries = {}
    all_post_codes = []
    for city, country, postcodes in data:
        try:
            country = countries[country]
        except KeyError:
            countries[country] = country = Country(country)

        city = City(city, country)
        pc = [PostalCode(code, city) for code in postcodes]
        Session.add_all(pc)
        all_post_codes.extend(pc)

    for i in xrange(1, 51):
        person = Person(
            "person %.2d" % i,
            Address(street="street %.2d" % i, postal_code=all_post_codes[random.randint(0, len(all_post_codes) - 1)]),
        )
        Session.add(person)

    Session.commit()

    # start the demo fresh
    Session.remove()
Exemplo n.º 2
0
 def delete_all(self):
     for obj in [
             Posting, Transaction, Account, Slice, KeyValue,
             EnumerationValue, Key
     ]:
         Session.query(obj).delete()
     Session.commit()
     Session.remove()
def install():
    Base.metadata.create_all(Session().bind)

    data = [('Chicago', 'United States', ('60601', '60602', '60603', '60604')),
            ('Montreal', 'Canada', ('H2S 3K9', 'H2B 1V4', 'H7G 2T8')),
            ('Edmonton', 'Canada', ('T5J 1R9', 'T5J 1Z4', 'T5H 1P6')),
            ('New York', 'United States', ('10001', '10002', '10003', '10004',
                                           '10005', '10006')),
            ('San Francisco', 'United States', ('94102', '94103', '94104',
                                                '94105', '94107', '94108'))]

    countries = {}
    all_post_codes = []
    for city, country, postcodes in data:
        try:
            country = countries[country]
        except KeyError:
            countries[country] = country = Country(country)

        city = City(city, country)
        pc = [PostalCode(code, city) for code in postcodes]
        Session.add_all(pc)
        all_post_codes.extend(pc)

    for i in xrange(1, 51):
        person = Person(
            "person %.2d" % i,
            Address(street="street %.2d" % i,
                    postal_code=all_post_codes[random.randint(
                        0,
                        len(all_post_codes) - 1)]))
        Session.add(person)

    Session.commit()

    # start the demo fresh
    Session.remove()
Exemplo n.º 4
0
"""helloworld.py

Illustrate how to load some data, and cache the results.

"""

import environment
from model import Person
from meta import Session, FromCache

# load Person objects.  cache the result under the namespace "all_people".
print "loading people...."
people = Session.query(Person).options(FromCache("default", "all_people")).all()

# remove the Session.  next query starts from scratch.
Session.remove()

# load again, using the same FromCache option. now they're cached 
# under "all_people", no SQL is emitted.
print "loading people....again!"
people = Session.query(Person).options(FromCache("default", "all_people")).all()

# want to load on some different kind of query ?  change the namespace 
# you send to FromCache
print "loading people two through twelve"
people_two_through_twelve = Session.query(Person).\
                            options(FromCache("default", "people_on_range")).\
                            filter(Person.name.between("person 02", "person 12")).\
                            all()

# the data is cached under the "namespace" you send to FromCache, *plus*
Illustrate how to load some data, and cache the results.

"""

import environment
from model import Person
from meta import Session, FromCache

# load Person objects.  cache the result under the namespace "all_people".
print "loading people...."
people = Session.query(Person).options(FromCache("default",
                                                 "all_people")).all()

# remove the Session.  next query starts from scratch.
Session.remove()

# load again, using the same FromCache option. now they're cached
# under "all_people", no SQL is emitted.
print "loading people....again!"
people = Session.query(Person).options(FromCache("default",
                                                 "all_people")).all()

# want to load on some different kind of query ?  change the namespace
# you send to FromCache
print "loading people two through twelve"
people_two_through_twelve = Session.query(Person).\
                            options(FromCache("default", "people_on_range")).\
                            filter(Person.name.between("person 02", "person 12")).\
                            all()