我正在使用Fortran创建一个Python模块f2py.如果在Fortran模块中遇到错误,我想在Python程序中产生错误(包括错误消息).请考虑以下示例:
Fortran代码(test.f):
subroutine foo(a,m)
integer :: m,i
integer, dimension(m) :: a
!f2py intent(in) :: m
!f2py intent(in,out) :: a
!f2py intent(hide), depend(a) :: m=shape(a)
do i = 1,m
if ( a(i) .eq. 0 ) then
print*, 'ERROR HERE..?'
end if
a(i) = a(i)+1
end do
end subroutine
Run Code Online (Sandbox Code Playgroud)
这个非常简单的程序增加1了每个元素a.但如果a(i)等于零,应该产生错误.随附的Python代码:
import test
print test.foo(np.array([1,2],dtype='uint32'))
print test.foo(np.array([0,2],dtype='uint32'))
Run Code Online (Sandbox Code Playgroud)
输出现在是:
[2 3]
ERROR HERE..?
[1 3]
Run Code Online (Sandbox Code Playgroud)
但我希望Python程序能够抓住错误.请帮忙.
回答
stopFortran中的命令就是这样做的.考虑更新的Fortran代码:
subroutine foo(a,m)
integer :: m,i …Run Code Online (Sandbox Code Playgroud) 最近有人建议我作为包管理器查看 conda。不幸的是,我没有成功地找到如何让我的编译器找到与 conda 安装的仅标头库?理想情况下,我希望根本不必手动指定编译器的路径。
(上下文是我来自 macOS 上的自制软件,它会在正确的位置创建符号链接。显然这是 conda 避免的。但是,如果有一种简单的方法来编译简单的示例就好了!)
例如,如果我的代码是下面的代码。注意:这个问题是通用的,与特定的包无关,我也不想再次手动指定我的特定虚拟环境。
#include <iostream>
#include <xtensor/xarray.hpp>
#include <xtensor/xio.hpp>
int main()
{
xt::xarray<double> a
{{1.0, 2.0, 3.0},
{2.0, 5.0, 7.0},
{2.0, 5.0, 7.0}};
std::cout << a;
}
Run Code Online (Sandbox Code Playgroud)
我已经使用“安装”了该库
conda create --name example
source activate example
conda install -c conda-forge xtensor-python
Run Code Online (Sandbox Code Playgroud)
现在我想编译
clang++ -std=c++14 test.cpp
Run Code Online (Sandbox Code Playgroud)
请注意,我知道这是有效的:
clang++ -std=c++14 -I~/miniconda3/envs/example/include test.cpp
Run Code Online (Sandbox Code Playgroud)
但我认为这不是我们想要的,因为:
example)。我想按照时间顺序对照片进行排序.因此,我从EXIF数据中提取时间作为字符串,然后我将其转换为std::time_t.但是我有时会得到错误的结果.我把问题简化为这个最小的例子.它有三个时间串,相隔一秒:
#include <vector>
#include <string>
#include <iostream>
#include <ctime>
#include <iomanip>
#include <sstream>
int main()
{
std::vector<std::string> vec;
vec.push_back("2016:07:30 09:27:06");
vec.push_back("2016:07:30 09:27:07");
vec.push_back("2016:07:30 09:27:08");
for ( auto & i : vec )
{
struct std::tm tm;
std::istringstream iss;
iss.str(i);
iss >> std::get_time(&tm,"%Y:%m:%d %H:%M:%S");
std::time_t time = mktime(&tm);
std::cout << i << " time = " << time << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
编译clang++ -std=c++14 test.cpp.
输出:
2016:07:30 09:27:06 time = 1469867226
2016:07:30 09:27:07 time = 1469863627
2016:07:30 …Run Code Online (Sandbox Code Playgroud) 我想使用逗号作为小数点分隔符来打印小数点。当我这样做时
import locale
locale.setlocale(locale.LC_ALL, 'nl_NL')
'{0:#.2n}'.format(1.1)
Run Code Online (Sandbox Code Playgroud)
我明白了'1,1'。逗号在那里,但精度只有 1,而我将其设置为 2。怎么会?
请注意,此格式的构造如下:
#:“该'#'选项导致使用“替代形式”进行转换。...此外,对于'g'和'G'转换,不会从结果中删除尾随零。”.2: 精确。n:“数字。这与 相同'g',只是它使用当前区域设置来插入适当的数字分隔符。”其中引用来自手册:格式规范迷你语言。
按照评论中的建议使用{.2f}也不能达到我想要的效果:'1.10'。精度是正确的,但区域设置中的逗号被忽略。
我想沿着可能具有任意维度的多维矩阵的任意轴(例如 10 维数组的轴 5)执行求和缩减。矩阵使用行主格式存储,即vector与沿每个轴的步幅一起存储。
我知道如何使用嵌套循环执行此缩减(请参见下面的示例),但这样做会产生硬编码轴(缩减沿下面的轴 1)和任意数量的维度(下面的 4)。在不使用嵌套循环的情况下如何概括这一点?
#include <iostream>
#include <vector>
int main()
{
// shape, stride & data of the matrix
size_t shape [] = { 2, 3, 4, 5};
size_t strides[] = {60,20, 5, 1};
std::vector<double> data(2*3*4*5);
for ( size_t i = 0 ; i < data.size() ; ++i ) data[i] = 1.;
// shape, stride & data (zero-initialized) of the reduced matrix
size_t rshape [] = { 2, 4, 5};
size_t rstrides[] = {20, …Run Code Online (Sandbox Code Playgroud) 我有一个我在Python扩展中使用的只有头文件的C++库.我希望能够将它们安装到Python的包含路径中,这样我就可以非常轻松地编译扩展python3 setup.py build.我部分能干,但有两件事我无法工作(见下文):
如何使用python3 setup.py install安装头文件?目前我只获得了一些*.egg文件,但没有安装头文件.
如何保留模块的文件结构?目前,文件结构被错误地展平.
有以下几点 setup.py
from setuptools import setup
setup(
name = 'so',
description = 'Example',
headers = [
'so.h',
],
)
Run Code Online (Sandbox Code Playgroud)
我可以将模块上传到PyPi:
python3 setup.py bdist_wheel --universal
twine upload dist/*
Run Code Online (Sandbox Code Playgroud)
然后使用pip安装它:
pip3 install so
Run Code Online (Sandbox Code Playgroud)
在我的系统上,然后我在这里找到标题
/usr/local/include/python3.6m/so/so.h
Run Code Online (Sandbox Code Playgroud)
当我使用Python编译扩展时可用.
使用这种策略我不能简单地运行
python3 setup.py install
Run Code Online (Sandbox Code Playgroud)
在这种情况下,so*.egg会安装一些标头,但标头不会存储在编译器可用的位置.
当模块有点复杂,并且存在一些目录层次结构时,我也会遇到问题.对于以下内容setup.py
from setuptools import setup
setup(
name = 'so',
description = 'Example',
headers = [
'so.h',
'so/implementation.h',
],
)
Run Code Online (Sandbox Code Playgroud)
问题是标头已安装到 …
我想要一个带有两个参数的选项。即希望能够使用
$ ./foo --path "old" "new"
Run Code Online (Sandbox Code Playgroud)
或者我真正想要的是:
$ ./foo --path "old" "new" --path "old" "new"
Run Code Online (Sandbox Code Playgroud)
但我不知道该怎么办?(事实上我担心这可能是不可能的......)
我知道如何有一个重复选项(见下文),但这实际上不是我想要的。
#!/usr/bin/env python3
'''foo
Usage:
foo [--path ARG]...
Options:
--path=ARG Repeating option.
'''
import docopt
args = docopt.docopt(__doc__)
print(args)
Run Code Online (Sandbox Code Playgroud)
可以调用
$ ./foo --path "old" --path "new"
Run Code Online (Sandbox Code Playgroud) 在更新到macOS Mojave之后,OS在/usr/local/include使用clang++(从命令行,即Xcode外部)进行编译时,无法在其中搜索标头。请注意,/usr/local/include对于我的系统,CMake将在其中安装标头的默认路径,因此对于手动包含此标头感到很奇怪。
顺便说一句,我还执行了全新安装,导致了同样的问题。在后者中,我具有以下顺序:
xcode-select --install。该问题似乎与该问题类似,建议该问题:
xcode-select --install。这显然不能解决问题。通过重新运行install命令坚持这一点会导致error: command line tools are already installed。xcode-select -p。它显然应该读为/Library/Developer/CommandLineTools,而不是 /Applications/Xcode.app/Contents/Developer我的情况。建议是“从Applications目录中删除[sic] Xcode”,这似乎有些残酷。如何解决呢?
我有一个std::vector<double>或随机数 [0, 1]。(如何)我可以使用标准库将其转换为特定的(例如 Weibull)分布(使用分布的累积分布)吗?
需要明确的是:我没有标准库认为的“生成器”(我没有一个operator()返回整数的类)。我已经有一个随机双精度列表 [0, 1] 并且只想使用标准库的不同分布(例如 Weibull)的累积密度函数的实现。
我有一个与 STL 向量非常相似的类(差异对于 pybind11 类型caster 来说并不重要,所以我在这里忽略它们)。我为这门课编写了一个类型转换程序。下面给出了我的代码的最小工作示例。代码下方包含一个显示问题的示例。
问题是我的脚轮非常有限(因为我已经使用过py::array_t)。原则上,该接口确实接受元组、列表和 numpy 数组。但是,当我基于类型名重载时,接口对于输入的元组和列表失败(只是选择了第一个重载,即使它是不正确的类型)。
我的问题是:怎样才能让型脚轮更加坚固?是否有一种有效的方法可以尽可能多地为 STL 向量类重用现有的类型转换程序?
#include <iostream>
#include <vector>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
// class definition
// ----------------
template<typename T>
class Vector
{
private:
std::vector<T> mData;
public:
Vector(){};
Vector(size_t N) { mData.resize(N); };
auto data () { return mData.data (); };
auto data () const { return mData.data (); };
auto begin() { return mData.begin(); };
auto begin() const { return mData.begin(); }; …Run Code Online (Sandbox Code Playgroud) c++ ×4
python ×4
include-path ×2
algorithm ×1
clang ×1
clang++ ×1
conda ×1
docopt ×1
f2py ×1
fortran ×1
header-only ×1
macos ×1
pybind11 ×1
python-3.x ×1
setup.py ×1
setuptools ×1
xtensor ×1