forth语法中的可编程调试器。设置一个断点以对所有内容进行X光检查。
peforth的Python项目详细描述
向前推进
一个可编程的python调试器允许您突然设置过程,以便在断点处动态地研究程序代码。
你们已经知道怎么做了。我们都知道。 但说到机器学习和Tensorflow之类的东西, 事情越来越烦人了。一个可编程的调试器是我和你的想法。一个断点调查一切!在这一点上,你可以 然后测试你想要的,由所有的力量支持。
forth语法中的调试命令
所以现在我们需要选择一个交互式ui及其语法 重量轻,可靠和灵活,所以我们不会后悔选择它。 总有一天,在那里已经有几十年了,所以很多人不需要了解 另一种新的语言虽然我们只是使用一些调试命令,但是很容易 对于新用户来说,已经足够了。
安装peforth
pip install peforth
对于jupyter笔记本用户,我们可以使用forth语言通过peforth magics%f和%f来调查python对象。对于教程,请在github上的这个项目的“notebook”目录中找到并阅读jupyter笔记本。
运行peforth:
打印“你好,世界!“
Microsoft Windows [Version 10.0.15063] (c) 2017 Microsoft Corporation. All rights reserved. c:\Users\your-working-folder>python -m peforth .' Hello World!!' cr bye Hello World!! c:\Users\your-working-folder>
所以你的peforth很好。 对于您的应用程序,import peforth像往常一样引入调试器:
c:\Users\your-working-folder>python Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import peforth p e f o r t h v1.07 source code http://github.com/hcchengithub/peforth Type 'peforth.ok()' to enter forth interpreter, 'exit' to come back. >>>
打招呼的信息告诉我们如何为您的 调试或调查以及如何返回以继续运行 代码。
让我们试着调试程序
# 100.py sum = 0 for i in range(100): sum += i print("The sum of 1..100 is ", sum)
运行:
c:\Users\your-working-folder>python 100.py The sum of 1..100 is 4950 c:\Users\your-working-folder>
结果应该是5050,但不是!让我们放弃一个断点 看看有什么问题:
# 100.py with breakpoing .----- Specify an unique command prompt to indicate where | the breakpoint is from if there are many of them import peforth | .----- pass locals() at the breakpoint sum = 0 | | to our debugger for i in range(100): | | .------- use a FORTH constant sum += i | | | to represent the locals() peforth.ok('my first breakpoint> ',loc=locals(),cmd="constant locals-after-the-for-loop") print("The sum of 1..100 is ", sum)
再次运行:
c:\Users\your-working-folder>python 100.py p e f o r t h v1.07 source code http://github.com/hcchengithub/peforth Type 'peforth.ok()' to enter forth interpreter, 'exit' to come back. .--------------- at the breakpoint, type in 'words' | command to see what have we got my first breakpoint> words .-------- It's a long list of 'words' ... snip ....... | or available commands. Don't worry, we'll use only some of them. expected_rstack expected_stack test-result [all-pass] *** all-pass [r r] [d d] [p p] WshShell inport OK dir keys --- locals-after-the-for-loop | The last one is what ------' we have just created throuth the breakpoint statement , named "locals-after-the-for-loop"
让我们看看:
print a carriage return at the end -------. print the thing -----. | | | my first breakpoint> locals-after-the-for-loop . cr ({'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001DD2D737710>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '100.py', '__cached__': None, 'peforth': <module 'peforth' from 'C:\\Users\\hcche\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\pe forth\\__init__.py'>, 'sum': 4950, 'i': 99}, {}, 'my first breakpoint> ') my first breakpoint> | | | | | '--- our command our sum -----' | prompt | indicates where the 99 instead of 100 ----------' breakpoint is from this is the problem !!
现在离开断点,让程序继续:
my first breakpoint> exit my first breakpoint> The sum of 1..100 is 4950 c:\Users\your-working-folder>
在断点处进行实验进行调查
在tensorfow教程的断点处,我总是想 在那些令人沮丧的tf.something(tf.something(…),…) 对它们有更清楚的了解 不离开基础教程。我们用上面的例子 再次以另一种方式演示如何使用peforth:
运行peforth:
Microsoft Windows [Version 10.0.15063] (c) 2017 Microsoft Corporation. All rights reserved. c:\Users\your-working-folder>python Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import peforth p e f o r t h v1.07 source code http://github.com/hcchengithub/peforth Type 'peforth.ok()' to enter forth interpreter, 'exit' to come back. >>> peforth.ok() OK <-------- Default FORTH command prompt OK
假设我们在一个断点,我们需要一个过程来 加1..100得到它们的和。我们不确定这个过程 是正确的,所以我们需要尝试。现在复制程序 你的文本编辑器。` <py>...</py> `告诉调试器 其中的代码是一个内嵌python块。 `outport()`函数将给定的`locals()`输出到 内嵌python块之外的第四个环境。
<py> sum = 0 for i in range(100): sum += i print("The sum of 1..100 is ", sum) outport(locals()) </py>
这是一个由多行文本字符串组成的块,所以我们按ctrl-d 要开始多行输入,请复制粘贴,然后按另一个CTRL-D键 结束多行块。像这样:
OK OK ^D <py> sum = 0 for i in range(100): sum += i print("The sum of 1..100 is ", sum) outport(locals()) </py> ^D The sum of 1..100 is 4950 OK
现在使用“words”命令查看我们得到了什么:
OK words code end-code \ // <selftest> </selftest> bye /// immediate stop compyle trim indent -indent <py> </py> </pyV> words . cr help interpret-only compile-only literal reveal privacy (create) : ; ( BL CR word ' , [compile] py: py> py:~ py>~ 0branch here! here swap ! @ ? >r r> r@ drop dup over 0< + * - / 1+ 2+ 1- 2- compile if then compiling char last version execute cls private nonprivate (space) exit ret rescan-word-hash (') branch bool and or not (forget) AND OR NOT XOR true false "" [] {} none >> << 0= 0> 0<> 0<= 0>= = == > < != >= <= abs max min doVar doNext depth pick roll space [ ] colon-word create (marker) marker next abort alias <> public nip rot -rot 2drop 2dup invert negate within ['] allot for begin until again ahead never repeat aft else while ?stop ?dup variable +! chars spaces .( ." .' s" s' s` does> count accept accept2 <accept> nop </accept> refill [else] [if] [then] (::) (:>) :: :> ::~ :>~ "msg"abort abort" "msg"?abort ?abort" '<text> (<text>) <text> </text> <comment> </comment> (constant) constant value to tib. >t t@ t> [begin] [again] [until] [for] [next] modules int float drops dropall char>ASCII ASCII>char ASCII .s (*debug*) *debug* readTextFile writeTextFile tib.insert sinclude include type obj>keys obj2dict stringify toString .literal .function (dump) dump dump2ret d (see) .members .source see dos cd slice description expected_rstack expected_stack test-result [all-pass] *** all-pass [r r] [d d] [p p] WshShell inport OK dir keys --- i sum OK
在长列表末尾的` --- `标记后面,我们找到了` i `和 ` sum `。它们都是内联python块中某个点上的locals()。 让我们看看他们:
OK i . cr 99 OK sum . cr 4950 OK
再次,我们找到了为什么总和不是5050的根本原因,因为 ` i `未达到预期的100。这正是 python`range()`可以工作,这实际上让我困惑了很多次。
访问此项目的 Wiki 页 有关如何查看mnist手写数字图像的更多示例 在TensorFlow教程的研究过程中, 示例,以及此可编程调试器的用法。