小编sla*_*r98的帖子

如何在没有子包的情况下创建包含多个文件的python包

我正在尝试创建一个包含几个类的包 (mypackage),但希望这些类包含在多个文件中。

例如,我希望 class_a.py 包含一个名为 ClassA 的类,等等...

因此,我想要以下文件结构:

  .../mypackage 
       __init__.py
       class_a.py
       class_b.py
       ...
Run Code Online (Sandbox Code Playgroud)

但是,我想按如下方式加载和使用该包:

load mypackage
a = mypackage.ClassA()
Run Code Online (Sandbox Code Playgroud)

我需要做什么(我在 __init__.py 中假设)文件才能使这成为可能。目前,它使用“mypackage.class_a.ClassA()”运行?

python python-module

7
推荐指数
2
解决办法
5822
查看次数

在C++中使用模板成员函数继承模板类

问题:我收到以下代码的以下错误,有谁知道原因?

问题: 我正在研究一个类(ClassB),它控制来自外部库(libMesh)的许多类的行为."...做某事......代码的一部分用于在具有模板函数的这些外部库类中设置一些变量.

我希望能够从继承类(ClassC)的构造函数中设置其中的一些值.但是,如果我这样做,就像在下面的代码中,我得到错误显示.如果我在构造函数中删除此命令,它可以正常工作.

我还包含一个更详细的示例,使用它产生相同的错误,但使用libmesh类本身,它说明了我想做的更好一点.我不确定我想要做什么的有用性,我主要想知道为什么这不起作用,因为它似乎应该.

我找到了另一个类似的帖子,但我似乎无法将它们应用到我的问题中.

模板继承内部类访问问题

谢谢你的帮助,安德鲁

错误:

XXXXXXX@XXXXX:~/Documents/programs/build$ make test
[100%] Building CXX object CMakeFiles/test.dir/source/test.cpp.o
test.cpp: In constructor ‘ClassC<T>::ClassC()’:
test.cpp:16:29: error: expected primary-expression before ‘int’
test.cpp:16:29: error: expected ‘;’ before ‘int’
make[3]: *** [CMakeFiles/test.dir/source/test.cpp.o] Error 1
make[2]: *** [CMakeFiles/test.dir/all] Error 2
make[1]: *** [CMakeFiles/test.dir/rule] Error 2
make: *** [test] Error 2
Run Code Online (Sandbox Code Playgroud)

简单代码:

// A class that sets that sets the value of something 
template <typename Type> class ClassB{
public:
    ClassB(){}
    template<typename TypeValue> void …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates

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

如何使用 clang python 绑定获取类方法定义?

给定以下 C++ 文件:

class Foo
{
public:
  Foo();

  void bar(int input);

  void another(int input, double & output);
};

void
Foo::bar(int input)
{
  input += 1;
}

void
Foo::another(int input, double & output)
{
  input += 1;
  output = input * 1.2345;
}
Run Code Online (Sandbox Code Playgroud)

如何利用 clang python 绑定来提取这两个方法的定义。我可以使用下面的 python 脚本获取类声明,但我似乎不知道如何提取完整的方法。例如,我想要以下信息:

void
Foo::another(int input, double & output)
{
  input += 1;
  output = input * 1.2345;
}
Run Code Online (Sandbox Code Playgroud)

Python脚本:

#!/usr/bin/env python
import clang.cindex
clang.cindex.Config.set_library_path('/opt/moose/llvm-3.7.0/lib')

def getCursors(cursor, output, kind):
    """
    Recursively extract all the cursors …
Run Code Online (Sandbox Code Playgroud)

c++ python parsing clang abstract-syntax-tree

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

MATLAB正则表达式跳过,如果是单词的一部分

给出以下示例:

str = 'deriv*dot(N,iv)';
expr = 'iv';
idx = regexp(str,expr);
Run Code Online (Sandbox Code Playgroud)

这将返回idx为4和13.我如何才能找到不属于单词的'iv'?

我尝试使用Lookr操作符来解决expr,但是无法得到我想要的结果.谢谢您的帮助.

regex matlab

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

如何在applescript中将文本写入iterm2会话?

我正在尝试为iTerm2创建脚本,该脚本将创建一个会话(通过拆分),然后在该新创建的会话中输入文本。如何激活新创建的会话?

tell application "iTerm"
    tell current session of current window
        split horizontally with default profile
    end tell

    -- Here is what I need help with?
    set _new_session to <what goes here> of current window
    tell _new_session
        write text "ls"
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)

applescript iterm2

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

C++访问继承类的成员,其中继承的类是模板参数

我正在使用libMesh FEM库,我正在尝试开发一个继承自libMesh的类(EqCore).这个类将提供一些我想要实际使用的类(MainEq)再次继承的附加功能.

这两个函数set_constant和get_constant导致下面的错误.这些工作如图所示,具有不同的继承方案(请参阅使用C++中的模板成员函数继承模板类).与此问题的区别在于,现在模板参数(Type)实际上是一个继承的类.这是一种危险的做法吗?

我很感激任何帮助使这个代码工作或找到一个替代方法.

错误信息:

在成员函数'void EqCore :: set_constant(std :: string,ParamType)':test_libmesh.cpp:26:57:error:在'>'令牌之前的预期primary-expression

在成员函数'ParamType EqCore :: get_constant(std :: string)':/ home /slaughter/Files/program/sources/test_libmesh.cpp:31:76:error:在'>'令牌之前的预期primary-expression

程序:

//! \example test_libmesh.cpp

#include <string>
using std::string;

// libMesh includes
#include <libmesh.h>
#include <libmesh_common.h> 
#include <equation_systems.h>
#include <transient_system.h>
#include <explicit_system.h>
#include <parameters.h>
#include <mesh.h>
using namespace libMesh;

// Fundamental behavior that will be used among many classes
template <typename Type> class EqCore : Type{
    public:

        // Class constructor
        EqCore(EquationSystems& sys, string name) : Type(sys, name, 1){}

        // A …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates disambiguation

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