2017-10-15 60 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/) –