小编Rob*_*sak的帖子

如何在shell函数中获得效果并使用"set -e"?

set -e(或以脚本开头#!/bin/sh -e)对于在出现问题时自动轰炸非常有用.它使我不必错误地检查可能失败的每个命令.

如何在函数内获得相应的内容?

例如,我有以下脚本在出错时立即退出并出现错误退出状态:

#!/bin/sh -e

echo "the following command could fail:"
false
echo "this is after the command that fails"
Run Code Online (Sandbox Code Playgroud)

输出如预期:

the following command could fail:
Run Code Online (Sandbox Code Playgroud)

现在我想把它包装成一个函数:

#!/bin/sh -e

my_function() {
    echo "the following command could fail:"
    false
    echo "this is after the command that fails"
}

if ! my_function; then
    echo "dealing with the problem"
fi

echo "run this all the time regardless of the success of my_function"
Run Code Online (Sandbox Code Playgroud)

预期产量:

the following command could fail: …
Run Code Online (Sandbox Code Playgroud)

shell sh

32
推荐指数
6
解决办法
2万
查看次数

使用distutils构建一个基于ctypes的"基础"C库

按照这个建议,我编写了一个本机C扩展库,通过ctypes优化Python模块的一部分.我之所以选择ctypes而不是编写CPython本地库,是因为它更快更容易(只有几个函数内部都有紧密的循环).

我现在遇到了障碍.如果我希望使用distutils可以轻松地安装我的工作python setup.py install,那么distutils需要能够构建我的共享库并安装它(可能是).但是,这不是一个Python扩展模块,所以据我所知,distutils不能这样做./usr/lib/myproject

我发现了一些对其他人有这个问题的引用:

我知道我可以做一些原生的东西而不是为共享库使用distutils,或者确实使用我的发行版的包装系统.我担心的是,这会限制可用性,因为不是每个人都能轻松安装它.

所以我的问题是:当前最好的分发共享库的方法是什么,这些方法将由ctypes使用,但是其他方面是OS本机而不是Python扩展模块?

如果您可以对其进行扩展并证明为什么这是最好的方法,请随意回答上面链接的其中一个黑客.如果没有更好的东西,至少所有的信息都会在一个地方.

python ctypes distutils

23
推荐指数
2
解决办法
2598
查看次数

确定数据集的"晃动" - Python

我正在开发一个需要实现一组数据摆动的软件.这是我将收到的输入样本,与每个垂直像素带的亮度图合并: 替代文字

很容易看出左边距确实很晃动(即有一吨最小/最大值),我想生成一组图像的关键点.我已经对数据应用了高斯平滑函数~10次,但它开始时看起来很晃动.

有任何想法吗?

这是我的原始代码,但它不会产生非常好的结果(为了摆动):

def local_maximum(list, center, delta):
  maximum = [0, 0]

  for i in range(delta):
    if list[center + i] > maximum[1]: maximum = [center + i, list[center + i]]
    if list[center - i] > maximum[1]: maximum = [center - i, list[center - i]]

  return maximum

def count_maxima(list, start, end, delta, threshold = 10):
      count = 0

  for i in range(start + delta, end - delta):
    if abs(list[i] - local_maximum(list, i, delta)[1]) < threshold: count += 1

  return …
Run Code Online (Sandbox Code Playgroud)

python statistics list frequency-distribution frequency-analysis

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

如何从三个ReceiveStream一次读取一行?

asyncio有StreamReader.readline(),允许这样的东西:

while True:
    line = await reader.readline()
    ...
Run Code Online (Sandbox Code Playgroud)

(我认为async for在asyncio中没有,但那将是明显的演变)

如何实现三重奏的等效?

在三人组0.9中,我没有直接看到任何高级别的支持.我所看到的只是ReceiveStream.receive_some()返回任意大小的二进制块; 对我来说,解码并将其转换为线性似乎并非易事.我可以使用标准库函数或代码片段吗?我发现io stdlib模块看起来很有前途,但我认为没有办法提供"feed"方法.

python python-trio

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

在编译C代码时,如何指定从.so库导出的函数?

我的"C"代码中有许多功能.当我编译.so时,我在结果.so文件中看到所有名称.如何指定(在代码或make文件中)只应导出某些函数,而其他函数仅供内部使用.

c

4
推荐指数
2
解决办法
2963
查看次数