博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python和golang_Python,Ruby和Golang:命令行应用程序比较
阅读量:2519 次
发布时间:2019-05-11

本文共 8302 字,大约阅读时间需要 27 分钟。

python和golang

This is a guest blog post by , 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 .

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



python, ruby, and golang images

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

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

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

应用结构 (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”工具的命令行界面为:

112233445566778899

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 package.

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

112233445566

Ruby (rumr)

Ruby(谣言)

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

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

1122334455667788991010111112121313141415151616

Golang (gomr)

高朗(GOMR)

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

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

1122334455667788991010111112121313141415151616171718181919202021212222

注册 (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 module.

对于python,这涉及模块。

112233

Ruby (rumr)

Ruby(谣言)

For ruby this involves the class.

对于Ruby,这涉及类。

112233

Golang (gomr)

高朗(GOMR)

For golang this involves the package.

对于golang,这涉及软件包。

112233

独特合并 (Unique Merge)

Python (pymr)

Python(pymr)

For python this involves the use of a .

对于python,这涉及使用 。

1122

Ruby (rumr)

Ruby(谣言)

For ruby this involves the use of the array method.

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

112233445566

Golang (gomr)

高朗(GOMR)

For golang this involves the use of custom function.

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

112233445566778899101011111212

文件读/写 (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 module.

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

1122334455

Ruby (rumr)

Ruby(谣言)

For ruby this involves the use of the module.

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

1122334455667788

Golang (gomr)

高朗(GOMR)

For golang this involves the use of the package.

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

1122334455

运行(命令执行) (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 module and module.

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

112233

Ruby (rumr)

Ruby(谣言)

For ruby this involves the and classes.

对于ruby,这涉及和类。

112233445566

Golang (gomr)

高朗(GOMR)

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

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

11223344556677

字符串比较 (String Comparison)

Python (pymr)

Python(pymr)

Nothing additional is needed in python for this task.

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

1122

Ruby (rumr)

Ruby(谣言)

Nothing additional is needed in ruby for this task.

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

1122

Golang (gomr)

高朗(GOMR)

For golang this requires the package.

对于golang,这需要包。

1122

调用Shell命令 (Calling a Shell Command)

Python (pymr)

Python(pymr)

For python this requires the module and the module.

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

1122

Ruby (rumr)

Ruby(谣言)

For ruby this involves the module and the Backticks syntax.

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

11223344

Golang (gomr)

高朗(GOMR)

For golang this involves the package and the package.

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

112233

打包 (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进行安装。

112233445566778899101011111212131314141515161617171818191920202121222223232424252526262727282829293030313132323333343435353636

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进行安装。

11223344556677889910101111121213131414

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 libraries decorator style declaration is clean and simple. Keep in mind I have only tried the Ruby 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是这里的赢家。 库装饰器样式声明简洁明了。 请记住,我只尝试了Ruby 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 tool also makes installing in semi-isolated environments a snap.

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

最终决定 (Final Determination)

翻译自:

python和golang

转载地址:http://vwhwd.baihongyu.com/

你可能感兴趣的文章
NIO学习之Channel
查看>>
两分布间距离的度量:MMD、KL散度、Wasserstein 对比
查看>>
HDU 1300 Pearls (DP)
查看>>
2014年军训总结
查看>>
扩展 -------jQuery
查看>>
Winform跨线程操作最简单的办法
查看>>
[51nod1532]带可选字符的多字符串匹配
查看>>
socket 基于udp实现远程执行命令
查看>>
读取本地json文件,解析json
查看>>
【学习】循环语句1027
查看>>
Git提交代码报错Git push error:src refspec XXX matches more than one解决方案
查看>>
软件设计规格说明书
查看>>
bzoj 1500: [NOI2005]维修数列 -- splay
查看>>
设计模式 - 简单工厂
查看>>
数组与指针杂记
查看>>
四色原理
查看>>
Codeforces Round#500 Div.2 翻车记
查看>>
再更新ww的mingw MinGW-full-20101119
查看>>
Benefit UVA - 11889
查看>>
全排列 最详细的解题报告
查看>>