python Lua 作为通用脚本语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/250151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:44:43  来源:igfitidea点击:

Lua as a general-purpose scripting language?

pythonscriptinglua

提问by user32756

When I see Lua, the only thing I ever read is "great for embedding", "fast", "lightweight" and more often than anything else: "World of Warcraft" or in short "WoW".

当我看到 Lua 时,我读到的唯一内容是“非常适合嵌入”、“快速”、“轻量级”,而且比其他任何东西都更常见:“魔兽世界”或简称“WoW”。

Why is it limited to embedding the whole thing into another application? Why not write general-purpose scripts like you do with Python or Perl?

为什么它仅限于将整个事物嵌入到另一个应用程序中?为什么不像使用 Python 或 Perl 那样编写通用脚本?

Lua seems to be doing great in aspects like speed and memory-usage (The fastest scripting language afaik) so why is it that I never see Lua being used as a "Desktop scripting-language" to automate tasks? For example:

Lua 似乎在速度和内存使用(最快的脚本语言 afaik)等方面做得很好,为什么我从来没有看到 Lua 被用作“桌面脚本语言”来自动化任务?例如:

  • Renaming a bunch of files
  • Download some files from the web
  • Webscraping
  • 重命名一堆文件
  • 从网上下载一些文件
  • 网页抓取

Is it the lack of the standard library?

是缺少标准库吗?

采纳答案by JesperE

Just because it is "marketed" (in some general sense) as a special-purpose language for embedded script engines, does not mean that it is limited to that. In fact, WoW could probably just as well have chosen Python as their embedded scripting language.

仅仅因为它作为嵌入式脚本引擎的专用语言被“销售”(在某种一般意义上),并不意味着它仅限于此。事实上,魔兽世界也可能选择 Python 作为他们的嵌入式脚本语言。

回答by Fang-Pen Lin

Lua is a cool language, light-weight and extremely fast!

Lua 是一种很酷的语言,轻量级且速度极快!

But the point is: Is performance so important for those tasks you mentioned?

但关键是:性能对你提到的那些任务有那么重要吗?

  • Renaming a bunch of files
  • Download some files from the web
  • Webscraping
  • 重命名一堆文件
  • 从网上下载一些文件
  • 网页抓取

You write those programs once, and run them once, too maybe. Why do you care about performance so much for a run-once program?

您编写一次这些程序,然后运行它们,也可能如此。为什么您如此关心一次性程序的性能?

For example:

例如:

  1. Cost 3 hours to write a C/C++ program, to handle data once, the program will take 1 hour to run.
  2. Cost 30 Minute to write a Python program to handle data once, the program will take 10 hours to run.
  1. 写一个C/C++程序需要3个小时,处理一次数据,程序运行需要1个小时。
  2. 编写一个 Python 程序来处理一次数据需要 30 分钟,该程序将需要 10 个小时才能运行。

If you choose the first, you save the time to run the program, but you cost your time to develop the program.

如果选择第一种,则可以节省运行程序的时间,但会花费开发程序的时间。

On the other hand, if you choose the second, you waste time to run the program, but you can do other things when the program is running. How about play World of Warcraft, kill monsters with your warlock? Eat my D.O.T! :P

另一方面,如果你选择第二个,你会浪费时间运行程序,但你可以在程序运行时做其他事情。玩魔兽世界,和你的术士一起杀怪物怎么样?吃掉我的 DOT!:P

That's it! Although Lua is not so difficult to write, everything about Lua is designed to be efficient.And what's more, there are little modules for Lua, but there are so many modules for Python. You don't want to port a C library for Lua just for a run-once program, do you? Instead, choose Python and use those module to achieve your task easily might be a better idea.

而已!Lua虽然写起来不是那么难,但Lua的一切都是为了高效而设计的,而且Lua的模块很少,Python的模块却很多。你不想为 Lua 移植一个 C 库只是为了运行一次程序,是吗?相反,选择 Python 并使用这些模块轻松完成您的任务可能是一个更好的主意。

FYI: Actually, I have tried to use Lua to do webscraping, but finally, I realized I do not have to care so much about language performance. The bottleneck of webscraping is not on the performance of the language. The bottleneck is on network I/O, HTML parsing and multitasking. All I have to do is make sure the program works and find the bottleneck. Finally, I chose Python rather than Lua. There is so many excellent Python modules; I have no reason to build my own.

仅供参考:实际上,我曾尝试使用 Lua 进行网页抓取,但最终我意识到我不必太在意语言性能。网页抓取的瓶颈不在于语言的性能。瓶颈在于网络 I/O、HTML 解析和多任务处理。我所要做的就是确保程序正常运行并找到瓶颈。最后,我选择了 Python 而不是 Lua。有这么多优秀的Python模块;我没有理由建立自己的。

According to my experience about webscraping, I chose Twisted for network I/O and lxml for html parsing as the backend of my webscraping program. I have wrote an article for an introduction to this technology.

根据我对网页抓取的经验,我选择 Twisted 作为网络 I/O 和 lxml 用于 html 解析作为我的网页抓取程序的后端。我写了一篇文章来介绍这项技术。

The best choice to grab data from websites: Python + Twisted + lxml

从网站抓取数据的最佳选择:Python + Twisted + lxml

Hope this is helpful.

希望这是有帮助的。

回答by Sébastien RoccaSerra

Lua has fewer libraries than Python. But be sure to have a look at LuaForge. It has a lot of interesting libs, like LuaCURL, wxLuaor getopt.

Lua 的库比 Python 少。但一定要看看LuaForge。它有很多有趣的库,比如LuaCURLwxLuagetopt

Then, visit LuaRocks, the package management system for Lua. With it, you can search and install most mature Lua modules with dependencies. It feels like RubyGemsor aptitude.

然后,访问LuaRocks,Lua 的包管理系统。有了它,您可以搜索和安装最成熟的 Lua 模块和依赖项。感觉就像RubyGemsaptitude

The site lua-users.orghas a lot of interesting resources too, like tutorials or the Lua Wiki.

lua-users.org站点也有很多有趣的资源,比如教程或Lua Wiki

What I like about Lua is not its speed, it's its minimal core language, flexibility and extensibility.

我喜欢 Lua 的不是它的速度,而是它最小的核心语言、灵活性和可扩展性。

That said, I would probably use Python for the tasks you mentionned because of the larger community doing such things in Python.

也就是说,我可能会使用 Python 来完成你提到的任务,因为更大的社区在 Python 中做这样的事情。

回答by Firas Assaad

It's probably because Lua was designed as a scripting and extension language. On the official siteit's described as a powerful, fast, light-weight, embeddable scripting language. There's nothing stopping you from writing general purpose programs for it (if I recall correctly it ships with an interpreter and compiler), but the language designers intended it to be used mainly as an embedded language (hence being light-weight and all)

这可能是因为 Lua 被设计为一种脚本和扩展语言。在官方网站上,它被描述为一种强大、快速、轻量级、可嵌入的脚本语言。没有什么能阻止你为它编写通用程序(如果我没记错的话,它带有解释器和编译器),但语言设计者希望它主要用作嵌入式语言(因此是轻量级的)

回答by Norman Ramsey

This is a sociological question, not a programming question.

这是一个社会学问题,而不是一个编程问题。

I use Lua for general-purpose scripting almost exclusively. But I had to write a few hundred lines of code so that Lua would play better with the shell. This included such tricks as

我几乎只将 Lua 用于通用脚本编写。但是我不得不写几百行代码,这样 Lua 才能更好地使用 shell。这包括这样的技巧

  • Quoting a string so it is seen as one word by the shell
  • Writing a function to capture the output of a command as in shell $(command)
  • Writing a function to crawl the filesystem using the Lua posix library and expand shell globbing patterns
  • 引用一个字符串,以便 shell 将其视为一个单词
  • 编写一个函数来捕获命令的输出,如 shell $(command)
  • 编写一个函数来使用 Lua posix 库抓取文件系统并扩展 shell globbing 模式

(For those who may be interested, I've left the code in my Lua drop box, which also contains some other stuff. The interesting stuff is probably in osutilin os.quote, os.runf, os.capture, and maybe os.execve. The globbing is in posixutil.lua. They both use Luiz Henrique de Figuereido's Lua Posix library.)

(对于那些谁可能有兴趣,我已经离开了代码在我的Lua中投递箱,其中还包含一些其他的东西。有趣的东西可能是osutilos.quoteos.runfos.capture,也许os.execve,文件名匹配是posixutil.lua,它们都使用路易斯恩里克德de Figuereido 的Lua Posix 库。)

To me, the extra effort is worth it because I can deal with simple syntax and great data structures. To others, a more direct connection with the shell might be preferred.

对我来说,额外的努力是值得的,因为我可以处理简单的语法和出色的数据结构。对于其他人来说,可能更喜欢与外壳更直接的连接。

回答by uroc

Definitely a lack of standard libraries. It's also lesser known than Python, Perl or Ruby.

绝对缺乏标准库。它的知名度也不如 Python、Perl 或 Ruby。

回答by RBerteig

There has been a recent push to create a batteries included installation for Lua on Windows. The result can be found at the Lua for Windowsproject at LuaForge. It includes the interpreter and a large collection of extra modules allowing useful scripts and applications to be written and used out of the box.

最近一直在推动为 Windows 上的 Lua 创建一个包含电池的安装。结果可以在 LuaForge的Lua for Windows项目中找到。它包括解释器和大量额外模块,允许编写和使用开箱即用的有用脚本和应用程序。

I know that various Linux distros are including Lua and some modules now, and more to come.

我知道现在各种 Linux 发行版都包括 Lua 和一些模块,而且还会有更多。

There are also a couple of proposed module libraries under discussion in the mailing list, but the community hasn't yet settled on one as the "official" mechanism.

邮件列表中还有几个提议的模块库正在讨论中,但社区尚未确定一个作为“官方”机制。

I use Lua both as a scripting language and as the "main" loop of my typical application, supported by one or more DLLs containing code that was better implemented in C, or wrapping existing libraries or API functions that are needed by a particular project. Used with a GUI toolkit such as IUPor wxLua(a Lua binding for wxWindows), Lua makes writing small to mid-sized GUI applications quite pleasant.

我将 Lua 用作脚本语言和典型应用程序的“主”循环,由一个或多个包含用 C 更好实现的代码的 DLL 支持,或者包装特定项目所需的现有库或 API 函数。与诸如IUPwxLua(wxWindows 的 Lua 绑定)之类的 GUI 工具包一起使用,Lua 使编写中小型 GUI 应用程序变得非常愉快。

回答by Arthur Reutenauer

Lua is used in LuaTeX, a TeX extension, as an embedded language, and has gained popularity rapidly among TeX developers because of that. It is used as a scripting language for some utilities in the TeX Live distribution, be it only because now there is a luatexbinary, available on all platforms, that can also be used as a Lua interpreter (with some vital modules added – slnunicode, luafilesystem, etc.) That's very important for Windows installations, that relied on additional Unix scripting tools earlier (ActivePerl, etc.) The ConTeXt macro language uses Lua scripts extensively nowadays.

Lua 在TeX 扩展LuaTeX 中用作嵌入式语言,因此在 TeX 开发人员中迅速流行起来。它被用作 TeX Live 发行版中某些实用程序的脚本语言,仅仅是因为现在有一个luatex二进制文件,可在所有平台上使用,也可以用作 Lua 解释器(添加了一些重要的模块 - slnunicode、luafilesystem等)这对于 Windows 安装非常重要,它依赖于早期的其他 Unix 脚本工具(ActivePerl 等)。ConTeXt 宏语言现在广泛使用 Lua 脚本。

That's admittedly a very special field :-) But completely unrelated to games!

这无疑是一个非常特殊的领域:-) 但与游戏完全无关!

回答by bortzmeyer

Lack of standard library. Period. Even listing all the files in a directory require a non-standard module.

缺乏标准库。时期。即使列出目录中的所有文件也需要非标准模块

There are good reasons for that (keeping strict ANSI portability, not requiring POSIX) but the result is that, for general programming, I prefer Python.

这样做有充分的理由(保持严格的 ANSI 可移植性,不需要 POSIX),但结果是,对于一般编程,我更喜欢 Python。

回答by MattG

I think the answer about it being a "marketing" thing is probably correct, along with the lack of a large set of libraries to choose from. I would like to point out another case of this: Ruby. Ruby is meant to be a general purpose scripting language. The problem is that since Ruby on Rails has risen to be so popular, it is becoming hard to find something that is unrelated to Rails. I'm afraid Lua will suffer this as well, being popular because of a few major things using it, but never able to break free of that stigma.

我认为关于它是“营销”事物的答案可能是正确的,同时缺乏大量可供选择的库。我想指出另一个案例:Ruby。Ruby 旨在成为一种通用脚本语言。问题是,自从 Ruby on Rails 变得如此流行以来,很难找到与 Rails 无关的东西。恐怕 Lua 也会受到影响,因为使用它的一些主要事情而流行,但永远无法摆脱这种耻辱。