覆盖python模块与一个有一个额外的参数 - python

问题描述:

我已经导入了一个很酷的包(如果它很重要,它被称为tweepy)。覆盖python模块与一个有一个额外的参数 - python

但是由于最近Twitter做出的改变,有两种方法存在一个小的局限性。已经有一个修复,但它一直没有拉动

我正在考虑尝试暂时添加修补程序在我身边并覆盖现有的模块,直到包被更新,虽然我不是很有经验蟒蛇

def statuses_lookup(self, id_, include_entities=None, 
        trim_user=None, map_=None): 
    return self._statuses_lookup(list_to_csv(id_), include_entities, 
           trim_user, map_) 

@property 
def _statuses_lookup(self): 
    """ :reference: https://dev.twitter.com/rest/reference/get/statuses/lookup 
     :allowed_param:'id', 'include_entities', 'trim_user', 'map' 
    """ 
    return bind_api(
     api=self, 
     path='/statuses/lookup.json', 
     payload_type='status', payload_list=True, 
     allowed_param=['id', 'include_entities', 'trim_user', 'map'], 
     require_auth=True 
    ) 

,并覆盖与类似的(区别是增加一个额外的参数tweet_mode据我所知):

def statuses_lookup(self, id_, include_entities=None, 
        trim_user=None, map_=None, tweet_mode=None): 
    return self._statuses_lookup(list_to_csv(id_), include_entities, 
           trim_user, map_, tweet_mode) 

@property 
def _statuses_lookup(self): 
    """ :reference: https://dev.twitter.com/rest/reference/get/statuses/lookup 
     :allowed_param:'id', 'include_entities', 'trim_user', 'map', 'tweet_mode' 
    """ 
    return bind_api(
     api=self, 
     path='/statuses/lookup.json', 
     payload_type='status', payload_list=True, 
     allowed_param=['id', 'include_entities', 'trim_user', 'map', 'tweet_mode'], 
     require_auth=True 
    ) 

我从来没有真正尝试过这样的事情之前,蟒蛇3.X

+0

什么问题? –

+0

@SergeyVasilyev,是否有可能覆盖导入(第一个代码块)中的现有方法,其中更改后的版本(第二个代码块) – user3120554

+0

,因为差异只是另一个参数。我以为只需添加到改变我的代码应该足够(根据我的理解后期绑定多态性),但后来我不知道怎么说通过tweepy.API._statuses_lookup = _statuses_lookup2和 适用于进口 – user3120554

在这种情况下,为了实现最好的办法是从原始对象延伸并覆盖你想要什么:

class CustomAPI(API): 
    def statuses_lookup(self, id_, include_entities=None, 
        trim_user=None, map_=None, tweet_mode=None): 
     return self._statuses_lookup(list_to_csv(id_), include_entities, 
           trim_user, map_, tweet_mode) 

    @property 
    def _statuses_lookup(self): 
     """ :reference: https://dev.twitter.com/rest/reference/get/statuses/lookup 
      :allowed_param:'id', 'include_entities', 'trim_user', 'map', 'tweet_mode' 
     """ 
     return bind_api(
      api=self, 
      path='/statuses/lookup.json', 
      payload_type='status', payload_list=True, 
      allowed_param=['id', 'include_entities', 'trim_user', 'map', 'tweet_mode'], 
      require_auth=True 
     ) 

然后你可以使用CustomAPI而不是API

更新

假设您创建一个新的文件custom_tweepy.py

from tweepy import API 

class CustomAPI(API): 
    def statuses_lookup(self, id_, include_entities=None, 
        trim_user=None, map_=None, tweet_mode=None): 
     return self._statuses_lookup(list_to_csv(id_), include_entities, 
           trim_user, map_, tweet_mode) 

    @property 
    def _statuses_lookup(self): 
     """ :reference: https://dev.twitter.com/rest/reference/get/statuses/lookup 
      :allowed_param:'id', 'include_entities', 'trim_user', 'map', 'tweet_mode' 
     """ 
     return bind_api(
      api=self, 
      path='/statuses/lookup.json', 
      payload_type='status', payload_list=True, 
      allowed_param=['id', 'include_entities', 'trim_user', 'map', 'tweet_mode'], 
      require_auth=True 
     ) 

其他每一个地方,你原来使用from tweepy import API,你可以用from custom_tweepy import CustomAPI as API取代它。接下来,您可以使用您的自定义API实现。而且,你只需重写这两个函数,所有其他函数根本不会改变。

+0

如果我使用customapi而不是api,那是不是意味着我必须修改导入的代码来包含类?否则tweepy.API不会被替换tweepy.CusomAPI – user3120554

+0

我也想保留在API中的其他方法的功能。实际上模仿了进入源代码并直接更改代码的行为 – user3120554

+0

不,您不需要修改源代码。您可以创建一个包含'tweepy导入API'和'CustomAPI'的新文件。然后你可以用'from your_file import CustomAPI as API'替换'tweepy import API'来使用'CustomAPI'。此外,在这种情况下,您只需重写这两个函数,其他函数根本不会改变。我会更新我的答案,使其更加明确。 – Sraw

一切都是可行的。

以下是关于如何破解课程的简化解决方案。即使模块是进口,类用于创建对象 - 这个技巧会影响到所有立即现有对象:

class KLS(object): 
    @property 
    def x(self): 
     print('OLD') 
     return 100 

def new_function(self): 
    print('NEW') 
    return 200 

obj = KLS() 

print(obj.x) # OLD + 100  
KLS.x = property(new_function) # <-- HACK! 
print(obj.x) # NEW + 200 

该解决方案既适用PY2 & PY3。

但是,请确保在原始类中没有该属性的setter/deleters(即可设置吗?或只是只读?)对于设置者,您必须致电property(getter_func, setter_func)

+0

嗯,我想的是类似的东西tweepy.API.statuses_lookup = statuses_lookup2。我试着财产(),但我得到一个错误:statuses_lookup2()失踪1人需要的位置参数: 'ID_' – user3120554

+0

所有我真的打电话是:API = tweepy.API(AUTH) ((((其中上面的修改已经完成)))))和tweets = api。statuses_lookup(id_batch,tweet_mode ='extended') – user3120554