简体   繁体   English

可视化 Python function 流(例如作为树或概念图)

[英]Visualize Python function flow (e.g. as tree or concept map)

I have taught myself python in quite a haphazard way.我以非常随意的方式自学了 python。 So my question perhaps won't be very pythonic.所以我的问题可能不会很pythonic。

When I write functions in classes, I often lose overview of what each function does.当我在类中编写函数时,我经常忘记每个 function 的作用。 I do try to name them properly.我确实尝试正确命名它们。 But still, they are sometimes smaller parts of code where it seems arbitrary in which functions to put them.但是,它们有时仍然是代码的较小部分,在哪些函数中放置它们似乎是任意的。 So whenever I want to make changes, I still need to go through the entire code in order to figure out how my functions actually flow.因此,每当我想进行更改时,我仍然需要通过整个代码 go 来弄清楚我的函数实际上是如何流动的。

From what I understand, we have objects and we have functions, and these are our units for structuring our code.据我了解,我们有对象和函数,这些是我们构建代码的单元。 But this only gives you a flat structure.但这只会给你一个扁平的结构。 It doesn't allow you to do any kind of nesting, like in a tree diagram, over multiple levels.它不允许您在多个级别上进行任何类型的嵌套,例如在树形图中。 Especially, the code in my file doesn't automatically water itself so that the functions that are called first and more often automatically further up on the top, whereas helper functions would be automatically further down in the document, or even nested.特别是,我的文件中的代码不会自动浇灌自身,因此首先调用的函数并且更频繁地自动位于顶部,而辅助函数将自动位于文档的更下方,甚至嵌套。

In fact, even being able to visually nest lower-order subroutines "inside" a higher-order function that calls it would seem helpful.事实上,即使能够在视觉上将低阶子例程嵌套在调用它的高阶 function 中似乎也很有帮助。 But it's not something that would be supported by Python's syntax.但这不是 Python 语法支持的东西。 (Plus, it wouldn't quite suffice, because a sub-routine might be used by several higher-order functions.) (另外,这还不够,因为一个子例程可能会被几个高阶函数使用。)

I imagine it would be useful to see all functions in my code visualized as a tree, or as a concept map:我想将代码中的所有函数可视化为树或概念 map 会很有用:

Where each function is a dot.其中每个 function 是一个点。 And calling order visualized by arrows.并通过箭头显示调用顺序。 This way, I would also easily see which functions are more central, and which are more outliers, or even orphaned.这样,我也可以很容易地看到哪些函数更中心化,哪些函数更离群,甚至是孤立的。

Yet perhaps this isn't even a case for another tool.然而,也许这甚至不是另一种工具的情况。 Perhaps this is more a case of me not understanding proper coding.也许这更多是我不了解正确编码的情况。 Perhaps there is something I can do differently in order to get the kind of intuitive overview over how my program works, without needing another tool.也许我可以做一些不同的事情,以便对我的程序如何工作有一种直观的了解,而无需其他工具。

Firstly, I am not quite sure why this isn't asked more often.首先,我不太清楚为什么不经常问这个问题。 Reading code is not intuitive, at all.阅读代码一点也不直观。 We should be able to visualize the evolution of a process or function so well that close to every one will be able to understand its behavior, In the 60s and so, people had to be reasonably sure their programs would run;我们应该能够很好地可视化进程或 function 的演变,以至于几乎每个人都能够理解它的行为,在 60 年代左右,人们必须合理地确定他们的程序会运行; because getting access to the computer would take time, today we execute or compile our program, run tests if we have them.因为访问计算机需要时间,所以今天我们执行或编译我们的程序,如果有测试就运行测试。 and get to know immediately whether it works, What happened is there is less mental effort now, we execute a bit less code in our heads.并立即了解它是否有效,发生的事情是现在的脑力劳动减少了,我们在头脑中执行的代码减少了。 and the computer a bit more.和电脑多一点。 But we must still think of how the program behaves midst execution in order to debug, For the future.但是我们仍然必须考虑程序在执行过程中的行为以便调试,以备将来使用。 it would be nice if the computer could just tell us how the program behaves.如果计算机能告诉我们程序是如何运行的,那就太好了。

You propose looking at a sort of tree of the program as a resolute, and after all, the abstract syntax tree is literally a tree, but I don't think this is what we ought to spend our efforts on when it comes to visualizing systems.您建议将程序的一种树视为一种果断,毕竟抽象语法树实际上是一棵树,但我认为这不是我们在可视化系统时应该花费的精力. What would be preferable is if we could look at an interactive view of how the problem changes its intermediate data-structures as a function of time.更可取的是,如果我们可以查看问题如何将其中间数据结构更改为时间 function 的交互式视图。

for循环的演变

Currently, we have debuggers--but that's akin to looking at the issue by asking what a function is at many values, when we would much rather look at its graph.目前,我们有调试器——但这类似于通过询问 function 在许多值上是什么来查看问题,而我们更愿意查看它的图表。 A lot of programming is done by doing something you feel is right, observing if the behavior correct, if it's not correct then we make modifications by reacting and correcting said behavior.很多编程都是通过做一些你认为正确的事情来完成的,观察行为是否正确,如果它不正确,那么我们通过反应和纠正所述行为来进行修改。

Bret Victor in his essay, Learnable Programming , discusses this topic. Bret Victor 在他的文章Learnable Programming中讨论了这个话题。 I highly recommend it, even though it won't help you right now, maybe you can help others in the future by making these ideas more prevalent.我强烈推荐它,即使它现在对您没有帮助,也许您可以通过使这些想法更普遍来帮助其他人。

Onwards, then to where I tell you what you can do right now.之后,然后到我告诉你你现在可以做什么的地方。 In his book Clean Code, Robert C. Robert C 在他的《清洁代码》一书中。 Martin proposes structuring code much like how a newspaper is laid out. Martin 建议将代码结构化,就像报纸的布局方式一样。

Think of a well-written newspaper article.想想一篇写得很好的报纸文章。 You read it vertically.你竖着读。 At the top you expect a headline that will tell you what the story is about and allows you to decide whether it is something you want to read.在顶部,您希望有一个标题,它将告诉您故事的内容,并允许您决定是否要阅读它。 The first paragraph gives you a synopsis of the whole story, hiding all the details while giving you the broad-brush concepts.第一段为您提供了整个故事的概要,隐藏了所有细节,同时为您提供了粗略的概念。 As you continue downward, the details increase until you have all the dates, names, quotes, claims, and other minutia.随着您继续向下,详细信息会增加,直到您拥有所有日期、姓名、报价、声明和其他细节。

What is proposed, is to organize your program top-down, with higher level procedures that call mid-level procedures, which in turn call the lower level procedures.建议是自上而下地组织您的程序,其中较高级别的程序调用中级程序,而中级程序又调用较低级别的程序。 At any place, it should be obvious that (1) you are at the appropriate level of abstraction, and (2) you are looking at the part of the program implementing the behavior you seek to modify.在任何地方,很明显(1)您处于适当的抽象级别,并且(2)您正在查看实现您想要修改的行为的程序部分。

This means storing state at the level where it belongs, and exposing it anywhere else.这意味着将 state 存储在它所属的级别,并将其暴露在其他任何地方。 Procedures should take only the parameters they need, because more parameters means you must also think about more parameters when reasoning about the code.过程应该只取它们需要的参数,因为更多的参数意味着你在推理代码时也必须考虑更多的参数。

This is the primary reason for decomposing procedures into smaller procedures.这是将程序分解为更小的程序的主要原因。 For example, here some code I've written previously.例如,这里有一些我之前写过的代码。 It's not perfect, but you can see very clearly which part of the program you need to go to if you want to change anything.它并不完美,但是如果您想更改任何内容,您可以非常清楚地看到您需要 go 到程序的哪个部分。

Of course, higher order procedures are listed before any other.当然,高阶程序列在任何其他程序之前。 I'm telling you what I'm going to do, before I show you how I do it.我告诉你我要做什么,然后再告诉你我是怎么做的。

function formatLinks(matches, stripped) {
  let formatted_links = []
  for (match of matches) {
      let diff = difference(match, stripped)

      if (isSimpleLink(diff)) {
          formatted_links.push(formatAsSimpleLink(diff))
      } else if (hasPrefix(diff)) {
          formatted_links.push(formatAsPrefixedLink(diff))
      } else if (hasSuffix(diff)) {
          formatted_links.push(formatAsSuffixedLink(diff))
      }
  }

  // There might be multiple links within a word
  // in which case we join them and strip any duplicated parts
  // (which are otherwise interpreted as suffixes)
  if (formatted_links.length > 1) {
      return combineLinks(formatted_links)
  }

  // Default case
  return formatted_links[0]
}

If JavaScript was a typed language, and if we could see an image of the decisions made in the code, as a factor of input and time, this could be even better.如果 JavaScript 是一种类型化语言,并且如果我们可以看到代码中做出的决定的图像,作为输入和时间的因素,这可能会更好。

I think Quokka.js and VS Code Debug Visualizer are both doing interesting work in this sector.我认为Quokka.jsVS Code Debug Visualizer都在这个领域做着有趣的工作。

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

相关问题 Python:如何创建一个函数? 例如 f(x) = ax^2 - Python: How to create a function? e.g. f(x) = ax^2 python中any()函数的对立面是什么,例如没有任何 - What is the opposite of any() function in python, e.g. without any 用python进行窗口式写入,例如写入NetCDF - Windowed writes in python, e.g. to NetCDF 如何使用python和selenium从地图(例如Pokevision)上刮取GIS坐标? - How to scrape GIS coordinates from a map (e.g. Pokevision) using python and selenium? 模糊正则表达式(例如 {e&lt;=2})在 Python 中的正确用法 - Fuzzy regex (e.g. {e<=2}) correct usage in Python 使用 python 循环更改 function 中的部分变量名称(例如,在末尾插入不同的数字) - Using python loop to change part of variable name within function (e.g. inserting different number at end) 如何矢量化 python 中包含限制的 function(例如 sin(x)/x) - How to vectorize a function in python that includes a limit (e.g. sin(x)/x) 我是否可以将所有python脚本都缓存字节码,例如在主目录中的中央目录树下? - Can I have all of my python scripts cached bytecode, e.g. under a central directory tree in my home directory? 返回具有动态变量名称的 function 以用于例如 lmfit - Returning a function with dynamic variable names for use in e.g. lmfit 通过kwargs添加功能选项(例如,详细) - Adding function options via kwargs (e.g., verbose)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM