键入错误进行betfair.py API调用

时间:2017-10-14 11:47:59

标签: python python-2.7 anaconda betfair

我刚刚将一些代码移到Ubuntu 16.04.2 anaconda设置中,并且在调用代码时遇到了类型错误,我无法理解这种错误在许多其他机器上工作正常。

错误复制了我所有网球市场中的所有网球市场样本代码,以及下面的请求:

from betfair import Betfair
client = Betfair("app key needed here", "path to ssh key here")
client.login(username, password)
client.keep_alive()

client.list_market_book(market_ids=['1.135391020'], price_projection=dict(priceData=['EX_BEST_OFFERS']))

from betfair.models import MarketFilter
event_types = client.list_event_types(
    MarketFilter(text_query='tennis')
)
print(len(event_types))                 # 2
print(event_types[0].event_type.name)   # 'Tennis'
tennis_event_type = event_types[0]
markets = client.list_market_catalogue(
    MarketFilter(event_type_ids=[tennis_event_type.event_type.id])
)
markets[0].market_name 

尽管在Windows安装上使用相同的代码,但两者都抛出以下类型错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-69b51cf78438> in <module>()
      1 event_types = client.list_event_types(
----> 2     MarketFilter(text_query='tennis')
      3 )
      4 print(len(event_types))                 # 2
      5 print(event_types[0].event_type.name)   # 'Tennis'

<decorator-gen-125> in list_event_types(self, filter, locale)

/home/user/anaconda2/lib/python2.7/site-packages/betfair/utils.pyc in requires_login(func, *args, **kwargs)
    121     self = args[0]
    122     if self.session_token:
--> 123         return func(*args, **kwargs)
    124     raise exceptions.NotLoggedIn()

/home/user/anaconda2/lib/python2.7/site-packages/betfair/betfair.pyc in list_event_types(self, filter, locale)
    148             'listEventTypes',
    149             utils.get_kwargs(locals()),
--> 150             model=models.EventTypeResult,
    151         )
    152 

/home/user/anaconda2/lib/python2.7/site-packages/betfair/betfair.pyc in make_api_request(self, base, method, params, codes, model)
     87         utils.check_status_code(response, codes=codes)
     88         result = utils.result_or_error(response)
---> 89         return utils.process_result(result, model)
     90 
     91     # Authentication methods

/home/user/anaconda2/lib/python2.7/site-packages/betfair/utils.pyc in process_result(result, model)
     81         return result
     82     if isinstance(result, collections.Sequence):
---> 83         return [model(**item) for item in result]
     84     return model(**result)
     85 

/home/user/anaconda2/lib/python2.7/site-packages/betfair/meta/models.pyc in __init__(self, **data)
     24     def __init__(self, **data):
     25         super(BetfairModel, self).__init__()
---> 26         self.import_data(data)
     27 
     28     def import_data(self, data, **kwargs):

/home/user/anaconda2/lib/python2.7/site-packages/betfair/meta/models.pyc in import_data(self, data, **kwargs)
     28     def import_data(self, data, **kwargs):
     29         kwargs['strict'] = False
---> 30         return super(BetfairModel, self).import_data(data, **kwargs)

/home/user/anaconda2/lib/python2.7/site-packages/schematics/models.pyc in import_data(self, raw_data, recursive, **kwargs)
    269             The data to be imported.
    270         """
--> 271         data = self._convert(raw_data, trusted_data=_dict(self), recursive=recursive, **kwargs)
    272         self._data.converted.update(data)
    273         if kwargs.get('validate'):

/home/user/anaconda2/lib/python2.7/site-packages/schematics/models.pyc in _convert(self, raw_data, context, **kwargs)
    293         should_validate = getattr(context, 'validate', kwargs.get('validate', False))
    294         func = validate if should_validate else convert
--> 295         return func(self._schema, self, raw_data=raw_data, oo=True, context=context, **kwargs)
    296 
    297     def export(self, field_converter=None, role=None, app_data=None, **kwargs):

/home/user/anaconda2/lib/python2.7/site-packages/schematics/transforms.pyc in convert(cls, mutable, raw_data, **kwargs)
    427 
    428 def convert(cls, mutable, raw_data=None, **kwargs):
--> 429     return import_loop(cls, mutable, raw_data, import_converter, **kwargs)
    430 
    431 

/home/user/anaconda2/lib/python2.7/site-packages/schematics/transforms.pyc in import_loop(schema, mutable, raw_data, field_converter, trusted_data, mapping, partial, strict, init_values, apply_defaults, convert, validate, new, oo, recursive, app_data, context)
    153                 field_context = context
    154             try:
--> 155                 value = _field_converter(field, value, field_context)
    156             except (FieldError, CompoundError) as exc:
    157                 errors[serialized_field_name] = exc

/home/user/anaconda2/lib/python2.7/site-packages/schematics/transforms.pyc in __call__(self, *args)
    354 
    355     def __call__(self, *args):
--> 356         return self.func(*args)
    357 
    358 

/home/user/anaconda2/lib/python2.7/site-packages/schematics/transforms.pyc in import_converter(field, value, context)
    382     if value is None or value is Undefined:
    383         return value
--> 384     return field.convert(value, context)
    385 
    386 

/home/user/anaconda2/lib/python2.7/site-packages/schematics/types/compound.pyc in convert(self, value, context)
     34     def convert(self, value, context=None):
     35         context = context or get_import_context()
---> 36         return self._convert(value, context)
     37 
     38     def _convert(self, value, context):

/home/user/anaconda2/lib/python2.7/site-packages/schematics/types/compound.pyc in _convert(self, value, context)
    131                 "Input must be a mapping or '%s' instance" % self.model_class.__name__)
    132         if context.convert and context.oo:
--> 133             return model_class(value, context=context)
    134         else:
    135             return model_class.convert(value, context=context)

TypeError: __init__() takes exactly 1 argument (3 given)

有些怪异,请求如下:

client.list_market_catalogue(MarketFilter(market_ids=['1.135391020']))

工作正常。

python 2.7.13,Anaconda 4.4.0,Ubuntu 16.04.2

知道可能导致这种情况的原因吗?

1 个答案:

答案 0 :(得分:1)

从跟踪中我看起来像原理图库是你的问题。在撰写本文时,检查open issues on the betfair github是否有关于schematics breaking the api的公开机票。看起来作者已经从需求中删除了原理图,并且需要版本1.1.1。我的猜测是你在计算机上安装了原理图2.0导致问题。

将来发现这种情况的一种方法是pip freeze工作环境并区分破碎的环境。此外,当移动到新机器时,您可以使用pip freeze的输出来复制环境并避免像这样的混乱版本问题。