Snippets

Lucas Correia API Example - Test query object by path speed

Created by Lucas Correia
from contextlib import contextmanager
from time import time

import ftrack_api
session = ftrack_api.Session()

@contextmanager
def timing(description): 
    start = time()
    yield
    elapsed_time = time() - start
    print '{0}: {1}'.format(description, elapsed_time)


def test_link(path):
    link_value = u'%'.join(path)
    session.query(u'TypedContext where link like "%{}%"'.format(link_value)).all()


def test_parent_name(path):
    query = '' 
    for index, item in enumerate(reversed(path)):
        if (index > 0):
            query += ' and '
        parents = 'parent.' * index
        query += '{}name is "{}"'.format(parents, item)

    session.query(u'TypedContext where {}'.format(query)).all()


def test_ancestors(path):
    query = ' and '.join(['ancestors any (name is "{}")'.format(name) for name in path])
    session.query(u'TypedContext where {}'.format(query)).all()


def test_parent_has(path):
    project = path[0]
    first_parent = path[1]
    query = 'name is {} and project.name is {}'.format(first_parent, project)
    for index, item in enumerate(path[2:]):
        query = 'name is {} and parent[TypedContext] has ({})'.format(item, query)

    session.query(u'TypedContext where {}'.format(query)).all()


path = ['Sync', 'BikeChase', 'Motorway-0050', 'Compositing']

with timing('parent has (parent has (name))'):
    for i in xrange(100):
        test_parent_has(path)

with timing('link like'):
    for i in xrange(100):
        test_link(path)

with timing('parent.parent.name'):
    for i in xrange(100):
        test_parent_name(path)

with timing('ancestors'):
    for i in xrange(100):
        test_ancestors(path)

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.