你经常可以看到这个(变异a):
def main():
do_something()
do_sth_else()
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
我现在想知道为什么不这样(变异b):
if __name__ == '__main__':
do_something()
do_sth_else()
Run Code Online (Sandbox Code Playgroud)
或者至少这个(变化c):
if __name__ == '__main__':
def main():
do_something()
do_sth_else()
main()
Run Code Online (Sandbox Code Playgroud)
当然,内部函数调用main()
可能不是函数调用,它们只代表您可能想要在main()
函数中执行的任何操作.
那么为什么人们更喜欢变异而不是其他?它只是风格/感觉还是有一些真正的原因?如果可能,请链接来源.
我已经静态编译了Python2.7而没有任何错误.要测试我的构建,我使用以下代码段:
#include "Python.h"
int main()
{
Py_Initialize();
}
Run Code Online (Sandbox Code Playgroud)
我正在编译它:
$ gcc -static -I/path/to/python/header -L/path/to/my/staticpythonlib \
-lpython2.7 -ldl -l_all_other_needed_lib /tmp/my_previous_snippet.c -o myouput
Run Code Online (Sandbox Code Playgroud)
但是,发生了错误.gcc声称着名undefined reference
.
test.c :(.text + 0x1):对'Py_Initialize'的未定义引用
奇怪的是我使用带有详细标记的gcc(我不会在这里粘贴结果)并且编译器说,它使用的是我的libpython,但找不到引用.所以我列出了我的静态python2.7库的符号:
$ nm /path/to/pythonlib |grep Py_Initialize
frozenmain.o U Py_Initialize
pythonrun.o 0000009e9 T Py_Initialize
pythonrun.o 000000052 T Py_Initialize_Ex
main.o U Py_Initialize
Run Code Online (Sandbox Code Playgroud)
我们可以看到,Py_Initialize
pythonrun.o中正确引用了它.但是我不知道编译器如何选择正确的目标文件.
我的问题是:
谢谢你的帮助.
点是什么意思?
大多数时候我只写那个:
<activity android:name="OneActivity" ...>...</activity>
Run Code Online (Sandbox Code Playgroud)
但有时我会在自动生成的文件中看到:
<activity android:name=".OtherActivity" ...>...</activity>
Run Code Online (Sandbox Code Playgroud)
而且在Docs for Service中我看到他们写的
<manifest ... >
...
<application ... >
<service android:name=".ExampleService" />
...
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
但我从来没有看到尝试这一个或另一个的任何差异.
在阅读教程和自述文件时,我经常看到人们在广告中安装python包pip
,即使他们使用的操作系统也是如此,它有一个很好的包管理器apt
.然而在现实生活中,我只遇到了那些只能用他们的操作系统包管理器安装东西的人,推断这个包管理器会对所有包都一致,无论是否python.
通常,只要您不自己赚钱,您就可以免费使用开源项目.在那一刻,你经常需要购买另一个许可证以换取现金,以mySQL命名为例.但我找不到任何关于couchDB的信息.因此,我想知道它是否完全免费,即使您在其上建立业务.你能证实吗?
如何在Oracle SQL WHERE中表达"现在减去30分钟",以便我可以将其与存储的时间戳进行比较?
我喜欢Python的表现力.但是,我不能像我想的那样表达一切紧凑.例如,我经常写这个:
def is_everything_okay(some_array):
for s in some_array:
if not is_okay(s):
return False
return True
Run Code Online (Sandbox Code Playgroud)
但那就是Java,而不是Python.如何提高该代码片段的表现力(以及可能的执行速度)?
我发现只使用matplotlib
将图表绘制到文件中并不像阅读教程那样容易.在教程中解释说,您只是累积数据,然后:
import matplotlib.pyplot as plt
# ... fill variables with meaningful stuff
plt.plot(data.x,data.y,format_string)
plt.savefig(filename)
Run Code Online (Sandbox Code Playgroud)
并做了.如果你只是将它作为shell执行,那也可以正常工作.但是,如果您将此代码提供给没有任何窗口的进程(如jenkins),那么您只会收到以下错误:
Traceback (most recent call last):
File "./to_graph.py", line 89, in <module>
main()
File "./to_graph.py", line 78, in main
plt.plot(warnings[0],warnings[1],args.format_warnings,label="Warnings")
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2460, in plot
ax = gca()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 701, in gca
ax = gcf().gca(**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 369, in gcf
return figure()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure
**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
window = Tk.Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", …
Run Code Online (Sandbox Code Playgroud) 这是我刚刚在我的机器上播放的一个例子:
$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
# just a test class
>>> class A(object):
... def hi(self):
... print("hi")
...
>>> a = A()
>>> a.hi()
hi
>>> def hello(self):
... print("hello")
...
>>>
>>> hello(None)
hello
>>>
>>>
>>>
>>> a.hi = hello
# now I would expect for hi to work the same way as before
# and it just prints …
Run Code Online (Sandbox Code Playgroud) 通常当你调用sed时,你可以调用\1
引用第一个子匹配,即:
<something> | sed "s/<regex>\(<regex>\)<regex>/\1/"
Run Code Online (Sandbox Code Playgroud)
这将只取中间正则表达式并删除外部正则表达式.
但是,如果你有超过9场比赛,你会怎么做?简单写作\10
不起作用,因为它将被解释为take submatch number one and add a zero behind it
.
python ×6
android ×1
couchdb ×1
gcc ×1
linux ×1
matplotlib ×1
oracle ×1
oracle11g ×1
performance ×1
pip ×1
python-2.7 ×1
readability ×1
regex ×1
sed ×1
sql ×1
timestamp ×1