gpt4 book ai didi

c++ - 将 C++ 代码移植到 Python

转载 作者:行者123 更新时间:2023-11-30 04:24:30 24 4
gpt4 key购买 nike

我正在使用 swig 来包装这个 C++ 项目,并且在将所有头文件导入我的 python 代码之后,我现在正致力于在 python 中重新创建主类,但是我在寻找对应的数据结构和数据类型。下面是main.cpp下的main方法

int main( int argc, char *argv[] )
{
string exe_name = argv[ 0 ];

string usage = "Usage: " + exe_name + " unigene.fasta [options]\n\
Options:\n\
-g <GC content> \tminimum GC content in percentage (Default 45).\n\
-G <GC content> \tmaximum GC content in percentage (Default 55).\n\
-m <temperature> \tlowest melting temperature in degrees of celsius (Default 0).\n\
-M <temperature> \thighest melting temperature in degrees of celsius (Default 100).\n\
-r <repeat.fasta> \tfasta file containing repeat DNA sequences.\n\
-t <top number> \tnumber of top oligos selected for each unigene (Default 1).\n\n";

string unigene_file;
// options only, each must have an associated value
int min_gc = 45;
int max_gc = 55;
int min_tm = 0;
int max_tm = 100;
int top_num = 1;
string repeat_file;
vector< string > m_argument;
for ( int i = 0; i < argc; i ++ ) {
string new_arg = argv[ i ];
m_argument.push_back( new_arg );
}

parse_argument( m_argument, usage, unigene_file,
min_gc, max_gc, min_tm, max_tm, top_num, repeat_file );

// initialize filtration parameters
float norm_min_gc = (float)min_gc / 100.0;
float norm_max_gc = (float)max_gc / 100.0;
string splice_file; // empty splice file
filt_param m_filtparam( norm_min_gc, norm_max_gc, min_tm, max_tm,
repeat_file, splice_file );

// screen unigenes for unique oligos
seqs m_unigene;
m_unigene.init( unigene_file );

// map from unigene names to oligo sequences
map< string, vector< string > > uniq_oligo;
get_unique_oligo( m_unigene, m_filtparam, top_num, uniq_oligo );

// output oligos
if ( uniq_oligo.empty() ) {
cout << "No valid oligo has been found. " << endl <<
"It could be due to one of the following reasons: " << endl <<
"1. input sequences are not unique, or " << endl <<
"2. they are repetitive DNAs, or " << endl <<
"3. GC% range is too restricted, etc. " << endl;
}
map< string, vector< string > > :: const_iterator m_itr;
for ( m_itr = uniq_oligo.begin(); m_itr != uniq_oligo.end(); m_itr ++ ) {
for ( int o_idx = 0; o_idx < (int)m_itr->second.size(); o_idx ++ ) {
cout << ">unique-oligo-" << o_idx+1 << "-of-unigene-" <<
m_itr->first << endl;
cout << m_itr->second[ o_idx ] << endl;
}
}

//system("PAUSE");
return 1;
}

例如,当我尝试将一个空字符串 (repeat_file) 传递给 filtparam() 时,在 python 中,我得到了错误

in method 'new_filt_param', argument 5 of type 'string const &'

如何在 python 中声明一个“string const &”类型?

还有python中有'map'这样的结构吗?

最佳答案

您正在寻找的是 dict,它的工作原理与 C++ 的 map 基本相似。更多信息可以在 documentation 中找到.

不同的问题:您是否有任何理由在 Python 中重新创建 main() 而不是简单地用 SWIG 包装那个?虽然 SWIG 能够以有限的方式处理 std::string 之类的事情,但我会尝试使用仅使用指针/数组的更简单的接口(interface),而不是尝试传递任何 C++ 对象或构造.

关于c++ - 将 C++ 代码移植到 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12723491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com