首先,我对Python并不十分熟悉,所以如果这是一个过于基本的问题我会道歉。我曾尝试在Google和SO上搜索解决方案,并且没有任何结果。
我目前正致力于加密货币交易平台,该平台使用Autobahn接收WAMP市场更新。但是,我的一个类方法存在问题。有问题的代码如下:
class TradeClient(ApplicationSession):
exchange = "NULL"
def eventProcessor(self, event):
print("eventProcessor called")
print("market event received: {}".format(event))
def onJoin(self, details):
print("{} client session ready".format(exchange))
def marketEvent(event):
print("marketEvent called")
self.processEvent(event)
[...]
现在,显然,TradeClient是一个旨在成为超类的类,并且交换变量和eventProcessor方法旨在在子类中被覆盖,以便与不同的交换(对于套利等)兼容。 marketEvent方法被称为精细,但eventProcessor方法不是。
这是一个范围问题吗?好像它不应该是这样,并且传递eventProcessor一个event [:]的参数没有任何区别。
编辑:
marketEvent函数嵌套在onJoin中。我很抱歉没有明确说明情况就是这样 - 我曾假设缩进会使这一点清楚。下面粘贴了正确完整的代码。我希望调用eventProcessor的原因是为了使JSON的解析易于覆盖不同的交换。
from autobahn.asyncio.wamp import ApplicationSession
from autobahn.asyncio.wamp import ApplicationRunner
from asyncio import coroutine
import sys
import json
from io import StringIO
poloniex_URL = "wss://api.poloniex.com"
class TradeClient(ApplicationSession):
exchange = "NULL"
def eventProcessor(self, event):
print("eventProcessor called")
print("market event received: {}".format(event))
def onJoin(self, details):
print("{} client session ready".format(self.exchange))
def marketEvent(event):
print("marketEvent called")
self.eventProcessor(self, event)
# Read in configuration files
try:
pairs = [line.strip() for line in open("conf/" + self.exchange + ".conf")]
except:
print("Configuration file not found for {}!".format(self.exchange))
sys.exit(1)
# Subscribe to each currency pair / topic in the conf file
for pair in pairs:
try:
yield from self.subscribe(marketEvent, pair)
print("subscribed to {} on {}".format(pair, self.exchange))
except Exception as e:
print("could not subscribe to {} on {}: {}".format(pair, exchange, e))
sys.exit(1)
class PoloniexClient(TradeClient):
exchange = "poloniex"
poloniex_runner = ApplicationRunner(url = poloniex_URL, realm = "realm1")
poloniex_runner.run(PoloniexClient)