python和golang_Python,Ruby和Golang:命令行应用程序比较

python和golang

This is a guest blog post by Kyle Purdon, a software engineer in Boulder, CO. Kyle is a Python first developer with experience in Ruby, Golang, and many more languages. This post was originally authored on Kyle’s personal blog and included great discussion on Reddit.

这是一个客人博客文章由凯尔珀登 ,博尔德一名软件工程师,CO。凯尔是在Ruby中,Golang,越来越多的语言体验和Python的显影剂。 这篇文章最初是在Kyle的个人博客上撰写的,其中包括有关Reddit的精彩讨论。



python, ruby, and golang images

In late 2014 I built a tool called pymr. I recently felt the need to learn golang and refresh my ruby knowledge so I decided to revisit the idea of pymr and build it in multiple languages. In this post I will break down the “mr” (merr) application (pymr, gomr, rumr) and present the implementation of specific pieces in each language. I will provide an overall personal preference at the end but will leave the comparison of individual pieces up to you.

2014年底,我构建了一个名为pymr的工具。 最近,我感到有必要学习golang和更新我的Ruby知识,因此我决定重新审视pymr的想法,并以多种语言构建它。 在这篇文章中,我将分解“ mr”(merr)应用程序(pymr,gomr,rumr),并介绍每种语言中特定部分的实现。 最后,我将提供整体的个人喜好,但将各个部分的比较交给您。

For those that want to skip directly to the code check out the repo.

对于那些想直接跳到代码的人,请查看repo

应用结构 (Application Structure)

The basic idea of this application is that you have some set of related directories that you want to execute a single command on. The “mr” tool provides a method for registering directories, and a method for running commands on groups of registered directories. The application has the following components:

此应用程序的基本思想是,您具有要在其上执行单个命令的一组相关目录。 “ mr”工具提供了一种用于注册目录的方法,以及一种用于在已注册目录组上运行命令的方法。 该应用程序包含以下组件:

  • A command-line interface
  • A registration command (writes a file with given tags)
  • A run command (runs a given command on registered directories)
  • 命令行界面
  • 注册命令(使用给定标签写入文件)
  • 运行命令(在注册目录上运行给定命令)

命令行界面 (Command-Line Interface)

The command line interface for the “mr” tools is:

“ mr”工具的命令行界面为:

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9

To compare building the command-line interface let’s take a look at the register command in each language.

为了比较构建命令行界面,让我们看一下每种语言的register命令。

Python (pymr)

Python(pymr)

To build the command line interface in python I chose to use the click package.

为了在python中构建命令行界面,我选择使用click包。

1
1
2
2
3
3
4
4
5
5
6
6

Ruby (rumr)

Ruby(谣言)

To build the command line interface in ruby I chose to use the thor gem.

为了在ruby中构建命令行界面,我选择使用thor gem。

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16

Golang (gomr)

高朗(GOMR)

To build the command line interface in Golang I chose to use the cli.go package.

为了在Golang中构建命令行界面,我选择使用cli.go包。

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20
21
21
22
22

注册 (Registration)

The registration logic is as follows:

注册逻辑如下:

  1. If the user asks to --append read the .[py|ru|go]mr file if it exists.
  2. Merge the existing tags with the given tags.
  3. Write a new .[...]mr file with the new tags.
  1. 如果用户要求--append读取.[py|ru|go]mr文件(如果存在)。
  2. 将现有标签与给定标签合并。
  3. 用新标签编写一个新的.[...]mr文件。

This breaks down into a few small tasks we can compare in each language:

这分为几个小任务,我们可以用每种语言进行比较:

  • Searching for and reading a file.
  • Merging two items (only keeping the unique set)
  • Writing a file
  • 搜索和读取文件。
  • 合并两个项目(仅保留唯一集合)
  • 写文件

档案搜寻 (File Search)

Python (pymr)

Python(pymr)

For python this involves the os module.

对于python,这涉及os模块。

1
1
2
2
3
3

Ruby (rumr)

Ruby(谣言)

For ruby this involves the File class.

对于Ruby,这涉及File类。

1
1
2
2
3
3

Golang (gomr)

高朗(GOMR)

For golang this involves the path package.

对于golang,这涉及path软件包。

1
1
2
2
3
3

独特合并 (Unique Merge)

Python (pymr)

Python(pymr)

For python this involves the use of a set.

对于python,这涉及使用set

1
1
2
2

Ruby (rumr)

Ruby(谣言)

For ruby this involves the use of the .uniq array method.

对于Ruby,这涉及使用.uniq数组方法。

1
1
2
2
3
3
4
4
5
5
6
6

Golang (gomr)

高朗(GOMR)

For golang this involves the use of custom function.

对于golang,这涉及使用自定义函数。

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12

文件读/写 (File Read/Write)

I tried to choose the simplest possible file format to use in each language.

我试图选择每种语言使用的最简单的文件格式。

Python (pymr)

Python(pymr)

For python this involves the use of the pickle module.

对于python,这涉及使用pickle模块。

1
1
2
2
3
3
4
4
5
5

Ruby (rumr)

Ruby(谣言)

For ruby this involves the use of the YAML module.

对于Ruby,这涉及使用YAML模块。

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8

Golang (gomr)

高朗(GOMR)

For golang this involves the use of the config package.

对于golang,这涉及使用config软件包。

1
1
2
2
3
3
4
4
5
5

运行(命令执行) (Run (Command Execution))

The run logic is as follows:

运行逻辑如下:

  1. Recursively walk from the given basepath searching for .[...]mr files
  2. Load a found file, and see if the given tag is in it
  3. Call the given command in the directory of a matching file.
  1. 从给定的基本路径递归地搜索.[...]mr文件
  2. 加载找到的文件,然后查看给定标签是否在其中
  3. 在匹配文件的目录中调用给定命令。

This breaks down into a few small tasks we can compare in each language:

这分为几个小任务,我们可以用每种语言进行比较:

  • Recursive Directory Search
  • String Comparison
  • Calling a Shell Command
  • 递归目录搜索
  • 字符串比较
  • 调用Shell命令

递归目录搜索 (Recursive Directory Search)

Python (pymr)

Python(pymr)

For python this involves the os module and fnmatch module.

对于python,这涉及os模块和fnmatch模块。

1
1
2
2
3
3

Ruby (rumr)

Ruby(谣言)

For ruby this involves the Find and File classes.

对于ruby,这涉及FindFile类。

1
1
2
2
3
3
4
4
5
5
6
6

Golang (gomr)

高朗(GOMR)

For golang this requires the filepath package and a custom callback function.

对于golang,这需要filepath包和自定义回调函数。

1
1
2
2
3
3
4
4
5
5
6
6
7
7

字符串比较 (String Comparison)

Python (pymr)

Python(pymr)

Nothing additional is needed in python for this task.

python中不需要任何其他操作。

1
1
2
2

Ruby (rumr)

Ruby(谣言)

Nothing additional is needed in ruby for this task.

Ruby不需要额外的费用来完成这项任务。

1
1
2
2

Golang (gomr)

高朗(GOMR)

For golang this requires the strings package.

对于golang,这需要字符串包。

1
1
2
2

调用Shell命令 (Calling a Shell Command)

Python (pymr)

Python(pymr)

For python this requires the os module and the subprocess module.

对于python,这需要os模块和subprocess模块。

1
1
2
2

Ruby (rumr)

Ruby(谣言)

For ruby this involves the Kernel module and the Backticks syntax.

对于Ruby,这涉及内核模块和Backticks语法。

1
1
2
2
3
3
4
4

Golang (gomr)

高朗(GOMR)

For golang this involves the os package and the os/exec package.

对于golang,这涉及os软件包和os / exec软件包。

1
1
2
2
3
3

打包 (Packaging)

The ideal mode of distribution for this tool is via a package. A user could then install it tool install [pymr,rumr,gomr] and have a new command on there systems path to execute. I don’t want to go into packaging systems here, rather I will just show the basic configuration file needed in each language.

该工具的理想分发方式是通过包装。 然后,用户可以使用tool install [pymr,rumr,gomr]对其进行tool install [pymr,rumr,gomr]并在该系统路径上执行新命令。 我不想在这里进入打包系统,而只是显示每种语言所需的基本配置文件。

Python (pymr)

Python(pymr)

For python a setup.py is required. Once the package is created and uploaded it can be installed with pip install pymr.

对于python,需要setup.py 。 创建并上传软件包后,可以使用pip install pymr进行安装。

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20
21
21
22
22
23
23
24
24
25
25
26
26
27
27
28
28
29
29
30
30
31
31
32
32
33
33
34
34
35
35
36
36

Ruby (rumr)

Ruby(谣言)

For ruby a rumr.gemspec is required. Once the gem is created and uploaded is can be installed with gem install rumr.

对于ruby,需要rumr.gemspec 。 一旦创建并上传了gem,就可以使用gem install rumr进行安装。

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14

Golang (gomr)

高朗(GOMR)

For golang the source is simply compiled into a binary that can be redistributed. There is no additional file needed and currently no package repository to push to.

对于golang,源代码可以简单地编译为可以重新分发的二进制文件。 不需要其他文件,当前也没有要推送的软件包存储库。

结论 (Conclusion)

For this tool Golang feels like the wrong choice. I don’t need it to be very performant and I’m not utilizing the native concurrency Golang has to offer. This leaves me with Ruby and Python. For about 80% of the logic my personal preference is a toss-up between the two. Here are the pieces I find better in one language:

对于此工具,Golang感觉是错误的选择。 我不需要它具有很高的性能,也没有利用Golang提供的本机并发。 这让我有了Ruby和Python。 对于大约80%的逻辑,我个人的偏好是两者之间的折衷。 以下是我用一种语言找到的更好的作品:

命令行界面声明 (Command-Line Interface Declaration)

Python is the winner here. The click libraries decorator style declaration is clean and simple. Keep in mind I have only tried the Ruby thor gem so there may be better solutions in Ruby. This is also not a commentary on either language, rather that the CLI library I used in python is my preference.

Python是这里的赢家。 Click库装饰器样式声明简洁明了。 请记住,我只尝试了Ruby thor gem,所以Ruby中可能会有更好的解决方案。 这也不是对这两种语言的注释,而是我偏爱在python中使用的CLI库。

递归目录搜索 (Recursive Directory Search)

Ruby is the winner here. I found that this entire section of code was much cleaner and more readable using ruby’s Find.find() and especially the next unless syntax.

Ruby是这里的赢家。 我发现使用ruby的Find.find()尤其是next unless语法,整段代码都更加清晰易读。

打包 (Packaging)

Ruby is the winner here. The rumr.gemspec file is much simpler and the process of building and pushing a gem was much simpler as well. The bundler tool also makes installing in semi-isolated environments a snap.

Ruby是这里的赢家。 rumr.gemspec文件要简单得多,而构建和推送gem的过程rumr.gemspec简单得多。 捆绑器工具还使在半隔离环境中的安装变得轻而易举

最终决定 (Final Determination)

翻译自: https://www.pybloggers.com/2015/06/python-ruby-and-golang-a-command-line-application-comparison/

python和golang

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值