小编Car*_*cio的帖子

如果未设置变量,则中止makefile

如何根据未设置/值的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)

makefile

139
推荐指数
6
解决办法
9万
查看次数

是否有可能重载Python赋值?

有没有一个可以重载赋值运算符的魔术方法,比如__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 methods class magic-methods assignment-operator

65
推荐指数
7
解决办法
3万
查看次数

如何防止嵌入式python退出()我的进程

我在运行嵌入式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)

必须执行一个文件,而不是一个命令.

python embedded exception exit

9
推荐指数
1
解决办法
894
查看次数

使用fill char和counter创建python字符串

我想以优雅的方式做到这一点:

>>> ''.zfill(5, '-')
'-----'
Run Code Online (Sandbox Code Playgroud)

有没有办法用fill char和counter初始化一个字符串?当然,数量可能会有所不同.

python string

4
推荐指数
1
解决办法
4455
查看次数

从可能缺少的dict值初始化Python变量

什么是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)

python indexing dictionary initialization

1
推荐指数
1
解决办法
1742
查看次数