如何使用从函数

时间:2017-08-05 17:07:10

标签: python mysql dictionary sqlalchemy

我有一张名为starting_pitchers的表格。我使用该表中的统计数据在名为average.py.的单独python文件中创建复合排名。

复合排名是一个字典,其中键是投手的名称(原始表中的唯一列),值是表示其他统计信息组合的浮点数。我如何在average.py文件中在原始表中创建一个新列,该列由复合值组成,其中名称(字典键)与原始表中的name列匹配,并相应地插入值?

    from flask_sqlalchemy import SQLAlchemy  
from models import starting_pitchers  
from db import db  
from sqlalchemy import create_engine  
from sqlalchemy.orm import sessionmaker  
from main import app  

engine = create_engine('mysql+pymysql://baseball:baseball@localhost:8889/baseball', echo=True)



def average():  
    CFIP_sort_list = []  
    for i in starting_pitchers.query.order_by('CFIP asc'):  
        CFIP_sort_list.append(i)  
    xFIP_sort_list = []  
    for ii in starting_pitchers.query.order_by('xFIP asc'):  
        xFIP_sort_list.append(ii)  
    FIP_sort_list = []  
    for iii in starting_pitchers.query.order_by('FIP asc'):  
        FIP_sort_list.append(iii)  
    KperBB_sort_list = []  
    for iv in starting_pitchers.query.order_by('KperBB desc'):  
        KperBB_sort_list.append(iv)  
    Total_Ks_sort_list = []  
    for v in starting_pitchers.query.order_by('Total_Ks desc'):  
        Total_Ks_sort_list.append(v)  
    WHIP_sort_list = []  
    for vi in starting_pitchers.query.order_by('WHIP asc'):  
        WHIP_sort_list.append(vi)  
    ERA_sort_list = []  
    for vii in starting_pitchers.query.order_by('ERA asc'):  
        ERA_sort_list.append(vii)  
    IP_sort_list = []  
    for viii in starting_pitchers.query.order_by('Innings_Pitched desc'):  
        IP_sort_list.append(viii)  
    Wins_sort_list = []  
    for ix in starting_pitchers.query.order_by('Wins desc'):  
        Wins_sort_list.append(ix)  
    QS_Rate_sort_list = []  
    for x in starting_pitchers.query.order_by('Quality_Start_Rate desc'):  
        QS_Rate_sort_list.append(x)  
    GB_Rate_sort_list = []  
    for xi in starting_pitchers.query.order_by('Ground_Ball_Rate desc'):  
        GB_Rate_sort_list.append(xi)  
    SC_Rate_sort_list = []  
    for xii in starting_pitchers.query.order_by('Soft_Contact_Rate desc'):  
        SC_Rate_sort_list.append(xii)  
    FP_Rank_sort_list = []  
    for xiii in starting_pitchers.query.order_by('FP_Rank asc'):  
        FP_Rank_sort_list.append(xiii)  
    SW_Rank_sort_list = []  
    for xiii in starting_pitchers.query.order_by('SW_Rank asc'):  
        SW_Rank_sort_list.append(xiii)  
    CT_Rank_sort_list = []  
    for xiv in starting_pitchers.query.order_by('CT_Rank asc'):  
        CT_Rank_sort_list.append(xiv)  
    HC_Rank_sort_list = []  
    for xv in starting_pitchers.query.order_by('HC_Rank asc'):  
        HC_Rank_sort_list.append(xv)  

    Sorted_avg_dict = {}  

    for o in CFIP_sort_list:  

        index_collector = (3 * CFIP_sort_list.index(o))  
        if o in xFIP_sort_list:  
            index_collector += xFIP_sort_list.index(o)  
        if o in FIP_sort_list:  
            index_collector += FIP_sort_list.index(o)  
        if o in KperBB_sort_list:  
            index_collector += KperBB_sort_list.index(o)  
        if o in Total_Ks_sort_list:  
            index_collector += Total_Ks_sort_list.index(o)  
        if o in WHIP_sort_list:  
            index_collector += WHIP_sort_list.index(o)  
        if o in ERA_sort_list:  
            index_collector += ERA_sort_list.index(o)  
        if o in IP_sort_list:  
            index_collector += IP_sort_list.index(o)  
        if o in Wins_sort_list:  
            index_collector += WHIP_sort_list.index(o)  
        if o in QS_Rate_sort_list:  
            index_collector += QS_Rate_sort_list.index(o)  
        if o in GB_Rate_sort_list:  
            index_collector += GB_Rate_sort_list.index(o)  
        if o in SC_Rate_sort_list:  
            index_collector += SC_Rate_sort_list.index(o)  
        if o in FP_Rank_sort_list:  
            index_collector += 3 * (FP_Rank_sort_list.index(o))  
        if o in SW_Rank_sort_list:  
            index_collector += 3 * (SW_Rank_sort_list.index(o))  
        if o in CT_Rank_sort_list:  
            index_collector += 3 * (SW_Rank_sort_list.index(o))  
        if o in HC_Rank_sort_list:  
            index_collector += 3 * (HC_Rank_sort_list.index(o))  

            Sorted_avg_dict[o.Name] = (index_collector / 26)  

    print(Sorted_avg_dict)  

    return Sorted_avg_dict  

    def insert_column():  

        name_values_list = [{'Chris Sale': 1.5, 'Corey Kluber':                                                                                                   2.3076923076923075, 'Max Scherzer': 1.2692307692307692, 'Clayton Kershaw':   4.1923076923076925, 'Stephen Strasburg': 5.423076923076923,  
    'Zack Greinke': 4.423076923076923, 'Jacob Degrom': 5.115384615384615, 
'Yu Darvish': 6.346153846153846, 'Madison Bumgarner': 6.961538461538462, 'Jon Lester': 7.461538461538462}]  


     for name_value_dict in name_values_list:  
            starting_pitchers.insert().values(**name_value_dict)  

    if __name__ == "__main__":  
        db.app = app  
        average()
        insert_column()

model.py

from db import db

class starting_pitchers(db.Model):
    Name = db.Column(db.String(80), unique=True)
    ID = db.Column(db.Integer, primary_key=True)
    CFIP = db.Column(db.Integer)
    xFIP = db.Column(db.Float)
    FIP = db.Column(db.Float)
    KperBB = db.Column(db.Float)
    Total_Ks = db.Column(db.Integer)
    WHIP = db.Column(db.Float)
    ERA = db.Column(db.Float)
    Innings_Pitched = db.Column(db.Float)
    Wins = db.Column(db.Integer)
    Quality_Start_Rate = db.Column(db.Integer)
    Ground_Ball_Rate = db.Column(db.Float)
    Soft_Contact_Rate = db.Column(db.Float)
    FP_Rank = db.Column(db.Integer, unique=True)
    SW_Rank = db.Column(db.Integer)
    CT_Rank = db.Column(db.Integer)
    HC_Rank = db.Column(db.Integer)
    Composite_Rank = db.Column(db.Float)

    def __init__(self, Name, CFIP, xFIP, FIP, KperBB, Total_Ks, WHIP, ERA, Innings_Pitched, Wins, Quality_Start_Rate,
                 Ground_Ball_Rate, Soft_Contact_Rate, FP_Rank, SW_Rank, CT_Rank, HC_Rank, Composite_Rank):
        self.Name = Name
        self.CFIP = CFIP
        self.xFIP = xFIP
        self.FIP = FIP
        self.KperBB = KperBB
        self.Total_Ks = Total_Ks
        self.WHIP = WHIP
        self.ERA = ERA
        self.Innings_Pitched = Innings_Pitched
        self.Wins = Wins
        self.Quality_Start_Rate = Quality_Start_Rate
        self.Ground_Ball_Rate = Ground_Ball_Rate
        self.Soft_Contact_Rate = Soft_Contact_Rate
        self.FP_Rank = FP_Rank
        self.SW_Rank = SW_Rank
        self.CT_Rank = CT_Rank
        self.HC_Rank = HC_Rank
        self.Composite_Rank = Composite_Rank

1 个答案:

答案 0 :(得分:0)

name_values_list = [{'name': 'abcd', 'value': 2.3}, {'name': 'xyz', 'value': 1.0}]
for name_value_dict in name_values_list:
    table_name.insert().values(**name_value_dict)

使用**解压缩字典,将namevalue作为参数传递给insert方法。