python:重写导入的类以扩展功能

时间:2017-10-15 04:00:53

标签: python python-3.x

我正在运行以下代码:

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和inside(我刚刚添加了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'

1 个答案:

答案 0 :(得分: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)