如何扩展python模块?在`python-twitter`包中添加新功能

use*_*045 18 python module python-module python-twitter tweepy

扩展现有Python模块的最佳实践是什么 - 在这种情况下,我想python-twitter通过向基本API类添加新方法来扩展包.

我看过了tweepy,我也喜欢这样; 我发现我python-twitter更容易理解并扩展我想要的功能.

我已经编写了方法 - 我正在试图找出将Pythonic和破坏性最小的方法添加到python-twitter包模块中,而不是改变这个模块的核心.

Riz*_*sim 21

几种方式.

简单的方法:

不要扩展模块,扩展类.

exttwitter.py

import twitter

class Api(twitter.Api):
    pass 
    # override/add any functions here.
Run Code Online (Sandbox Code Playgroud)

缺点:twitter中的每个类都必须在exttwitter.py中,即使它只是一个存根(如上所述)

一种更难(可能是非pythonic)的方式:

将*从python-twitter导入到您随后扩展的模块中.

例如 :

basemodule.py

 class Ball():
    def __init__(self,a):
        self.a=a
    def __repr__(self):
        return "Ball(%s)" % self.a

def makeBall(a):
    return Ball(a)

def override():
    print "OVERRIDE ONE"

def dontoverride():
    print "THIS WILL BE PRESERVED"
Run Code Online (Sandbox Code Playgroud)

extmodule.py

from basemodule import *
import basemodule

def makeBalls(a,b):
    foo = makeBall(a)
    bar = makeBall(b)
    print foo,bar

def override():
    print "OVERRIDE TWO"

def dontoverride():
    basemodule.dontoverride()
    print "THIS WAS PRESERVED"
Run Code Online (Sandbox Code Playgroud)

runscript.py

import extmodule

#code is in extended module
print extmodule.makeBalls(1,2)
#returns Ball(1) Ball(2)

#code is in base module
print extmodule.makeBall(1)
#returns Ball(1)

#function from extended module overwrites base module
extmodule.override()
#returns OVERRIDE TWO

#function from extended module calls base module first
extmodule.dontoverride()
#returns THIS WILL BE PRESERVED\nTHIS WAS PRESERVED
Run Code Online (Sandbox Code Playgroud)

我不确定extmodule.py中的双重导入是否是pythonic - 你可以删除它,但是你不会处理想要扩展basemodule命名空间中的函数的用例.

就扩展类而言,只需创建一个新的API(basemodule.API)类来扩展Twitter API模块.


Mik*_*ham 5

不要将它们添加到模块中。在您要扩展的类中创建子类,并在自己的模块中使用子类,而根本无需更改原始内容。


fis*_*000 5

您可以在运行时直接操作模块列表- 扰流板警报:您可以从types模块获取模块类型:

from __future__ import print_function
import sys
import types
import typing as tx

def modulize(namespace: tx.Dict[str, tx.Any],
             modulename: str,
             moduledocs: tx.Optional[str] = None) -> types.ModuleType:

    """ Convert a dictionary mapping into a legit Python module """

    # Create a new module with a trivially namespaced name:
    namespacedname: str = f'__dynamic_modules__.{modulename}'
    module = types.ModuleType(namespacedname, moduledocs)
    module.__dict__.update(namespace)

    # Inspect the new module:
    name: str = module.__name__
    doc: tx.Optional[str] = module.__doc__
    contents: str = ", ".join(sorted(module.__dict__.keys()))
    print(f"Module name:      {name}")
    print(f"Module contents:  {contents}")
    if doc:
        print(f"Module docstring: {doc}")

    # Add to sys.modules, as per import machinery:
    sys.modules.update({ modulename : module })

    # Return the new module instance:
    return module
Run Code Online (Sandbox Code Playgroud)

…然后您可以使用如下功能:

ns = {
         'func' : lambda: print("Yo Dogg"), # these can also be normal non-lambda funcs
    'otherfunc' : lambda string=None: print(string or 'no dogg.'),
      '__all__' : ('func', 'otherfunc'),
      '__dir__' : lambda: ['func', 'otherfunc'] # usually this’d reference __all__
}

modulize(ns, 'wat', "WHAT THE HELL PEOPLE")
import wat

# Call module functions:
wat.func()
wat.otherfunc("Oh, Dogg!")

# Inspect module:
contents = ", ".join(sorted(wat.__dict__.keys()))
print(f"Imported module name:      {wat.__name__}")
print(f"Imported module contents:  {contents}")
print(f"Imported module docstring: {wat.__doc__}")
Run Code Online (Sandbox Code Playgroud)

…当然,您也可以通过指定types.ModuleType为新声明的祖先来创建自己的模块子类class;我从来没有亲自觉得有必要这样做。

(另外,你不,从获得的模块类型types模块-你永远可以做这样的事情ModuleType = type(os)在导入后os-我特别指出的类型的这一个来源,因为它并不明显,不像许多其他内置的类型,Python不提供对全局命名空间中模块类型的访问。)

真正的动作在sys.modules字典中,在这里(如果您有足够的勇气),您可以替换现有模块,也可以添加新模块。