从SQLAlchemy对象构建词典列表的最快方法

时间:2018-09-27 18:26:52

标签: python python-3.x sqlalchemy

我有一段代码将SQLAlchemy对象处理成Python字典。在下面的代码中,aggcontainerssales都是SQLAlchemy对象,其结构如下:

agg -- (one to many) --> containers -- (one to many) --> sales

当前,此循环的每次迭代大约需要0.1秒。反正有优化的方法吗?

_json = {}
_json['period_sales'] = []

for container in agg.containers:
    s = time.time()
    for sale in container.sales:
        _json['period_sales'].append({
            'start_date': None,
            'end_date': None,
            'sale': sale.purchases
        })
    print('{}'.format(time.time() - s))

0 个答案:

没有答案