python模块导入问题
所以我有一个包含许多模块的大项目,我想对其运行一些分析。我有一个配置文件模块,它基本上只提供一个装饰器,我可以将其添加到函数中以在调用时对其进行分析。
问题是,我必须将该模块导入到我拥有的数十个模块中。我想这很好,但我无法将导入的分析模块或函数上的装饰器的代码推送到版本控制。这意味着每次导入/导出时,我都必须添加/删除所有分析代码。
是否有任何类型的系统可以管理这种添加/删除分析代码,而无需手动导入/删除项目的每个模块中的模块?我们使用 Mercurial,但我不能真正搞乱 Mercurial 设置或创建分支。
So I have a big project with many modules in it, and I want to run some profiling on it. I have a profile module that basically just provides a decorator that I can add to a function to profile it when it's called.
The problem is, I'll have to import that module into the dozens of modules that I have. This is fine I guess, but I can't push the code with the profiling modules imported or the decorator on the functions to version control. This means every time I import/export, I have to add/remove all my profiling code.
Is there any kind of system to manage this adding/removing of profiling code without manually importing/deleting the modules in every module of my project? We use mercurial, but I can't really mess with mercurial settings or make a branch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建分析模块,以便导入其他模块并注释它们的功能:
这样您就不必修改这些模块,但如果您选择在运行时的任何地方导入此分析模块,它将“修补”所有模块您想要的其他模块
您应该能够类似地装饰类方法。
You could create your profiling module such that it imports your other modules and annotates their functions:
This way you don't have to modify those modules, but if you choose to import this profiling module anywhere at runtime, it will "patch" all the other modules as you'd like
You should be able to decorate class methods similarly.
为什么不直接创建一个包含所有分析更改的分支,然后在您想要测试时合并该分支……然后恢复所有内容。
git 通过“git stash”使这变得更容易,但是使用分支应该不会变得更加困难。
Why not just create a branch with all the profiling changes, and then merge the branch when you want to test ... and revert everything afterwards.
git makes this somewhat easier with it's "git stash", but using branches shouldn't be significantly harder.