V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Oct 15, 2015 · 10179 views
    This topic created in 3849 days ago, the information mentioned may be changed or developed.

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

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

    7 replies    2015-10-16 10:16:08 +08:00
    wmttom
        1
    wmttom  
       Oct 15, 2015
    用 monkey_patch 的方式吧
    ynztyl10
        2
    ynztyl10  
    OP
       Oct 15, 2015
    @wmttom 高级魔术啊
    doyoubi
        3
    doyoubi  
       Oct 15, 2015
    改动比较少的可以用 mock 库的 patch
    deepreader
        4
    deepreader  
       Oct 15, 2015
    @wmttom 黑魔法
    ahxxm
        5
    ahxxm  
       Oct 15, 2015
    2 比较好点, python 第三方库感觉质量普遍不高,还不如自己定制,既减少依赖又容易维护
    a358003542
        6
    a358003542  
       Oct 15, 2015
    看源码,重构。都有自己的特殊需求这个高级的要求了,还满足 patch ?那些 python 第三方模块又不是什么特别复杂的东西。
    xiazi
        7
    xiazi  
       Oct 16, 2015
    如果改得地方不多, 可以这样:
    先 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
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5874 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 06:29 · PVG 14:29 · LAX 23:29 · JFK 02:29
    ♥ Do have faith in what you're doing.