- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我从 API 获得了一个对象:
class Order(object):
""" generated source for class Order """
# main order fields
m_orderId = 0
m_clientId = 0
m_permId = 0
m_action = ""
m_totalQuantity = 0
m_orderType = ""
m_lmtPrice = float()
m_auxPrice = float()
# extended order fields
m_tif = "" # "Time in Force" - DAY, GTC, etc.
m_activeStartTime = "" # GTC orders
m_activeStopTime = "" # GTC orders
m_ocaGroup = "" # one cancels all group name
m_ocaType = 0 # 1 = CANCEL_WITH_BLOCK, 2 = REDUCE_WITH_BLOCK, 3 = REDUCE_NON_BLOCK
m_orderRef = ""
m_transmit = bool() # if false, order will be created but not transmited
m_parentId = 0 # Parent order Id, to associate Auto STP or TRAIL orders with the original order.
m_blockOrder = bool()
m_sweepToFill = bool()
m_displaySize = 0
m_triggerMethod = 0 # 0=Default, 1=Double_Bid_Ask, 2=Last, 3=Double_Last, 4=Bid_Ask, 7=Last_or_Bid_Ask, 8=Mid-point
m_outsideRth = bool()
m_hidden = bool()
m_goodAfterTime = "" # FORMAT: 20060505 08:00:00 {time zone}
m_goodTillDate = "" # FORMAT: 20060505 08:00:00 {time zone}
m_overridePercentageConstraints = bool()
m_rule80A = "" # Individual = 'I', Agency = 'A', AgentOtherMember = 'W', IndividualPTIA = 'J', AgencyPTIA = 'U', AgentOtherMemberPTIA = 'M', IndividualPT = 'K', AgencyPT = 'Y', AgentOtherMemberPT = 'N'
m_allOrNone = bool()
m_minQty = 0
m_percentOffset = float() # REL orders only; specify the decimal, e.g. .04 not 4
m_trailStopPrice = float() # for TRAILLIMIT orders only
m_trailingPercent = float() # specify the percentage, e.g. 3, not .03
# Financial advisors only
m_faGroup = ""
m_faProfile = ""
m_faMethod = ""
m_faPercentage = ""
# Institutional orders only
m_openClose = "" # O=Open, C=Close
m_origin = 0 # 0=Customer, 1=Firm
m_shortSaleSlot = 0 # 1 if you hold the shares, 2 if they will be delivered from elsewhere. Only for Action="SSHORT
m_designatedLocation = "" # set when slot=2 only.
m_exemptCode = 0
# SMART routing only
m_discretionaryAmt = float()
m_eTradeOnly = bool()
m_firmQuoteOnly = bool()
m_nbboPriceCap = float()
m_optOutSmartRouting = bool()
# BOX or VOL ORDERS ONLY
m_auctionStrategy = 0 # 1=AUCTION_MATCH, 2=AUCTION_IMPROVEMENT, 3=AUCTION_TRANSPARENT
# BOX ORDERS ONLY
m_startingPrice = float()
m_stockRefPrice = float()
m_delta = float()
# pegged to stock or VOL orders
m_stockRangeLower = float()
m_stockRangeUpper = float()
# VOLATILITY ORDERS ONLY
m_volatility = float() # enter percentage not decimal, e.g. 2 not .02
m_volatilityType = 0 # 1=daily, 2=annual
m_continuousUpdate = 0
m_referencePriceType = 0 # 1=Bid/Ask midpoint, 2 = BidOrAsk
m_deltaNeutralOrderType = ""
m_deltaNeutralAuxPrice = float()
m_deltaNeutralConId = 0
m_deltaNeutralSettlingFirm = ""
m_deltaNeutralClearingAccount = ""
m_deltaNeutralClearingIntent = ""
m_deltaNeutralOpenClose = ""
m_deltaNeutralShortSale = bool()
m_deltaNeutralShortSaleSlot = 0
m_deltaNeutralDesignatedLocation = ""
# COMBO ORDERS ONLY
m_basisPoints = float() # EFP orders only, download only
m_basisPointsType = 0 # EFP orders only, download only
# SCALE ORDERS ONLY
m_scaleInitLevelSize = 0
m_scaleSubsLevelSize = 0
m_scalePriceIncrement = float()
m_scalePriceAdjustValue = float()
m_scalePriceAdjustInterval = 0
m_scaleProfitOffset = float()
m_scaleAutoReset = bool()
m_scaleInitPosition = 0
m_scaleInitFillQty = 0
m_scaleRandomPercent = bool()
m_scaleTable = ""
# HEDGE ORDERS ONLY
m_hedgeType = "" # 'D' - delta, 'B' - beta, 'F' - FX, 'P' - pair
m_hedgeParam = "" # beta value for beta hedge (in range 0-1), ratio for pair hedge
# Clearing info
m_account = "" # IB account
m_settlingFirm = ""
m_clearingAccount = "" # True beneficiary of the order
m_clearingIntent = "" # "" (Default), "IB", "Away", "PTA" (PostTrade)
# ALGO ORDERS ONLY
m_algoStrategy = ""
m_algoParams = None
# What-if
m_whatIf = bool()
# Not Held
m_notHeld = bool()
# Smart combo routing params
m_smartComboRoutingParams = None
# order combo legs
m_orderComboLegs = []
def __init__(self):
""" generated source for method __init__ """
self.m_lmtPrice = Double.MAX_VALUE
self.m_auxPrice = Double.MAX_VALUE
self.m_activeStartTime = self.EMPTY_STR
self.m_activeStopTime = self.EMPTY_STR
self.m_outsideRth = False
self.m_openClose = "O"
self.m_origin = self.CUSTOMER
self.m_transmit = True
self.m_designatedLocation = self.EMPTY_STR
self.m_exemptCode = -1
self.m_minQty = Integer.MAX_VALUE
self.m_percentOffset = Double.MAX_VALUE
self.m_nbboPriceCap = Double.MAX_VALUE
self.m_optOutSmartRouting = False
self.m_startingPrice = Double.MAX_VALUE
self.m_stockRefPrice = Double.MAX_VALUE
self.m_delta = Double.MAX_VALUE
self.m_stockRangeLower = Double.MAX_VALUE
self.m_stockRangeUpper = Double.MAX_VALUE
self.m_volatility = Double.MAX_VALUE
self.m_volatilityType = Integer.MAX_VALUE
self.m_deltaNeutralOrderType = self.EMPTY_STR
self.m_deltaNeutralAuxPrice = Double.MAX_VALUE
self.m_deltaNeutralConId = 0
self.m_deltaNeutralSettlingFirm = self.EMPTY_STR
self.m_deltaNeutralClearingAccount = self.EMPTY_STR
self.m_deltaNeutralClearingIntent = self.EMPTY_STR
self.m_deltaNeutralOpenClose = self.EMPTY_STR
self.m_deltaNeutralShortSale = False
self.m_deltaNeutralShortSaleSlot = 0
self.m_deltaNeutralDesignatedLocation = self.EMPTY_STR
self.m_referencePriceType = Integer.MAX_VALUE
self.m_trailStopPrice = Double.MAX_VALUE
self.m_trailingPercent = Double.MAX_VALUE
self.m_basisPoints = Double.MAX_VALUE
self.m_basisPointsType = Integer.MAX_VALUE
self.m_scaleInitLevelSize = Integer.MAX_VALUE
self.m_scaleSubsLevelSize = Integer.MAX_VALUE
self.m_scalePriceIncrement = Double.MAX_VALUE
self.m_scalePriceAdjustValue = Double.MAX_VALUE
self.m_scalePriceAdjustInterval = Integer.MAX_VALUE
self.m_scaleProfitOffset = Double.MAX_VALUE
self.m_scaleAutoReset = False
self.m_scaleInitPosition = Integer.MAX_VALUE
self.m_scaleInitFillQty = Integer.MAX_VALUE
self.m_scaleRandomPercent = False
self.m_scaleTable = self.EMPTY_STR
self.m_whatIf = False
self.m_notHeld = False
它非常冗长,我需要为 SQLAlchemy 的 Order(Base)
对象上的每个变量生成一个字段。有没有办法以编程方式执行此操作,还是必须复制/粘贴此操作?
最佳答案
我可能会创建一个 git 存储库,并尝试更详细地解决这个问题,因为我不知道为什么似乎还没人做到这一点:P 除非他们做到了,而我只是找不到答案.. . ?
from sqlalchemy import Table, MetaData, Column, Integer, String, Float, Boolean
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.orm import mapper
from sqlalchemy import create_engine
metadata = MetaData()
class Hero:
def __init__(self):
self.name = "name"
self.xp = 500
self.luck = 3.7
self.is_alive = True
self.quests = []
self.attributes = {}
self.town = None
hero = Hero() #Some generic object. I was using it for a game database :P
class BuildTable:
def __init__(self, obj):
self.obj = obj
self.tablename = BuildTable.get_table_name(obj)
self.column_names = BuildTable.get_column_names(obj)
self.table = self.build_table()
def build_table(self):
return Table(self.tablename, metadata,
Column('id', Integer, primary_key=True),
*self.build_columns()
)
def build_columns(self):
"""Generate columns for table.
Also builds extra attribute self.extra that will accomodate relationships and such
using recursion.
"""
data = vars(self.obj)
relationships = []
dicts = []
nones = []
for name in data.keys():
column_type = type(data[name])
if type(list()) == column_type:
relationships.append(name)
elif type(int()) == column_type:
yield Column(name, Integer)
elif type(str()) == column_type:
yield Column(name, String)
elif type(dict()) == column_type:
dicts.append(name)
elif type(float()) == column_type:
yield Column(name, Float)
elif type(None) == column_type:
nones.append(name)
elif type(bool()) == column_type:
yield Column(name, Boolean)
else:
raise TypeError("Can't yet handle type {}".format(type(data[name])))
#Methods that requir extra work.
#They might be relationships, foreign keys or children or parents or something.
#Or just basic lists, or dicts or booleans
self.extra = {'relationships': relationships, 'dictionaries': dicts, 'Nones': nones}
def get_column_names(obj):
return (attr for attr in vars(obj))
def get_table_name(obj):
return obj.__class__.__name__
hero_table = BuildTable(hero).table
mapper(Hero, hero_table)
for t in metadata.sorted_tables:
print("Table name: ", t.name)
print("t is page_table: ", t is hero_table)
for column in hero_table.columns:
print("Column Table name: ", column.type)
engine = create_engine('sqlite:///:memory:', echo=True)
metadata.bind = engine
metadata.create_all(checkfirst=True)
警告:目前无法处理字典、无类型或列表。这是因为它们都需要某种“关系”,而且你知道关系有时非常复杂:)。我认为为此需要某种递归。哦,如果你解决了这个问题,请制作一个答案的 git 存储库,因为我也在尝试解决这个问题。
我认为这忽略了函数。我相信它可能会忽略类变量......但尚未测试。它仅为初始化对象而设计。我希望这对你仍然有效。或者您可以对其进行足够的修改以使其工作。
关于python - 如何从非声明性对象自动生成 SQLAlchemy 架构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41201536/
我正在尝试创建一个使用 UUID 作为主键的用户模型: from src.db import db # SQLAlchemy instance import sqlalchemy_utils impo
在 sqlalchemy 中,我试图合并表,然后使用 WHERE 和 ORDER_BY 创建别名 有点像 SELECT * FROM ( SELECT [TABLE_ONE].[SOME_ID]
我正在使用 SQL Alchemy(通过 Flask_sqlalchemy)将 Python 字典列表插入到 Postgres 数据库中。 其中一个表是所有唯一项目的列表(表 1),而第二个是与某个项
This source详细说明如何使用关联代理创建具有 ORM 对象值的 View 和对象。 但是,当我附加一个与数据库中现有对象匹配的值(并且所述值是唯一的或主键)时,它会创建一个冲突的对象,因此我
SQLAlchemy Core和SQLAlchemy ORM的目的有什么区别? 最佳答案 顾名思义,ORM是一个对象关系映射器:其目的是将数据库关系表示为Python对象。 核心是查询构建器。其目的是
带有ForeignKey的Column是否自动创建索引? 还是我需要手动添加index=True? some_field = Column(Integer, ForeignKey(SomeModel.
我有一个主数据库,每个客户自己的数据库连接存储在其中。 因此,每个客户端都使用2个db:main和它自己的db,必须确定其连接 对于每个http调用。我如何使用flask-sqlalchemy扩展名执
当我仅对类进行继承时,它才起作用 class User(Base): __tablename__ = ’users’ id = Column(Integer, primary_key=
从用户的角度来看,SQLAlchemy 的查询日志似乎有点过于冗长,有时甚至有点神秘: 2015-10-02 13:51:39,500 INFO sqlalchemy.engine.base.Engi
我正在尝试使用 wtforms.ext.sqlalchemy QuerySelectMultipleField 显示复选框列表,但我无法在 GET 的表单上显示模型数据。 这是我的models.py
我想为查询返回一个中继连接。使用标准的 graphene-sqlalchemy 你可以这样做: class Query(graphene.ObjectType): node = relay.N
我在 centos 7.5 虚拟机上部署了最新的 Airflow ,并将 sql_alchemy_conn 和 result_backend 更新到 postgresql 实例上的 postgres
我想将多个项目插入到一个表中,并在发生冲突时更新该表。这是我想出的以下内容 from sqlalchemy.dialects.postgresql import insert meta = MetaD
我有以下模型: class Item(Base): a = relationship() b = relationship() c = relationship() d
我有 presto 和 superset 设置。 presto 运行良好,可以通过命令访问: ./app/hadoop/setjdk8.sh;bin/presto-cli --server http:
我一直在寻找一种在 sqlalchemy 中使用 tsvector 的方法(就像 INTEGER 等其他方法一样),但到目前为止我还不清楚如何做到这一点。我读过可以使用 UserDefinedType
我正在使用 sqlalchemy(现在使用 sqlite,但稍后可能会改变)来构建一个数据库,其中插入的顺序和 rowids 很重要。我基本上有以下几点: class Message(Base):
给定一个对象,我想知道如何知道它是否是 sqlalchemy 映射模型的实例。 通常,我会使用 isinstance(obj, DeclarativeBase)。但是,在这种情况下,我没有可用的 De
我已经通读了查询文档,如果有办法从查询中获取表名,就看不到任何地方 - 例如如果我有 q = query(Users) ,我可以得到Users从 q 退出? 最佳答案 请注意,像您这样的事件简单查询可
我不确定如何定义create schema foo迁移?我的模型如下所示(我正在使用Flask-Migrate): class MyTable(db.Model): __tablename__
我是一名优秀的程序员,十分优秀!