我最近介绍了库configparser.我希望能够检查每个部分是否至少有一个布尔值设置为1.例如:
[Horizontal_Random_Readout_Size]
Small_Readout = 0
Medium_Readout = 0
Large_Readout = 0
Run Code Online (Sandbox Code Playgroud)
以上会导致错误.
[Vertical_Random_Readout_Size]
Small_Readout = 0
Medium_Readout = 0
Large_Readout = 1
Run Code Online (Sandbox Code Playgroud)
以上将通过.下面是我想到的一些伪代码:
exit_test = False
for sections in config_file:
section_check = False
for name in parser.options(section):
if parser.getboolean(section, name):
section_check = True
if not section_check:
print "ERROR:Please specify a setting in {} section of the config file".format(section)
exit_test = True
if exit_test:
exit(1)
Run Code Online (Sandbox Code Playgroud)
问题:
1)如何执行第一个for循环并迭代配置文件的各个部分?
2)这是一个很好的方法吗?还是有更好的方法?(如果没有请回答问题一.)
我想在我的主域创建一个子域,以便在家中访问我的覆盆子pi.我的域名注册公司每个子域收费10英镑,所以我认为可能有一种更简单(也更便宜)的创建方式.如果我对它们有更多了解以及如何创建它们,它也会有所帮助.
我有像素数据,我想用它来创建一个.tif有多个帧的新图像.我该怎么做呢?我试过python PIL但是我发现它只支持多帧读取而不是写入.请参阅下面的我没有尝试的尝试.
new_Image = Image.new("I;16", (num_pixels,num_rows))
for frame in range((len(final_rows)/num_rows)):
pixels = new_Image.load()
for row in range(num_rows):
row_pixel = final_rows[row].getPixels()
for pixel in range(num_pixels):
pixels[pixel,row] = row_pixel[pixel]
print frame
new_Image.seek(frame)
Run Code Online (Sandbox Code Playgroud) 我目前使用 python PIL 从图像中读取像素。这些像素为 16 位灰度且无符号。但是,每当 PIL 读取它们时,它都会认为它们已签名并将应该类似于45179 的值转换为-20357。
org_Image = Image.open(image)
org_Data = org_Image.load()
width, height = org_Image.size
for y in range(0, height):
temprow_data = []
for x in range(0, width):
temprow_data.append(org_Data[x, y])
Run Code Online (Sandbox Code Playgroud)
我如何让 PIL 输出无符号而不是有符号整数?或者是否有一种非常简单的方法来获取 PIL 输入并在之后进行转换?
我对c和Raspberry Pi相对较新,我正在尝试简单的程序.我想要的是当按下按钮时printfs一次并且不再打印,直到再次按下按钮,即使按下按钮(类似闩锁).我想可能会添加第二个while循环来解决这个问题,但有时它仍然没有检测到按下按钮.
#include <bcm2835.h>
#include <stdio.h>
#define PIN RPI_GPIO_P1_11
int main()
{
if(!bcm2835_init())
return 1;
bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT);
while(1)
{
if(bcm2835_gpio_lev(PIN))
{
printf("The button has been pressed\n");
}
while(bcm2835_gpio_lev(PIN)){}
}
bcm2835_close();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个变量,它是函数的确切名称,但是以字符串格式.例如...
ran_test_opt = "random_aoi"
Run Code Online (Sandbox Code Playgroud)
而且功能是
def random_aoi():
logging.info("Random AOI Test").
Run Code Online (Sandbox Code Playgroud)
从配置文件接收,因此无法更改.有没有办法可以将字符串转换为我可以调用函数的格式?即
ran_test_opt()
Run Code Online (Sandbox Code Playgroud)
这将运行random_aoi函数.
我创建了一个阶跃函数的 Matplotlib 动画。我正在使用以下代码...
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.step([], [])
def init():
line.set_data([], [])
return line,
def animate(i):
x = np.linspace(0, 2, 10)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)
它模糊地类似于我想要的(类似于下面的 gif),但不是恒定的值和随时间滚动的值,每个步骤都是动态的并且上下移动。如何更改我的代码以实现这种转变?

我有一个表示代码字节的整数列表.如何快速,高效地将它们写入二进制文件.
我试过了:
with open (output1, "wb") as compdata:
for row in range(height):
for data in cobs(delta_rows[row].getByte_List()):
output_stream.append(Bits(uint=data, length=8))
compdata.write(output_stream.tobytes())
Run Code Online (Sandbox Code Playgroud)
和
with open (output1, "wb") as compdata:
for row in range(height):
bytelist = cobs(delta_rows[row].getByte_List())
for byte in bytelist:
compdata.write(chr(byte))
Run Code Online (Sandbox Code Playgroud)
两个都给我一个我认为是正确的结果(我还没有扭转过程),但都需要很长时间(6分钟和4分钟).
我正在使用的python模块包含错误代码列表,如下所示: -
DRV_ERROR_CODES = 20001
DRV_SUCCESS = 20002
DRV_VXDNOTINSTALLED = 20003
DRV_ERROR_SCAN = 20004
DRV_ERROR_CHECK_SUM = 20005
DRV_ERROR_FILELOAD = 20006
DRV_UNKNOWN_FUNCTION = 20007
...
Run Code Online (Sandbox Code Playgroud)
我目前只是将返回值与成功错误代码进行比较,以检查进程是否成功.
if atmcd.DRV_SUCCESS==ret:
Run Code Online (Sandbox Code Playgroud)
我想使用返回值并输出究竟导致错误的内容(例如返回值为2004,因此脚本将打印出有扫描错误).有没有办法比较所有这些变量?有没有更好的方法来查找错误?
import struct
f = open('file.bin', 'wb')
value = 1.23456
data = struct.pack("f", value)
f.write(data)
f.close()
f = open('file.bin', 'rb')
print struct.unpack('f',f.read(4))
f.close()
Run Code Online (Sandbox Code Playgroud)
我尝试执行上面的代码,它给了我错误:
AttributeError:'module'对象没有属性'pack'
我使用的是python 2.7.5,我检查了模块列表,并且"struct"就在那里.
我想执行填充并限制将使用.format输入到字符串的变量的小数点数.我设法限制小数位,但我似乎也无法添加填充.该文件是没有太大的帮助.下面是我的代码的副本.
print "Frame No. {:6d} Expected: {:7.3f}ms Actual: {:7.3f}ms Difference: {:7.3f}ms - ERROR".format(currentImage, expected_time, regression_obj.timeFromStart, (regression_obj.timeFromStart - expected_time))
Run Code Online (Sandbox Code Playgroud)
此处的输出显示值之间没有足够的空格.我希望所有的值都对齐.
Frame No. 17 Expected: 582.080ms Actual: 484.971ms Difference: -97.109ms - ERROR
Frame No. 18 Expected: 616.320ms Actual: 513.498ms Difference: -102.822ms - ERROR
Run Code Online (Sandbox Code Playgroud)
预期产出;
Frame No. 17 Expected: 582.080ms Actual: 484.971ms Difference: -97.109ms - ERROR
Frame No. 18 Expected: 616.320ms Actual: 513.498ms Difference: -102.822ms - ERROR
Run Code Online (Sandbox Code Playgroud) python ×9
file ×2
module ×2
animation ×1
bit ×1
button ×1
c ×1
configparser ×1
debouncing ×1
dns ×1
format ×1
integer ×1
list ×1
matplotlib ×1
performance ×1
raspberry-pi ×1
sections ×1
string ×1
struct ×1
subdomain ×1
tiff ×1
trigonometry ×1
unsigned ×1
variables ×1