我们是否应该避免在C++;为了成为;蟒蛇;,怎么做? 我在Python和前卵子阶段的C++阶段,但我正在尽最大努力,特别是用“不要重复自己”的原则。

我们是否应该避免在C++;为了成为;蟒蛇;,怎么做? 我在Python和前卵子阶段的C++阶段,但我正在尽最大努力,特别是用“不要重复自己”的原则。,c++,python,abstraction,repeat,hardcoded,C++,Python,Abstraction,Repeat,Hardcoded,我有一个要打开的多通道原始文件格式,带有一个主ascii头,其中的字段可以表示为字符串和整数(总是编码为字符并填充空格)。第二部分是N个头,其中N是主头的一个字段,每个头本身都有更多的文本和数字字段(编码为ascii),这些字段涉及组成文件其余部分的实际16位多通道流的长度和大小 到目前为止,我用C++编写了以下工作代码: #include <iostream> #include <sstream> #include <fstream> #include &l

我有一个要打开的多通道原始文件格式,带有一个主ascii头,其中的字段可以表示为字符串和整数(总是编码为字符并填充空格)。第二部分是N个头,其中N是主头的一个字段,每个头本身都有更多的文本和数字字段(编码为ascii),这些字段涉及组成文件其余部分的实际16位多通道流的长度和大小

到目前为止,我用C++编写了以下工作代码:

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>

using namespace std;

struct Header {
    string version;
    string patinfo;
    string recinfo;
    string start_date;
    string start_time;
    int header_bytes;
    string reserved;
    int nrecs;
    double rec_duration;
    int nchannels;
};

struct Channel {
    string label;
    string transducertype;
    string phys_dim;
    int pmin;
    int pmax;
    int dmin;
    int dmax;
    string prefiltering;
    int n_samples;
    string reserved;
};


int main()
{
    ifstream edf("/home/helton/Dropbox/01MIOTEC/06APNÉIA/Samples/Osas2002plusQRS.rec", ios::binary);

    // prepare to read file header
    Header header;
    char buffer[80];

    // reads header fields into the struct 'header'
    edf.read(buffer, 8);
    header.version = string(buffer, 8);

    edf.read(buffer, 80);
    header.patinfo = string(buffer, 80);

    edf.read(buffer, 80);
    header.recinfo = string(buffer, 80);

    edf.read(buffer, 8);
    header.start_date = string(buffer, 8);

    edf.read(buffer, 8);
    header.start_time = string(buffer, 8);

    edf.read(buffer, 8);
    stringstream(buffer) >> header.header_bytes;

    edf.read(buffer, 44);
    header.reserved = string(buffer, 44);

    edf.read(buffer, 8);
    stringstream(buffer) >> header.nrecs;

    edf.read(buffer,8);
    stringstream(buffer) >> header.rec_duration;

    edf.read(buffer,4);
    stringstream(buffer) >> header.nchannels;

    /*
    cout << "'" << header.version << "'" << endl;
    cout << "'" << header.patinfo << "'" << endl;
    cout << "'" << header.recinfo << "'" << endl;
    cout << "'" << header.start_date << "'" << endl;
    cout << "'" << header.start_time << "'" << endl;
    cout << "'" << header.header_bytes << "'" << endl;
    cout << "'" << header.reserved << "'" << endl;
    cout << "'" << header.nrecs << "'" << endl;
    cout << "'" << header.rec_duration << "'" << endl;
    cout << "'" << header.nchannels << "'" << endl;
    */

    // prepare to read channel headers
    int ns = header.nchannels; // ns tells how much channels I have
    char title[16]; // 16 is the specified length of the "label" field of each channel

    for (int n = 0; n < ns; n++)
    {
        edf >> title;
        cout << title << endl; // and this successfully echoes the label of each channel
    }


    return 0;
};
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构头{
字符串版本;
字符串patinfo;
字符串recinfo;
字符串开始日期;
字符串开始时间;
int头字节;
保留字符串;
int nrecs;
双记录持续时间;
内部通道;
};
结构通道{
字符串标签;
字符串转换器类型;
弦物理;
int-pmin;
int-pmax;
int-dmin;
int-dmax;
串预滤波;
int n_样本;
保留字符串;
};
int main()
{
ifstream edf(“/home/helton/Dropbox/01MIOTEC/06APNÉIA/Samples/Osas2002plusQRS.rec”,ios::binary);
//准备读取文件头
收割台;
字符缓冲区[80];
//将头字段读入结构“header”
edf.read(缓冲器,8);
header.version=字符串(缓冲区,8);
edf.read(缓冲器,80);
header.patinfo=字符串(缓冲区,80);
edf.read(缓冲器,80);
header.recinfo=字符串(缓冲区,80);
edf.read(缓冲器,8);
header.start_date=字符串(缓冲区,8);
edf.read(缓冲器,8);
header.start_time=字符串(缓冲区,8);
edf.read(缓冲器,8);
stringstream(缓冲区)>>header.header\u字节;
edf.read(缓冲器,44);
header.reserved=字符串(缓冲区,44);
edf.read(缓冲器,8);
stringstream(缓冲区)>>header.nrecs;
edf.read(缓冲器,8);
stringstream(缓冲区)>>header.rec_持续时间;
edf.read(缓冲器,4);
stringstream(缓冲区)>>header.nchannes;
/*

cout这段代码直截了当、简单易懂。如果它正常工作,不要浪费时间修改它。我确信有很多代码写得很糟糕、很复杂、很难理解(可能是不正确的)应该首先修复的代码:)

此代码简单明了,易于理解。如果它正常工作,请不要浪费时间更改它。我确信有大量编写糟糕、复杂且难以理解(可能不正确)的代码应该首先修复:)

您是正确的:编写时,代码是重复的(并且没有错误检查)。您读取的每个字段都需要执行三到五个步骤,具体取决于读取的数据类型:

  • 从流中读取字段
  • 确保读取成功
  • 分析数据(如有必要)
  • 确保分析成功(如有必要)
  • 将数据复制到目标位置
  • 您可以将所有这三个包打包成一个函数,这样代码就不会重复。例如,考虑以下函数模板:

    template <typename TStream, typename TResult>
    void ReadFixedWidthFieldFromStream(TStream& str, TResult& result, unsigned sz) 
    {
        std::vector<char> data(sz);
    
        if (!str.read(&data[0], sz))
            throw std::runtime_error("Failed to read from stream");
    
        std::stringstream ss(&data[0]);
        if (!(ss >> result))
            throw std::runtime_error("Failed to parse data from stream");
    }
    
    // Overload for std::string:
    template <typename TStream>
    void ReadFixedWidthFieldFromStream(TStream& str, std::string& result, unsigned sz) 
    {
        std::vector<char> data(sz);
    
        if (!str.read(&data[0], sz))
            throw std::runtime_error("Failed to read from stream");
    
        result = std::string(&data[0], sz);
    }
    

    您是正确的:编写时,代码是重复的(并且没有错误检查)。您读取的每个字段确实需要您执行三到五个步骤,具体取决于读取的数据类型:

  • 从流中读取字段
  • 确保读取成功
  • 分析数据(如有必要)
  • 确保分析成功(如有必要)
  • 将数据复制到目标位置
  • 您可以将所有这三个包打包成一个函数,这样代码就不会重复。例如,考虑以下函数模板:

    template <typename TStream, typename TResult>
    void ReadFixedWidthFieldFromStream(TStream& str, TResult& result, unsigned sz) 
    {
        std::vector<char> data(sz);
    
        if (!str.read(&data[0], sz))
            throw std::runtime_error("Failed to read from stream");
    
        std::stringstream ss(&data[0]);
        if (!(ss >> result))
            throw std::runtime_error("Failed to parse data from stream");
    }
    
    // Overload for std::string:
    template <typename TStream>
    void ReadFixedWidthFieldFromStream(TStream& str, std::string& result, unsigned sz) 
    {
        std::vector<char> data(sz);
    
        if (!str.read(&data[0], sz))
            throw std::runtime_error("Failed to read from stream");
    
        result = std::string(&data[0], sz);
    }
    

    我想说的是没有PythC++ C++代码。枯燥的原理适用于两种语言,但是很多被认为是“python”的方法只是用Python特定的结构来表达Python中最短、最甜蜜的方式。

    <代码> lambda < /C> >,有时不被认为是非常pythic的,并且保留在没有其他解决方案的情况下,但只是被添加到C++标准中。C++没有关键字参数,它非常Pythonic。C++程序员不喜欢在不必要时构造<代码> map <代码>,而Python程序员可能会使用。w

    dict
    在许多问题上,它们只是碰巧使意图比有效的替代方案更清晰

    如果要保存键入内容,请使用,然后:


    这将节省你的几行。但是,比那些几行更重要的是你已经实现了少量的模块化:<强> > < />强,读一个字段,<强> >什么> /强>字段是你程序的独立部分。

    < P>我不说有Pythic C++代码。干法原理适用于两个LA。但是,很多被认为是“python”的方法只是使用Python特定的构造来表达Python中的逻辑的最短、最甜蜜的方式。C++语言是完全不同的。

    <代码> lambda < /C> >,有时不被认为是非常pythic的,并且保留在没有其他解决方案的情况下,但只是被添加到C++标准中。C++没有关键字参数,它非常Pythonic。C++程序员不喜欢在不必要时构造<代码> map <代码>,而Python程序员可能会使用。w

    dict
    在许多问题上,它们只是碰巧使意图比有效的替代方案更清晰

    如果要保存键入内容,请使用,然后:

    这应该可以为您节省很多行。但比这几行更重要的是,您已经实现了少量的模块化:如何读取字段和读取内容字段现在是程序的独立部分。

    用于从
    header.version = read_field(edf, 8);
    header.patinfo = read_field(edf, 80);
    
    struct Header {
        char version[8];
        char patinfo[80];
        char recinfo[80];
        char start_date[8];
        char start_time[8];
        int header_bytes;
        char reserved[44];
        int nrecs;
        double rec_duration;
        int nchannels;
    };
    
    struct Header h;
    edf.read(&h,sizeof(struct Header));
    
    >>> import this
    The Zen of Python, by Tim Peters
    
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!