小编sup*_*gra的帖子

为什么bool(xml.etree.ElementTree.Element)的计算结果为False?

import xml.etree.ElementTree as ET
e = ET.Element('Brock',Role="Bodyguard")
print bool(e)
Run Code Online (Sandbox Code Playgroud)

为什么要xml.etree.ElementTree.Element考虑False

我知道我可以做if e is not None以检查是否存在.但我强烈期望bool(e)回归True.

python boolean

27
推荐指数
2
解决办法
3248
查看次数

更改运行进程的输出重定向

我有一个Python脚本会启动(其发射的孙子),并经过一段时间后,我终止子,但孙子继续泵到标准输出。在我杀死孩子之后,我想抑制/重定向孙子(及其所有后代)的 stdout 和 stderr。

这是家长:

import time
import subprocess
proc = subprocess.Popen('./child.sh')
print("Dad: I have begotten a son!")
time.sleep(1)
proc.kill()
proc.wait()
print("Dad: My son hath died!")
time.sleep(2)
print("Dad: Why does my grandson still speak?")
Run Code Online (Sandbox Code Playgroud)

这是我无法修改的子脚本。

#!/bin/bash
./grandchild.sh &
echo "Child: I had a son!"
for (( i = 0; i < 10; i++ )); do
    echo "Child: Hi Dad, meet your grandson!"
    sleep 1
done
exit 0
Run Code Online (Sandbox Code Playgroud)

这是一个吵闹的孙子,我无法修改

#!/bin/bash
for …
Run Code Online (Sandbox Code Playgroud)

python subprocess io-redirection

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

箭头方向由数据设置,但长度由图形大小设置

我想通过指向与函数相切的方向来绘制一个箭头,指示某个点上函数的渐变.我希望此箭头的长度与轴大小成比例,以便在任何缩放级别都可见.

假设我们想绘制x^2at x=1的衍生物(衍生物是2).这是我尝试过的两件事:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)

x = np.linspace(0, 2, 1000)
y = x**2
ax.plot(x, y)

x, y = (1.0, 1.0)
grad = 2.0

# Fixed size, wrong direction
len_pts = 40
end_xy = (len_pts, len_pts*grad)

ax.annotate("", xy=(x, y), xycoords='data',
        xytext=end_xy, textcoords='offset points',
        arrowprops=dict(arrowstyle='<-', connectionstyle="arc3"))

# Fixed direction, wrong size
len_units = 0.2
end_xy = (x+len_units, y+len_units*grad)

ax.annotate("", xy=(x, y), …
Run Code Online (Sandbox Code Playgroud)

python matplotlib

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

从 fill_ Between 的 PolyCollection 中提取 x/y 数据

如何从绘图中生成的 PolyCollection 中提取 x/y 数据fill_between

polyCollection = ax.fill_between(x,ylo,yhi)
Run Code Online (Sandbox Code Playgroud)

现在我如何从 获取数据polyCollection

对于其他Collection对象,我使用x, y = artist.get_offsets().T,但这里由于某种原因仅返回零。

对于“Line”类型的对象,我使用x, y = artist.get_xdata(), artist.get_ydata().

(我在回调中使用此信息来本地自动缩放 y 轴,以适应特定 x 范围内的数据。)

matplotlib

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

标签 统计

python ×3

matplotlib ×2

boolean ×1

io-redirection ×1

subprocess ×1