如何根据未设置/值的makefile变量中止make/makefile执行?
我想出了这个,但只有在调用者没有显式运行目标(即make
仅运行)时才有效.
ifeq ($(MY_FLAG),)
abort: ## This MUST be the first target :( ugly
@echo Variable MY_FLAG not set && false
endif
all:
@echo MY_FLAG=$(MY_FLAG)
Run Code Online (Sandbox Code Playgroud)
我认为这样的事情是个好主意,但在make的手册中没有找到任何内容:
ifndef MY_FLAG
.ABORT
endif
Run Code Online (Sandbox Code Playgroud) 有没有一个可以重载赋值运算符的魔术方法,比如__assign__(self, new_value)
?
我想禁止重新绑定一个实例:
class Protect():
def __assign__(self, value):
raise Exception("This is an ex-parrot")
var = Protect() # once assigned...
var = 1 # this should raise Exception()
Run Code Online (Sandbox Code Playgroud)
可能吗?这是疯了吗?我应该上药吗?
我在运行嵌入式python时遇到了麻烦.事实证明,我无法捕获sys.exit()引发的SystemExit异常;
这是我到目前为止:
$ cat call.c
#include <Python.h>
int main(int argc, char *argv[])
{
Py_InitializeEx(0);
PySys_SetArgv(argc-1, argv+1);
if (PyRun_AnyFileEx(fopen(argv[1], "r"), argv[1], 1) != 0) {
PyObject *exc = PyErr_Occurred();
printf("terminated by %s\n",
PyErr_GivenExceptionMatches(exc, PyExc_SystemExit) ?
"exit()" : "exception");
}
Py_Finalize();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
另外,我的脚本是:
$ cat unittest-files/python-return-code.py
from sys import exit
exit(99)
Run Code Online (Sandbox Code Playgroud)
运行它:
$ ./call unittest-files/python-return-code.py
$ echo $?
99
Run Code Online (Sandbox Code Playgroud)
我必须执行一个文件,而不是一个命令.
我想以优雅的方式做到这一点:
>>> ''.zfill(5, '-')
'-----'
Run Code Online (Sandbox Code Playgroud)
有没有办法用fill char和counter初始化一个字符串?当然,数量可能会有所不同.
什么是Pythonic的方法呢?
value = (mydict.has_key('index') and mydict['index']) or 1024
Run Code Online (Sandbox Code Playgroud)
从dict的值初始化(如果存在),否则使用默认值.
在LBYL和EAFP上得到了一些答案,但是过于冗长的代码:p
我想要一些单线初始化.
PS:仅限python-2.4(运行CentOS5)