V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ynztyl10
V2EX  ›  Python

python 修改第三方库的方法,有什么比较简单的办法啊?

  •  2
     
  •   ynztyl10 · 2015-10-15 09:38:10 +08:00 · 9089 次点击
    这是一个创建于 3115 天前的主题,其中的信息可能已经有所发展或是发生改变。

    由于业务需求,需要对第三方库的某些方法做修改。
    1.对于修改较多的模块,直接 fork 一份源码,修改之后再自己发布安装。
    2.对于改动较少的,就用继承类的方式,然后重写需要定制的方法。

    这两种方式都觉得复杂,第一种方式维护成本较大,第二种方式显得不够优雅

    7 条回复    2015-10-16 10:16:08 +08:00
    wmttom
        1
    wmttom  
       2015-10-15 09:47:38 +08:00
    用 monkey_patch 的方式吧
    ynztyl10
        2
    ynztyl10  
    OP
       2015-10-15 09:59:21 +08:00
    @wmttom 高级魔术啊
    doyoubi
        3
    doyoubi  
       2015-10-15 13:22:11 +08:00
    改动比较少的可以用 mock 库的 patch
    deepreader
        4
    deepreader  
       2015-10-15 14:10:11 +08:00
    @wmttom 黑魔法
    ahxxm
        5
    ahxxm  
       2015-10-15 15:04:41 +08:00
    2 比较好点, python 第三方库感觉质量普遍不高,还不如自己定制,既减少依赖又容易维护
    a358003542
        6
    a358003542  
       2015-10-15 22:08:00 +08:00
    看源码,重构。都有自己的特殊需求这个高级的要求了,还满足 patch ?那些 python 第三方模块又不是什么特别复杂的东西。
    xiazi
        7
    xiazi  
       2015-10-16 10:16:08 +08:00
    如果改得地方不多, 可以这样:
    先 git clone 下来, 然后直接修改代码, 修改完后生成 patch(git diff > patch).

    以后更新执行下面的命令就可以了:
    git reset --hard HEAD
    git pull
    git apply patch
    rm -rf build
    rm -rf dist
    python setup.py install
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2328 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 16:13 · PVG 00:13 · LAX 09:13 · JFK 12:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.