如何在Python包中安全地“伪造”模块
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在Python包中安全地“伪造”模块,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3166字,纯文字阅读大概需要5分钟。
内容图文
![如何在Python包中安全地“伪造”模块](/upload/InfoBanner/zyjiaocheng/795/1892ae0275b345f38f730826ffb37eba.jpg)
目前我在master git分支中有以下目录结构:
/dir1
__init__.py
module.py
这将改为(在我的分支中):
/dir1
__init__.py
/dir2
module1.py # With 70% of code of module.py
module2.py # With 30% of code of module.py
问题:
>我知道不可能让git跟踪两个新文件,但由于git识别重命名(并且它认为组织到文件夹中作为重命名),我将能够跟踪从主分支到我的分支的module1.py对module.py的更改. ,至少70%的代码(我必须手动更新module2.py).那么有更好的方法来解决这个问题吗?
>对于API一致性,我希望使用我的软件包的旧版本的人仍然可以使用dir1.module import abc(在dir1中没有module.py)这可以像here所描述的那样完成,但是这会带来危险弄乱sys路径变量,不建议考虑稳定性和安全性.有没有更好的方法可以使API向后兼容并且仍然安全/稳定?
但是,我的情况更复杂.有关更具代表性的示例,请考虑从以下位置移
/dir1
__init__.py
module_2.py
def add2(a, b):
return a + b
def sub2(a, b):
return a - b
module_3.py
def add3(a, b, c):
return a + b + c
至:
/dir1
__init__.py
/dir2
__init__.py
module_add.py
# ... Constitutes 70% of code from `dir1/module_2.py`
def add2(a, b):
return a + b
# A few more additional lines added from `dir1/module_3.py`
def add3(a, b, c):
return a + b + c
module_sub.py
# Constitutes the 30% from /dir1/module2.py
def sub2(a, b):
return a - b
所以本质上我正在拆分dir1 / module_2.py和dir1 / module_3.py的不同功能,并将它们重新组合成单??独的module_add.py和module_sub.py,并将它放在/ dir1 / dir2下
但是,获得版本2程序包的版本1用户仍应该能够:
from module_2 import add2, sub2
from module_3 import add3
我不能做的事情:
>在dir1中有module_2.py或module_3.py(我需要git来关联并跟踪主分支的dir1 / module_2.py到我的分支的dir1 / dir2 / module_2.py);
>以任何降低稳定性/安全性的方式改变或混乱sys.path;要么
>将dir2重命名为例如module_2.
解决方法:
请注意以下设置:
/dir1
__init__.py
from module import abc
module.py
abc = None
外部(几乎)无法区分:
/dir1
__init__.py
from module import abc
/module
__init__.py
from module1 import abc
module1.py # this is the moved and renamed module.py, with git history
abc = None
module2.py # this is the 30% you've factored out
# whatever's in here
从module.py/module外部,模块导入abc(以及dir1.module import abc等)的旧导入继续工作.
对于更复杂的示例,您仍然可以从以下位置切换:
/dir1
__init__.py
from module_2 import add2, sub2
from module_3 import add3
module_2.py
module_3.py
至:
/dir1
__init__.py
from dir2.module_add import add2, add3
from dir2.module_sub import sub2
/dir2
__init__.py
module_add.py # module_2.py moved & renamed
module_sub.py # module_3.py moved & renamed or new file
/module_2
__init__.py
from ..dir2.module_add import add2
from ..dir2.module_sub import sub2
/module_3
__init__.py
from ..dir2.module_add import add3
旧代码(例如来自dir1.module_2 import add2)仍然可以正常工作,但用户现在可以开始访问新位置(例如,来自dir1.dir2.module_add import add2,add3).
你也可以添加例如:
import warnings
warnings.warn("deprecated", DeprecationWarning)
到/ dir1 / module_2和/ dir1 / module_3中的__init__.py文件,向用户提供这些导入现在正在出路的警告.例如:
>>> import warnings
>>> warnings.simplefilter('always')
>>> from dir1.dir2.module_sub import sub2
>>> sub2(1, 2)
-1
>>> from dir1.module_3 import add3
Warning (from warnings module):
File "dir1\module_3\__init__.py", line 2
warnings.warn("deprecated", DeprecationWarning)
DeprecationWarning: deprecated
>>> add3(1, 2, 3)
6
内容总结
以上是互联网集市为您收集整理的如何在Python包中安全地“伪造”模块全部内容,希望文章能够帮你解决如何在Python包中安全地“伪造”模块所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。