在调试控制台中创建新变量

huangapple go评论87阅读模式
英文:

Create new variables in debug console

问题

这是一个关于Julia的问题。但为了提供一些背景信息,我不是最优秀的程序员,所以我的Python工作流程是在函数中编写一些不完整的代码,然后在PyCharm中调试代码,使用交互式调试控制台来帮助我找出如何完成函数。例如:

def cartesian_product():
   a = ['1', '2', '3']
   b = ['a', 'b', 'c', 'd']
   
   # 我想计算两个列表的笛卡尔积,但我不太知道如何做
   # 所以我在这里进行了一些谷歌搜索,然后在调试器中调试,探索各种方法,
   # 最终选择了itertools.product()方法。

   return list(itertools.product(a, b))

现在我想知道在Julia中是否有类似的方法,使用Visual Studio Code?

当我创建半完成的Julia函数时:

function cartesian_product()
    a = ['1', '2', '3']
    b = ['a', 'b', 'c', 'd']

    # 在这里附加调试器并尝试使用VS Code的交互式调试器来找出其余部分
end

这种方法的问题是我无法在调试控制台中创建新变量。可能是因为Julia是一种编译语言?

例如,如果我在b语句处设置断点,变量a已经加载到内存中,但b没有。因此,在调试控制台中,我定义了b,就像在Python的调试控制台中一样。但现在,当我尝试引用b时,我会得到一个UndefVarError: b not define的错误。

所以我的问题是,如果这种工作流程不可行(也就是在调试控制台中找出问题),那么有什么替代方法吗?

我尝试过以下方法:

  1. .jl文件中编写代码,然后在REPL中运行它们,但当你有很多自定义模块和函数(设置代码)在你感兴趣的代码之前运行时,这会变得混乱。
  2. 简单看了一下revise,但我认为它不完全满足我的需求。

我是不是只能忍受并采用不同的编程方法?

英文:

This is a Julia related question. But to give some context, I'm not the best programmer, so my python workflow is to write some half complete code in a function, then debug the code in pycharm and use the interactive debug console to help me figure out how to complete the function. For example

def cartesian_product():
   a = ['1', '2', '3']
   b = ['a', 'b', 'c', 'd']
   
   # I want to compute the cartesian product of two lists but I don't quite know how 
   # it's done so I google a bit attach the debugger here and explore the various 
   # approaches eventually settling with the itertools.product() approach. 

   return list(itertools.product(a, b))

Now I was wondering if there is a similar approach possible in Julia using visual studio code?

When I create my semi complete julia function

function cartesian_product()
    a = ['1', '2', '3']
    b = ['a', 'b', 'c', 'd']

    # attach debugger here and try figure out the rest using the vs code interactive 
    # debugger 
end

The problem with this approach is that I can't create new variables in the debug console. Possibly because julia is a compiled language?

For example if I set a breakpoint at the b statement, the variable a has loaded in memory but b has not. So in the debug console I define b just like in the python debug console. But now when I try to referrence b I get an UndefVarError: b not define

在调试控制台中创建新变量

So my question is if this type of workflow is not possible (that is figure things out in the debug console), what are the alternatives?

I've tried these approaches:

  1. Write code in .jl files. Then run them in the REPL - but this kind of get's messy when you have lot's of custom modules and functions (setup code) that run before the point of the code you are interested in.
  2. Had a quick look at revise but I don't think it quite does what I want.

Do I just have to suck it up and adopt a different approach to programming?

答案1

得分: 2

如果您想在交互式环境中进行实验或快速编写代码,您可能希望使用Julia REPL 作为您的调试控制台,特别是如果您有时间学习一些更高级的功能。

但是,如果您的代码已经跨越了多个模块,那么它应该已经有自己的.jl文件,如果没有,那么应该有自己的包。如果您正在积极开发的代码依赖于这些模块和函数,那就没有绕过它的办法,但您可以抑制输出以使其更整洁。

英文:

If you want to experiment or code something up quickly in an interactive environment, you may want to use the Julia REPL as your debug console, especially if you have time to learn some of its more advanced features.

But if your code already spans several modules, it should already have its own .jl file, if not its own package. If the code you're actively working on depends on those modules and functions, there's no getting around that, but you can supress output to make it less messy.

答案2

得分: 2

你可以尝试使用Debugger,然后你可以使用@enter宏:

julia> @enter cartesian_product()
In cartesian_product() at REPL[2]:1
 1  function cartesian_product()
>2      a = ['1', '2', '3']
 3      b = ['a', 'b', 'c', 'd']
 4
 5      # 在这里附加调试器,尝试使用VS Code互动来解决剩下的部分
 6      # 调试器
 7  end

About to run: (Base.vect)('1', '2', '3)
1|debug>

使用这个工具,你可以与正在运行的代码互动,特别是可以操作变量的值(要切换到Julia模式,请按撇号```和ENTER)。看看这个示例会话:

julia> function printc()
       c = 5
       println(c)
       end
printc (generic function with 1 method)

julia> @enter printc()
In printc() at REPL[5]:1
 1  function printc()
 2  c = 5
>3  println(c)
 4  end

About to run: (println)(5)
1|julia> c = 17
17

1|debug> n
17
In printc() at REPL[5]:1
 1  function printc()
 2  c = 5
>3  println(c)
 4  end

About to run: return
1|debug>

你可以看到c变量被修改了。

所有选项的详细信息在https://github.com/JuliaDebug/Debugger.jl中有描述。

另一个好的方法是使用Visual Studio Code和断点,如https://www.julia-vscode.org/docs/stable/userguide/debugging/中所述。在IDE调试器中,你可以在运行时更改变量的值 - 只需在断点上停止时双击变量即可。

英文:

You could try using Debugger than you can use @enter macro:

julia> @enter cartesian_product()
In cartesian_product() at REPL[2]:1
 1  function cartesian_product()
>2      a = ['1', '2', '3']
 3          b = ['a', 'b', 'c', 'd']
 4
 5      # attach debugger here and try figure out the rest using the vs code interactive
 6      # debugger
 7  end

About to run: (Base.vect)('1', '2', '3')
1|debug>

With this tool you can interact with running code and in particular manipulate the values of variables (to go to julia mode press apostrophe ``` and ENTER). See this sample session:


julia> function printc()
       c=5
       println(c)
       end
printc (generic function with 1 method)

julia> @enter printc()
In printc() at REPL[5]:1
 1  function printc()
 2  c=5
>3  println(c)
 4  end

About to run: (println)(5)
1|julia> c=17
17

1|debug> n
17
In printc() at REPL[5]:1
 1  function printc()
 2  c=5
>3  println(c)
 4  end

About to run: return
1|debug>

You can see that the c variable was mutated.

The full set of options is described at https://github.com/JuliaDebug/Debugger.jl.

Another good route is to use Visual Studio Code and breakpoints as described in https://www.julia-vscode.org/docs/stable/userguide/debugging/
In IDE debugger you can change variable values in the runtime - just double click the variable when being stopped on a breakpoint:

在调试控制台中创建新变量

huangapple
  • 本文由 发表于 2023年2月8日 21:48:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定