0
0 комментариев

Как заменить все выводы на экран объектами так, что бы функция возвращала эти объекты? То есть убрать из кода все print так, чтобы return заменял бы их.

from sqlalchemy import text
from sqlalchemy import create_engine
 
def data():
    engine=create_engine('mysql://***********:**************/nova', echo=False)
    connect=engine.connect()
 
q0 = text(
    "SELECT project_id FROM instance_actions "
    "WHERE user_id LIKE :e0 "
)
users = connect.execute(q0, e0='c88%')
seen=set()
newwlist=[]
for user in users:
    tt=tuple(user)
    if tt not in seen:
        newwlist.append(user)
        seen.add(tt)
 
leng=len(newwlist)
for i in range(0, leng):
    project_id=newwlist[i][0]
    print "Project:", project_id
    q = text(
        "SELECT instance_uuid FROM instance_actions "
        "WHERE project_id LIKE :e1 "
    )
    instances = connect.execute(q, e1=project_id).fetchall()
    seen=set()
    newlist=[]
    for item in instances:
        t=tuple(item)
        if t not in seen:
            newlist.append(item)
            seen.add(t)
   total[project_id] = {}
   len=len(newlist)
    for i in range(0, len):
        inst_id=newlist[i][0]
        print "Machine:", inst_id
        query = text(
            "SELECT unix_timestamp(created_at), action FROM instance_actions "
            "WHERE instance_uuid LIKE :e2 "
        )
        result = connect.execute(query, e2=inst_id).fetchall()
        for row in result:
            print row
 
 
        pozitive=['create', 'start', 'restart', 'reboot', 'unpause']
        negative=['stop', 'pause', 'delete']
        create='create'
        delete='delete'
 
        starttime = -1
        length = len(result)
        totaltime_1=0
 
        total[project_id][inst_id] = {}
        total[project_id][inst_id]['totaltime_1'] = []
        for j in range(0, length):
            if result[j][1] in pozitive:
                starttime = result[j][0]
            elif result[j][1] in negative and result[j-1][0] is starttime:
                a = (result[j][0] - starttime) / 3600
                b = (result[j][0] - starttime) % 3600
                if b is 0:
                    time=a
                elif b is not 0:
                    time=a+1
                    print time
                    totaltime_1 += time
                    starttime=-1
        days_1=totaltime_1 / 24
        remaining_hours_1=totaltime_1 % 24
total[project_id][inst_id]['totaltime_1'].append(totaltime_1)
return {'total' : total}

Что нужно написать в return чтобы вывод остался тот же, а все print можно было бы убрать?


Добавить комментарий