<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Attempting to reflect a database
This is a clever way of saying
Step1:  Let's hook this up to the ORM (rather than using Core)
We need to pull in declarative_base, build a Base class, and then map the tables we reflected.
Step2:  Now map the items together.
Step3:  Let's grab a session and do some querying
<ASSISTANT_TASK:>
Python Code:
from sqlalchemy import create_engine
engine = create_engine('sqlite:////vagrant/utils/db.sqlite')
from sqlalchemy import Table, Column, MetaData
metadata = MetaData()
connection = engine.connect()
user_table = Table('user', metadata, autoload=True, autoload_with=connection)
purchase_table = Table('purchase', metadata, autoload=True, autoload_with=connection)
Explanation: Attempting to reflect a database
This is a clever way of saying: let's see if we can read the database schema using SQLAlchemy?
End of explanation
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(object):
    def __init__(self, title, first_name, surname):
        self.title = title
        self.first_name = first_name
        self.surname = surname
        
    def __repr__(self):
        return "<User(id_user={}, {} {} {})>".format(self.id_user, self.title, self.first_name, self.surname)
        
class Purchase(object):
    def __init__(self, id_user, category, item, date, price):
        self.id_user = id_user
        self.category = category
        self.item = item
        self.date = date
        self.price = price
    
    def __repr__(self):
        return ("<Purchase(id_purchase={}, id_user={}, cat={}, item={}, date={}, price={})>"
                .format(self.id_purchase, self.id_user, self.category, self.item, self.date, self.price))
Explanation: Let's hook this up to the ORM (rather than using Core)
We need to pull in declarative_base, build a Base class, and then map the tables we reflected.
End of explanation
from sqlalchemy.orm import mapper, relationship, backref
mapper(Purchase, purchase_table)
mapper(User, user_table, properties={
        'purchases': relationship(Purchase, backref='user', order_by=purchase_table.c.id_purchase)
    })
Explanation: Now map the items together.
End of explanation
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()
users = session.query(User).limit(10).all()
for u in users:
    print(u)
u1 = users[0]
print(u1.purchases)
from sqlalchemy import func
results = (session.query(User, func.count(Purchase.id_purchase))
           .join(Purchase)
           .group_by(Purchase.id_user)
           .having(func.count(Purchase.id_purchase) > 10)
           .order_by(func.count(Purchase.id_purchase).desc())
           .limit(10)
           .all())
for (u, count) in results:
    print("{}, Num Purchase={}".format(u, count))
Explanation: Let's grab a session and do some querying
End of explanation
<END_TASK>