什么是clojure等价的Python成语“if __name__ =='__main __'"? [英] What is the clojure equivalent of the Python idiom "if __name__ == '__main__'"?

查看:164
本文介绍了什么是clojure等价的Python成语“if __name__ =='__main __'"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我习惯用clojure(和/或Lisp)等同于这个常见的python成语。是在python模块的底部有一个测试代码,然后是一个运行代码的语句,例如:

 #mymodule.py 
class MyClass(object):
图书馆的主逻辑/代码在这里
pass

def _runTests():
#测试MyClass的各个方面的代码...
mc = MyClass()#etc ...
assert 2 + 2 == 4

if __name__ =='__main__':_runTests()

-hoc测试。通常使用这个模块,从mymodule import MyClass 写,在这种情况下 _runTests()在最后的代码段中,也可以直接从命令行输入 python mymodule.py 来运行它。



在Clojure中是否有一个等价的成语(和/或common lisp)?我不是一个成熟的单元测试库(好吧,我是,但不是在这个问题),我只是想在一个模块中包含一些代码,只有在某些情况下运行,所以我可以一个快速的方式来运行我一直在努力的代码,但仍然允许我的文件导入像一个正常的模块/命名空间。

解决方案

从命令行反复运行Clojure脚本并不习惯。 REPL是一个更好的命令行。 Clojure是一个Lisp,它是常见的启动Clojure和离开同一个实例永远运行,并与它进行交互,而不是重新启动它。您可以一次一个地更改运行实例中的函数,运行它们,并根据需要戳它们。逃避乏味和缓慢的传统编辑/编译/调试周期是Lisps的一个很大的功能。



您可以轻松地编写函数来执行运行单元测试,只要在运行它们时从REPL调用这些函数,否则忽略它们。在Clojure中常见的是使用 clojure.contrib.test-is ,将你的测试函数添加到你的命名空间,然后使用 clojure.contrib.test- / run-tests 以运行它们。



另一个不从命令行运行Clojure的好理由是JVM的启动时间



如果你真的想从命令行运行一个Clojure脚本,有很多方法可以做到。有关讨论,请参见 Clojure邮寄名单



一种方法是测试命令行参数的存在。给定当前目录中的 foo.clj

 

(defn hello [x](printlnHello,x))

(如果*命令行参数*

(helloREPL))

你开始Clojure。

  $ java -cp〜/ path / to / clojure.jar:。 clojure.main foo.clj  -  
你好,命令行
$ java -cp〜/ path / to / clojure.jar:。 clojure.main
Clojure 1.1.0-alpha-SNAPSHOT
user => (use'foo)
Hello,REPL
nil
user =>

请参阅 src / clj / clojure / main.clj

另一种方法是将你的代码编译成 .class 文件,并从Java命令行调用它们。给定一个源文件 foo.clj

  $ b(:gen-class))

(defn hello [x](printlnHello,x))

(defn -main [ line))

创建一个目录来存储编译的 .class files;这默认为 ./ classes 。你必须自己创建这个文件夹,Clojure不会创建它。还要确保您设置 $ CLASSPATH 包括 ./ classes 和目录与您的源代码;我假设 foo.clj 在当前目录中。所以从命令行:

  $ mkdir classes 
$ java -cp〜/ path / to / clojure.jar :./ classes:。 clojure.main
Clojure 1.1.0-alpha-SNAPSHOT
user => (compile'foo)
foo

code>目录,你现在将有一堆 .class 文件。从命令行调用代码(默认运行 -main 函数):

  $ java -cp〜/ path / to / clojure.jar:./ classes foo 
你好,命令行。

有关在 clojure.org


I'm dabbling in clojure and am having a little trouble trying to determine the clojure (and / or Lisp) equivalent of this common python idiom.

The idiom is that at the bottom of a python module there is often a bit of test code, and then a statement which runs the code, for example:

# mymodule.py
class MyClass(object):
    """Main logic / code for the library lives here"""
    pass

def _runTests():
    # Code which tests various aspects of MyClass...
    mc = MyClass() # etc...
    assert 2 + 2 == 4

if __name__ == '__main__': _runTests()

This is useful for simple, ad-hoc testing. One would normally use this module by writing from mymodule import MyClass, in which case _runTests() is never called, but with the snippet at the end, one can also run it by typing python mymodule.py directly from the command line.

Is there an equivalent idiom in Clojure (and/or common lisp)? I'm not after a full-blown unit testing library (well, I am, but not in this question), I'd just like to include some code in a module which will only be run under some circumstances, so I can have a quick way to run code I've been working on but still allow my file to be imported like a normal module / namespace.

解决方案

It's not idiomatic to run Clojure scripts over and over from the command line. The REPL is a better command line. Clojure being a Lisp, it's common to fire up Clojure and leave the same instance running forever, and interact with it rather than restart it. You can change functions in the running instance one at a time, run them and poke them as needed. Escaping the tedious and slow traditional edit/compile/debug cycle is a great feature of Lisps.

You can easily write functions to do things like run unit tests, and just call those functions from the REPL whenever you want to run them and ignore them otherwise. It's common in Clojure to use clojure.contrib.test-is, add your test functions to your namespace, then use clojure.contrib.test-is/run-tests to run them all.

Another good reason not to run Clojure from the commandline is that the startup time of the JVM can be prohibitive.

If you really want to run a Clojure script from the command line, there are a bunch of ways you can do it. See the Clojure mailing list for some discussion.

One way is to test for the presence of command line arguments. Given this foo.clj in the current directory:

(ns foo)

(defn hello [x] (println "Hello," x))

(if *command-line-args*
  (hello "command line")
  (hello "REPL"))

You'll get different behavior depending how you start Clojure.

$ java -cp ~/path/to/clojure.jar:. clojure.main foo.clj --
Hello, command line
$ java -cp ~/path/to/clojure.jar:. clojure.main
Clojure 1.1.0-alpha-SNAPSHOT
user=> (use 'foo)
Hello, REPL
nil
user=>

See src/clj/clojure/main.clj in the Clojure source if you want to see how this is working.

Another way is to compile your code into .class files and invoke them from the Java command line. Given a source file foo.clj:

(ns foo
  (:gen-class))

(defn hello [x] (println "Hello," x))

(defn -main [] (hello "command line"))

Make a directory to store the compiled .class files; this defaults to ./classes. You must make this folder yourself, Clojure won't create it. Also make sure you set $CLASSPATH to include ./classes and the directory with your source code; I'll assume foo.clj is in the current directory. So from the command line:

$ mkdir classes
$ java -cp ~/path/to/clojure.jar:./classes:. clojure.main
Clojure 1.1.0-alpha-SNAPSHOT
user=> (compile 'foo)
foo

In the classes directory you will now have a bunch of .class files. To invoke your code from the command line (running the -main function by default):

$ java -cp ~/path/to/clojure.jar:./classes foo
Hello, command line.

There's a lot of information about compiling Clojure code on clojure.org.

这篇关于什么是clojure等价的Python成语“if __name__ =='__main __'"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆