小编Ric*_*ard的帖子

将docstrings添加到namedtuples?

是否可以以简单的方式将文档字符串添加到namedtuple?

我试过了

from collections import namedtuple

Point = namedtuple("Point", ["x", "y"])
"""
A point in 2D space
"""

# Yet another test

"""
A(nother) point in 2D space
"""
Point2 = namedtuple("Point2", ["x", "y"])

print Point.__doc__ # -> "Point(x, y)"
print Point2.__doc__ # -> "Point2(x, y)"
Run Code Online (Sandbox Code Playgroud)

但这并没有削减它.是否有可能以其他方式做?

python docstring namedtuple

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

将`__str__`方法添加到Boost Python C++类时构建问题

我已经开始尝试使用boost python并遇到问题.我试图将一个C++类暴露给python,这没有任何问题.但我似乎无法实现__str__类的功能而不会出现我不明白的构建错误.

我正在使用boostpro的boost 1_42 prebuild.我使用cmake和vs2010编译器构建库.

我有一个非常简单的设置.头文件(tutorial.h)如下所示:

#include <iostream>
namespace TestBoostPython{
    class TestClass {
        private:
            double m_x;
        public:
            TestClass(double x);
            double Get_x() const;
            void Set_x(double x);
    };
    std::ostream &operator<<(std::ostream &ostr, const TestClass &ts);
};
Run Code Online (Sandbox Code Playgroud)

和相应的cpp文件看起来像:

#include <boost/python.hpp>
#include "tutorial.h"

using namespace TestBoostPython;

TestClass::TestClass(double x)
{
    m_x = x;
}

double TestClass::Get_x() const
{
    return m_x;
}
void TestClass::Set_x(double x)
{
    m_x = x;
}

std::ostream &operator<<(std::ostream &ostr, const TestClass &ts)
{
    ostr << ts.Get_x() << "\n";
    return ostr;
} …
Run Code Online (Sandbox Code Playgroud)

python boost-python

18
推荐指数
2
解决办法
2997
查看次数

用于C++应用程序中库的良好跨平台函数语言?

在跨平台Qt C++应用程序中使用快速函数式语言库有哪些选择?

看起来几乎所有语言(无论是否有功能)都能以一种简单的方式调用C/C++代码.我想反过来 - 使用C++在Qt中编写应用程序,用于状态业务逻辑,GUI和内容,但是下拉并使用核心计算库的函数语言.

以这种方式哪些易于使用?例如,OCaml代码可以编译成静态库,然后由C++应用程序使用吗?

谢谢,里卡德

c++ functional-programming cross-platform

10
推荐指数
1
解决办法
551
查看次数

在DLR上建立一个小型表达式DSL,还是用F#手动滚动?

我正在构建一个类似电子表格的应用程序,其中许多小型计算需要在树结构中拼接在一起.这些计算是用户定义的,我需要一种方法让用户在运行时输入它们.

我目前的方法是在F#中编写一个小的"表达式DSL",我用FParsec解析输入,基于区分联合构建语法树,然后可以计算表达式.这非常有效.

但是,我正在考虑将语言基于DLR.沿着这条路走下去是否有任何优势(解析输入,使用Scripting.AST东西而不是我自己的东西生成AST,让DLR处理计算的执行)?

每次计算都可能非常小.计算之间的依赖关系将在更高层次上得到处理.

我可以期待更好的性能,因为DLR会为表达式生成CIL代码,或者开销是否会消耗掉它?

(至于使用像IronPython这样的现有语言,它可能很难,因为我计划在语言语法中添加很多切片和骰子操作符和维度处理的东西)

dsl f# dynamic-language-runtime expression-trees

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

将Python持久层嵌入到C++应用程序中 - 好主意?

说我要写一个带有薄GUI层的应用程序,一个非常胖的计算层(做计算量很大的校准和其他长时间运行的东西)和相当简单的持久层.我正在寻找用C++构建GUI +计算层(使用Qt作为gui部分).

现在 - 这将是一个疯狂的想法建立在Python的持久性层,使用的SQLAlchemy,并将其嵌入到C++应用,通过lightweigth数据传输对象让海誓山盟层接口(用C++编写,但是从蟒访问)?

(我倾向于另一种选择,可能是从一开始就用Python编写应用程序,使用PyQt包装器,然后调用C++进行计算任务)

谢谢,里卡德

c++ python orm embedded-language

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

在Python中将有状态对象与无状态计算分开的位置?

以下哪些代码片段最"pythonic"?在这个例子中计算是微不足道的,但可以假设在现实生活中是复杂的.

class A(object):
    """Freely mix state and calcs - no good I presume"""
    def __init__(self, state):
        self.state = state

    def calc_with_state(self, x):
        return (self.state + x)**2
Run Code Online (Sandbox Code Playgroud)

要么

class B(object):
    """Separate state from calc by a static method"""
    @staticmethod
    def inner_calc(u, v):
        return (u + v)**2

    def __init__(self, state):
        self.state = state

    def calc_with_state(self, x):
        return B.inner_calc(self.state, x)
Run Code Online (Sandbox Code Playgroud)

要么

class C(object):
    """Break out the calculation in a free function"""
    def __init__(self, state):
        self.state = state

    def calc_with_state(self, x):
        return outer_calc(self.state, x) …
Run Code Online (Sandbox Code Playgroud)

python

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

如何定义异常类型和另一种类型之间的递归关系?

创建包含从F#中抛出的类型的异常的最佳方法是什么?以下不起作用:

// this fails since HeaderInfo is not yet defined. Can't seem use the and-keyword
// on HeaderInfo
exception MissingHeader of string*HeaderInfo

type HeaderInfo =
    {
        DefaultHeaderIndices: Map<string, int>;
        AdditionalStudyIndices: Map<string, int>;
        VolumeIndex: int option;
    }
    with member this.GetCommonIndex(name) = 
            match this.DefaultHeaderIndices.TryFind(name) with
            | Some(idx) -> idx
            | None ->
                match this.AdditionalStudyIndices.TryFind(name) with
                | Some(idx) -> idx
                | None ->
                    match this.VolumeIndex with
                    | Some(idx) when name = optionalHeader -> idx
                    | _ -> raise <| MissingHeader(name, this)
Run Code Online (Sandbox Code Playgroud)

谢谢!

f# exception

0
推荐指数
2
解决办法
152
查看次数