Created
          January 13, 2014 11:53 
        
      - 
      
- 
        Save andir/8399048 to your computer and use it in GitHub Desktop. 
    flask-restless profile to_dict
  
        
  
    
      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
    
  
  
    
  | from flask import Flask | |
| from flask.ext.sqlalchemy import SQLAlchemy | |
| from flask.ext.restless.helpers import to_dict | |
| from sqlalchemy.ext.hybrid import hybrid_property | |
| import line_profiler | |
| profile = line_profiler.LineProfiler() | |
| profile.add_function(to_dict) | |
| app = Flask(__name__) | |
| app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' | |
| db = SQLAlchemy(app) | |
| class Parent(db.Model): | |
| id = db.Column(db.Integer(10), primary_key=True) | |
| name = db.Column(db.String(10), unique=True) | |
| childs = db.relationship('Child') | |
| @hybrid_property | |
| def test(self): | |
| return '%s-test' % self.name | |
| class Child(db.Model): | |
| id = db.Column(db.Integer(10), primary_key=True) | |
| name = db.Column(db.String(10), unique=True) | |
| parent_id = db.Column(db.ForeignKey('parent.id')) | |
| parent = db.relationship('Parent') | |
| db.create_all() | |
| parent = Parent(name='parent-%d' % 1) | |
| db.session.add(parent) | |
| db.session.commit() | |
| for ci in range(0,100): | |
| child = Child(parent_id=parent.id, name='child-%d-%d' % (1, ci)) | |
| db.session.add(child) | |
| db.session.commit() | |
| if __name__ == "__main__": | |
| from timeit import Timer | |
| profile.enable() | |
| def test(): | |
| to_dict(parent, deep={'childs': []}) | |
| timer = Timer(stmt=test) | |
| print timer.repeat(3, number=100) | |
| #map(lambda x: test(), range(0,99)) | |
| profile.disable() | |
| profile.print_stats() | |
| profile.dump_stats('to_dict.lprof') | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment