是否可以在 Python3 中的特定函数之外附加代码行?
Is it possible to append lines of code outside a specific function in Python3?
我正在尝试获取加密货币交易所的订单簿如何支持特定的货币对(例如 ETH/BTC)。因为我的函数需要每分钟 运行 ,所以每次都检查这个非常耗时。我正在使用 ccxt 来获取交易所的订单簿。
通过这行代码,我检查了每一笔交易。
import ccxt
binance = ccxt.binance()
livecoin = ccxt.livecoin()
kucoin = ccxt.kucoin()
hitbtc = ccxt.hitbtc()
kraken = ccxt.kraken()
crex24 = ccxt.crex24()
okex = ccxt.okex()
headerList = ["time","type","pair"]
try:
orderbookBinance = binance.fetch_order_book(self.pair,5)
headerList.append("binance")
headerList.append("binanceAmount")
except:
print("Pair isn't available in binance")
try:
orderbookLivecoin = livecoin.fetch_order_book(self.pair,5)
headerList.append("livecoin")
headerList.append("livecoinAmount")
except:
print("Pair isn't available in livecoin")
try:
orderbookKucoin = kucoin.fetch_order_book(self.pair,5)
headerList.append("kucoin")
headerList.append("kucoinAmount")
except:
print("Pair isn't available in kucoin")
try:
orderbookHitbtc = hitbtc.fetch_order_book(self.pair,5)
headerList.append("hitbtc")
headerList.append("hitbtcAmount")
except:
print("Pair isn't available in hitbtc")
try:
orderbookKraken = kraken.fetch_order_book(self.pair,5)
headerList.append("kraken")
headerList.append("krakenAmount")
except:
print("Pair isn't available in kraken")
try:
orderbookCrex24 = crex24.fetch_order_book(self.pair,5)
headerList.append("crex24")
headerList.append("crex24Amount")
except:
print("Pair isn't available in crex24")
try:
orderbookOkex = okex.fetch_order_book(self.pair,5)
headerList.append("okex")
headerList.append("okexAmount")
except:
print("Pair isn't available in okex")
现在我需要添加所有 try-blocks 的第一行,如果它们可以执行的话。这在 python 中可行吗?
你在那里采取了错误的方法。
"Lines of code",就像变量名在程序中应该固定一样。
Python 的超级动态特性甚至允许 "add lines of code",并在 运行 时间内重新编译模块 - 但那会很复杂,复杂,矫枉过正并且容易出错,当您需要的只是一种直截了当的方法时
您需要的是将引用外部交换的对象保存在数据结构中,例如普通字典。然后你只需要遍历字典来执行方法调用和每个方法所需的其他操作 - 程序的任何部分都可以用简单、普通的属性更新字典。
import ccxt
exchanges = {}
for exchange_name in "binance livecoin kucoin hitbtc kraken crex24 okex".split():
exchanges[exchange_name] = getattr(ccxt, exchange_name)()
...
for exchange_name, exchange in exchange.items():
try:
orderbook = exchange.fetch_order_book(self.pair,5)
headerList.append(exchange_name)
headerList.append(f"{exchange_name}Amount")
except:
print(f"Pair isn't available in {exchange_name}")
如果在您的某些交易所的实际代码中您需要运行不同的代码,只需改进数据结构:您可以创建另一个内部字典,而不是直接将交易所名称与交易所实例相关联,在其他 key/value 对中,它不仅包含 ccxt 交换实例,还包含要调用的方法的名称、要传递给每个方法的参数、在获得的响应中要检查的内容等等:
您程序中的代码已修复!它的一部分可以是 运行,具体取决于数据结构中存在的配置数据。
我正在尝试获取加密货币交易所的订单簿如何支持特定的货币对(例如 ETH/BTC)。因为我的函数需要每分钟 运行 ,所以每次都检查这个非常耗时。我正在使用 ccxt 来获取交易所的订单簿。
通过这行代码,我检查了每一笔交易。
import ccxt
binance = ccxt.binance()
livecoin = ccxt.livecoin()
kucoin = ccxt.kucoin()
hitbtc = ccxt.hitbtc()
kraken = ccxt.kraken()
crex24 = ccxt.crex24()
okex = ccxt.okex()
headerList = ["time","type","pair"]
try:
orderbookBinance = binance.fetch_order_book(self.pair,5)
headerList.append("binance")
headerList.append("binanceAmount")
except:
print("Pair isn't available in binance")
try:
orderbookLivecoin = livecoin.fetch_order_book(self.pair,5)
headerList.append("livecoin")
headerList.append("livecoinAmount")
except:
print("Pair isn't available in livecoin")
try:
orderbookKucoin = kucoin.fetch_order_book(self.pair,5)
headerList.append("kucoin")
headerList.append("kucoinAmount")
except:
print("Pair isn't available in kucoin")
try:
orderbookHitbtc = hitbtc.fetch_order_book(self.pair,5)
headerList.append("hitbtc")
headerList.append("hitbtcAmount")
except:
print("Pair isn't available in hitbtc")
try:
orderbookKraken = kraken.fetch_order_book(self.pair,5)
headerList.append("kraken")
headerList.append("krakenAmount")
except:
print("Pair isn't available in kraken")
try:
orderbookCrex24 = crex24.fetch_order_book(self.pair,5)
headerList.append("crex24")
headerList.append("crex24Amount")
except:
print("Pair isn't available in crex24")
try:
orderbookOkex = okex.fetch_order_book(self.pair,5)
headerList.append("okex")
headerList.append("okexAmount")
except:
print("Pair isn't available in okex")
现在我需要添加所有 try-blocks 的第一行,如果它们可以执行的话。这在 python 中可行吗?
你在那里采取了错误的方法。
"Lines of code",就像变量名在程序中应该固定一样。 Python 的超级动态特性甚至允许 "add lines of code",并在 运行 时间内重新编译模块 - 但那会很复杂,复杂,矫枉过正并且容易出错,当您需要的只是一种直截了当的方法时
您需要的是将引用外部交换的对象保存在数据结构中,例如普通字典。然后你只需要遍历字典来执行方法调用和每个方法所需的其他操作 - 程序的任何部分都可以用简单、普通的属性更新字典。
import ccxt
exchanges = {}
for exchange_name in "binance livecoin kucoin hitbtc kraken crex24 okex".split():
exchanges[exchange_name] = getattr(ccxt, exchange_name)()
...
for exchange_name, exchange in exchange.items():
try:
orderbook = exchange.fetch_order_book(self.pair,5)
headerList.append(exchange_name)
headerList.append(f"{exchange_name}Amount")
except:
print(f"Pair isn't available in {exchange_name}")
如果在您的某些交易所的实际代码中您需要运行不同的代码,只需改进数据结构:您可以创建另一个内部字典,而不是直接将交易所名称与交易所实例相关联,在其他 key/value 对中,它不仅包含 ccxt 交换实例,还包含要调用的方法的名称、要传递给每个方法的参数、在获得的响应中要检查的内容等等:
您程序中的代码已修复!它的一部分可以是 运行,具体取决于数据结构中存在的配置数据。