2017-10-15 38 views
0

我运行下面的一些代码:蟒蛇:覆盖导入的类来扩展功能

import tweepy 
# https://github.com/tweepy/tweepy 

auth = tweepy.OAuthHandler(keys['consumer_key'], keys['consumer_secret']) 
auth.set_access_token(keys['access_token'], keys['access_token_secret']) 
api = tweepy.API(auth) 
. 
. 
tweets = api.statuses_lookup(id_batch) 

我想通过我的代码来扩展tweepy的功能(而不是等待tweepy从github上与更新相同的功能),并为最后一行添加额外参数(tweet_mode ='extended')

我正在考虑在tweepy中重写某个类的两个特定函数。新功能将

我创建了一个新的customtweepy.py和内部完全一样,原来只是一个额外的变量(我刚添加的tweet_mode东西):

from tweepy import * 


def statuses_lookup(self, id_, include_entities=None, 
       trim_user=None, map_=None, tweet_mode=None): 
from tweepy.utils import list_to_csv 
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' 
""" 
from tweepy.binder import bind_api 
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 
) 

我想customtweepy到行为酷似tweepy不只是我的呼唤:

tweets = api.statuses_lookup(id_batch, tweet_mode='extended') 

它将使用功能从我customtweepy代替

但是当我更换tweepy和刚进口customtweepy:

import customtweepy 
# https://github.com/tweepy/tweepy 

auth = customtweepy.OAuthHandler(keys['consumer_key'], keys['consumer_secret']) 
auth.set_access_token(keys['access_token'], keys['access_token_secret']) 
api = customtweepy.API(auth) 
. 
. 
tweets = api.statuses_lookup(id_batch, tweet_mode='extended') 
#added extra argument 

我得到下面的错误:

tweets = api.statuses_lookup(id_batch, tweet_mode='extended') 
TypeError: statuses_lookup() got an unexpected keyword argument 'tweet_mode' 

回答

0

可以继承API类,并添加你的方法吧。 采用下面的伪代码来实现所需的输出:

import tweepy 
from tweepy.binder import bind_api 
from tweepy.utils import list_to_csv 


class ModifiedApi(tweepy.API): 

    def __init__(self, *args, **kwargs): 
     super().__init__() 

    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): 
     # maybe params needed to be scpecified 
     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 
     ) 

主要模块:

import tweepy 
import customtweepy 
# https://github.com/tweepy/tweepy 

auth = tweepy.OAuthHandler(keys['consumer_key'], keys['consumer_secret']) 
auth.set_access_token(keys['access_token'], keys['access_token_secret']) 
api = customtweepy.ModifiedAPI(auth) 
. 
. 
tweets = api.statuses_lookup(id_batch) 
+0

我不知道怎么类成分影响的代码。这是否意味着我需要导入customtweepy.ModifiedApi或其他东西,并失去其余的tweepy功能? – user3120554

+0

您不会失去其他功能,因为类是从tweepy.API继承的。 tweepy.API的每个方法都保持不变,除非被覆盖的'statuses_lookup'和'_statuses_lookup' –

+0

@ user3120554如果你需要一个类继承如何工作的例子 - 我可以在我的答案中提供它。但是,我认为,你最好阅读[tutotrial](http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/) –