vim折叠python代码
Recently I was working on some C# and Java code. And along the way, I used Python and Vim to (re)write my code. A small Python script and a 6-keystroke Vim macro did it faster and better than a human would.
最近,我正在研究一些C#和Java代码。 在此过程中,我使用Python和Vim来(重新)编写代码。 一个小的Python脚本和一个6键的Vim宏比人类更快,更好。
Every programmer should learn a good scripting language and use a programmable editor like Vim. Why? Here are two examples, after the break.
每个程序员都应该学习一种好的脚本语言,并使用像Vim这样的可编程编辑器。 为什么? 休息后,有两个例子。
第I集: INotifyPropertyChanged或Python编写C# (Episode I: INotifyPropertyChanged, or Python writing C#)
I was building a private C# weekend project (that turned into a weeklong project) — and by the way, WPF and C# are quite pleasant (Windows Forms is a trainwreck, though). One of the things I used in that project was a DataGrid bound to a list of custom objects (a DataGrid is a table, basically). And in order to use it, you need to use the INotifyPropertyChanged interface (MSDN). It involves doing something like this:
我当时正在构建一个私人的C#周末项目(该项目变成了一个为期一周的项目),而且,WPF和C#非常令人愉快(尽管Windows Forms令人不寒而栗)。 我在该项目中使用的一件事是绑定到自定义对象列表的DataGrid(基本上,DataGrid是一个表)。 为了使用它,您需要使用INotifyPropertyChanged接口(MSDN) 。 它涉及到这样的事情:
private private string string name_ name_ { { getget ; ; setset ; ; }; }; // can also be a field
// can also be a field
[JsonProperty]
[JsonProperty]
public public string string name name {
{
get get {
{
return return name_name_ ;
;
}
}
set set {
{
if if (( value value != != name_name_ ) ) {
{
name_ name_ = = valuevalue ;
;
NotifyPropertyChangedNotifyPropertyChanged (( "name""name" );
);
}
}
}
}
}
}
That’s 12 lines of code (excluding [JsonProperty] which comes from the Json.NET library) for that pattern. Oh: and I need to do that for every field/property of my class, because otherwise any changes to them would not be reflected in the tables (and maybe one or two fields were not in the table).
那是该模式的12行代码(不包括来自Json.NET库的[JsonProperty] )。 哦:我需要对我班上的每个字段/属性都这样做,因为否则,对它们的任何更改都不会反映在表中(也许表中没有一个或两个字段)。
Doing that by hand is really not feasible: you need to copy-paste this large block 14 times and take care of 5 instances of the name (3 with underscores, 2 without), 2 instances of the type, and the [JsonProperty] attribute (which does not appear on all properties).
手动执行实际上是不可行的:您需要将这个大块复制粘贴14次,并注意5个名称实例(3个带下划线,2个不带下划线),2个类型的实例以及[JsonProperty]属性(并非出现在所有属性上)。
So, I used one of those intelligent computer things to do it for me. I wrote a really simple Python script and ran it. And I ended up with all 14 fields built for me.
因此,我用那些智能计算机中的一种来为我做。 我编写了一个非常简单的Python脚本并运行了它。 最后,我为我建立了所有14个领域。
code-writing-code/write_properties.py
That script takes a list of properties and spits out a block of code, ready to be pasted into the code. Visual Studio has a nice Insert File as Text feature, so redirecting the output to a file and using that option is enough.
该脚本获取属性列表,并吐出一段代码,准备将其粘贴到代码中。 Visual Studio具有出色的“以文本插入文件”功能,因此将输出重定向到文件并使用该选项就足够了。
第II集:固定参数顺序或Vim(重新)编写Java (Episode II: Fixing argument order, or Vim (re)writing Java)
Another project, Number Namer, written in Java, and it does what it says on the tin: takes a number and writes it out as words, while being multilingual and extensible. I used Eclipse for this project, because it looks good, is really helpful with its code linting, and does not run slowly on my aging system (I’m looking at you, IntelliJ IDEA aka PyCharm aka Android Studio…)
另一个用Java编写的项目Number Namer ,它的功能与在罐子上的说的一样:提取一个数字并将其以单词形式写出来,同时具有多种语言和可扩展性。 我在这个项目中使用了Eclipse,因为它看起来不错,它的代码插入确实很有帮助,并且在老化的系统上运行不慢(我在看着您,IntelliJ IDEA(又名PyCharm,又名Android Studio)…)
And so, I was building a test suite, using JUnit. It’s pretty straightforward, and I remember the syntax from Python’s unittest (even though I write tests with pytest nowadays). Or so I thought.
因此,我正在使用JUnit构建测试套件。 这非常简单,而且我还记得Python的unittest的语法(即使现在我用pytest编写测试)。 还是我想。
// (incorrect)
// (incorrect)
assertEqualsassertEquals (( "Basic integers (7) failed""Basic integers (7) failed" , , namernamer .. namename (( 7L7L ), ), "seven""seven" );
);
// (fixed) ^ cursor
// (fixed) ^ cursor
assertEqualsassertEquals (( "Basic integers (7) failed""Basic integers (7) failed" , , "seven""seven" , , namernamer .. namename (( 7L7L ));
));
You see, the typical Python spelling is self.assertEquals(actual, expected). Java adds a String message parameter and it also swaps actual and expected. Which I didn’t notice at first, and I wrote my assertions incorrectly. While it doesn’t really matter (it will still work), the output looked a bit weird.
您会看到,典型的Python拼写是self.assertEquals(actual,expected) 。 Java添加了String消息参数,并且还交换了实际和预期参数 。 起初我没有注意到,但是我错误地写了我的断言。 虽然并不重要(它仍然可以使用),但输出看起来有点奇怪。
And I noticed only when I finished writing my tests (and I had a typo in my expected output). I wanted to fix them all — not manually, of course. So, I closed this file, brought up Vim, searched for the motion I need (it’s t{char} — see :help t). And I ended up with this (cursor placed on the comma after the first argument):
而且我只有在完成测试编写后才注意到(预期输出中有错字)。 我想全部修复-当然不是手动修复。 因此,我关闭了该文件,打开了Vim,搜索了我需要的动作(它是t {char} -参见:help t )。 我最后得到了这个结果(将光标放在第一个参数之后的逗号上):
What does this do, you may ask? It’s actually pretty self-explanatory:
您可能会问,这是做什么的? 这实际上是不言自明的:
delete till comma, (go) till closing parenthesis, paste.
d elete 吨生病逗号,(GO) 吨生病闭括号, 第 ASTE。
This fixes one line. Automatically. Make it a macro (wrap in qq … q, use with @q) and now you can run it on all lines, either by moving manually or by searching for , and pressing n@q until you run out of lines.
这修复了一行。 自动。 将其设置为宏(用qq … q包装,与@q一起使用),现在您可以在所有行上运行它,方法是手动移动或搜索,然后按n @ q直到行数用完。
结语 (Epilogue)
Some of you might say “but VS/Eclipse/IDEA has an option for that somewhere” or “[expensive tool] can do that” — and a Google search shows that there is an Eclipse plugin to swap arguments and that I could also write a regex to solve my second issue. Nevertheless, Python is a great tool in a programmer’s toolbox — especially the interactive interpreter. And Vim is an awesome editor that can accomplish magic in a few keystrokes — and there are many more things you can do with it.
你们中的某些人可能会说“但是VS / Eclipse / IDEA可以在某个地方提供此选项”或“ [昂贵的工具]可以做到这一点”,而Google搜索显示有一个Eclipse插件可以交换参数,我也可以编写一个正则表达式来解决我的第二个问题。 尽管如此,Python是程序员工具箱中的一个很棒的工具-尤其是交互式解释器。 Vim是一个了不起的编辑器,可以通过几次击键来完成魔术操作,并且您可以用它做更多的事情。
Also: don’t even bother with VsVim or IdeaVim or any other Vim emulation plugins, they work in unusual ways and often don’t give you everything — for example, VsVim has a Vim visual mode (v key) and Visual Studio selection mode (mouse), and only one allows Vim keystrokes (the other will replace selected text).
另外:甚至不用理会VsVim或IdeaVim或任何其他Vim仿真插件,它们以不寻常的方式工作,并且通常无法为您提供一切-例如,VsVim具有Vim可视模式( v键)和Visual Studio选择模式(鼠标),只有一个允许Vim击键(另一个将替换选定的文本)。
翻译自: https://www.pybloggers.com/2016/05/code-writing-code-python-and-vim-as-development-aids/
vim折叠python代码