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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值