python 获取对象中的属性并进行赋值操作

需求:

需要保存十张不同结构表的数据,但是不想写十个实体类赋值的代码。

于是乎,想着数据转成dict,然后dict转成实体类。

所以主要的就是dict 转换成实体类这里。

重要方法:

hasattr(entity, k):
            
setattr(entity, k, v)

 当然了,还有getattr方法

class spider_eastmoney_gdp_his(Base):
    __tablename__ = "spider_eastmoney_gdp_his"
    id = Column(Integer, primary_key=True, autoincrement=True)
    report_date = Column(String)
    time = Column(String)
    domesticl_product_base  = Column(FLOAT)
    first_product_base  = Column(FLOAT)
    second_product_base  = Column(FLOAT)
    third_product_base  = Column(FLOAT)
    sum_same  = Column(FLOAT)
    first_same  = Column(FLOAT)
    second_same  = Column(FLOAT)
    third_same  = Column(FLOAT)


class spider_eastmoney_pmi_his(Base):
    __tablename__ = "spider_eastmoney_pmi_his"
    id = Column(Integer, primary_key=True, autoincrement=True)
    report_date = Column(String)
    time = Column(String)
    make_index  = Column(FLOAT)
    make_same  = Column(FLOAT)
    nmake_index  = Column(FLOAT)
    nmake_same  = Column(FLOAT)

class spider_eastmoney_index_xfzxx_his(Base):
    __tablename__ = "spider_eastmoney_index_xfzxx_his"
    id = Column(Integer, primary_key=True, autoincrement=True)
    report_date = Column(String)
    time = Column(String)
    consumers_faith_index  = Column(FLOAT)
    faith_index_same  = Column(FLOAT)
    faith_index_sequential  = Column(FLOAT)
    consumers_astis_index  = Column(FLOAT)
    astis_index_same  = Column(FLOAT)
    astis_index_sequential  = Column(FLOAT)
    consumers_expect_index  = Column(FLOAT)
    expect_index_same  = Column(FLOAT)
    expect_index_sequential  = Column(FLOAT)

class spider_eastmoney_amt_shxfp_his(Base):
    __tablename__ = "spider_eastmoney_amt_shxfp_his"
    id = Column(Integer, primary_key=True, autoincrement=True)
    report_date = Column(String)
    time = Column(String)
    retail_total  = Column(FLOAT)
    retail_total_same  = Column(FLOAT)
    retail_total_sequential  = Column(FLOAT)
    retail_total_accumulate  = Column(FLOAT)
    retail_accumulate_same  = Column(FLOAT)

class spider_eastmoney_money_his(Base):
    __tablename__ = "spider_eastmoney_money_his"
    id = Column(Integer, primary_key=True, autoincrement=True)
    report_date = Column(String)
    time = Column(String)
    basic_currency  = Column(FLOAT)
    basic_currency_same  = Column(FLOAT)
    basic_currency_sequential  = Column(FLOAT)
    currency  = Column(FLOAT)
    currency_same  = Column(FLOAT)
    currency_sequential  = Column(FLOAT)
    free_cash_same  = Column(FLOAT)
    free_cash_sequential  = Column(FLOAT)

class spider_eastmoney_fiscal_revenue_his(Base):
    __tablename__ = "spider_eastmoney_fiscal_revenue_his"
    id = Column(Integer, primary_key=True, autoincrement=True)
    report_date = Column(String)
    time = Column(String)
    base  = Column(FLOAT)
    base_same  = Column(FLOAT)
    base_sequential  = Column(FLOAT)
    base_accumulate  = Column(FLOAT)
    accumulate_same  = Column(FLOAT)

class spider_eastmoney_tax_revenue_his(Base):
    __tablename__ = "spider_eastmoney_tax_revenue_his"
    id = Column(Integer, primary_key=True, autoincrement=True)
    report_date = Column(String)
    time = Column(String)
    tax_income  = Column(FLOAT)
    tax_income_same  = Column(FLOAT)
    tax_income_sequential  = Column(FLOAT)


class spider_eastmoney_new_credit_his(Base):
    __tablename__ = "spider_eastmoney_new_credit_his"
    id = Column(Integer, primary_key=True, autoincrement=True)
    report_date = Column(String)
    time = Column(String)
    rmb_loan  = Column(FLOAT)
    rmb_loan_same  = Column(FLOAT)
    rmb_loan_sequential  = Column(FLOAT)
    rmb_loan_accumulate  = Column(FLOAT)
    loan_accumulate_same  = Column(FLOAT)


def json_to_entity(dictValue, type):
    type_dict = {
            "cpi": spider_eastmoney_cpi_his()
            , "ppi": spider_eastmoney_ppi_his()
            , "gdp": spider_eastmoney_gdp_his()
            , "pmi": spider_eastmoney_pmi_his()
            , "index_xfzxx": spider_eastmoney_index_xfzxx_his()
            , "amt_shxfp": spider_eastmoney_amt_shxfp_his()
            , "mn": spider_eastmoney_money_his()
            , "fiscal_revenue": spider_eastmoney_fiscal_revenue_his()
            , "tax_revenue": spider_eastmoney_tax_revenue_his()
            , "new_credit": spider_eastmoney_new_credit_his()
        }
    entity = type_dict[type]
    for k, v in dictValue.items():
        if hasattr(entity, k):
            setattr(entity, k, v)

    return entity

你可能感兴趣的:(python,python,开发语言)