简体   繁体   English

如何调试/拆分/从别人的Python代码(基于Web)中学习?

[英]How do you Debug/Take Apart/Learn from someone else's Python code (web-based)?

A good example of this is: http://github.com/tav/tweetapp/blob/a711404f2935c3689457c61e073105c1756b62af/app/root.py 一个很好的例子是: http : //github.com/tav/tweetapp/blob/a711404f2935c3689457c61e073105c1756b62af/app/root.py

In Visual Studio (ASP.net C#) where I come from, the classes are usually split into separate files + I can set break points to understand the code level. 在我来自的Visual Studio(ASP.net C#)中,通常将类拆分为单独的文件+我可以设置断点以了解代码级别。

If I run a program like this, do I just do "system.out" to print out where in the code I am in? 如果我运行这样的程序,是否只执行“ system.out”以打印出代码所在的位置?

I read through this https://stackoverflow.com/questions/246546/good-techniques-for-understanding-someone-elses-code which was quite helpful. 我阅读了这个https://stackoverflow.com/questions/246546/good-techniques-for-understanding-someone-elses-code ,这很有帮助。

You've run into a pretty specific case of code that will be hard to understand. 您已经遇到了很难理解的特定代码案例。 They probably did that for the convenience of having all the code in one file. 他们可能这样做是为了将所有代码都放在一个文件中。

I would recommend letting epydoc have a pass at it. 我建议让epydoc通过。 It will create HTML documentation of the program. 它将创建程序的HTML文档。 This will show you the class structure and you can even build charts of which functions call which other functions. 这将向您显示类结构,您甚至可以构建哪些函数调用哪些其他函数的图表。

http://epydoc.sourceforge.net/manual-usage.html http://epydoc.sourceforge.net/manual-usage.html

Your other options are to break it into multiple files yourself (which I think will be tedious and not of much benefit) 您的其他选择是自己将其分为多个文件(我认为这很乏味,不会带来太大的好处)

If you install Eclipse and PyDev you can set breakpoints in the same way you can in visual studio. 如果安装EclipsePyDev ,则可以像在Visual Studio中一样设置断点。

Failing that, printing out information at cucial points is often a good way to see what's going on. 失败的话,在关键点打印信息通常是了解正在发生的事情的好方法。 I quite often add in debug information that way and leave it in the code but disabled until I change a variable. 我经常以这种方式添加调试信息,并将其保留在代码中,但是在更改变量之前被禁用。 I find this often helps if you break the code and need to go back and take another look at what's going on. 我发现如果您破坏了代码并需要回头再看看发生了什么,这通常会有所帮助。 Better still, send your debug information to a logging class and you can start to use the output in unit tests... you do test your code right? 更好的是,将调试信息发送到日志记录类,然后就可以开始在单元测试中使用输出了……您对代码进行了测试吗? ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM