-3

I have this C++ code which I am not able to understand from some time. I need to write an equivalent code in Python.

eLst is an instance of structure tEins.

for(int iEng = 0; iEng < eLst.length(); iEng++)
{
    //loop over connectors
    QList <QString> connKeys = eLst.at(iEng).connectors.uniqueKeys();
    for(int iCon = 0; iCon < connKeys.length(); iCon++)
    {
        QString connName =  eLst.at(iEng).connectors[connKeys.at(iCon)].name;

        //loop over frameports(frames) of connector
        QList <QString> frPortKeys = eLst.at(iEng).connectors[connKeys.at(iCon)].framePorts.uniqueKeys();
        // ...
    }
}

Definitions of the structures used in the above code snippet:

typedef struct Eins
{
    QString name;
    QHash<QString, tCanController> commControllers;
    QHash<QString, tCanConnector> connectors;
} tEIns

typedef struct CanConnector
{
    QString name;
    QString controllerName;
    QHash<QString, tFramePort> framePorts;
    QHash<QString, tCanFrame> inOutFrames;
} tCanConnector;

typedef struct CanController
{
    QString name;
} tCanController;
halfer
  • 19,824
  • 17
  • 99
  • 186
  • What did you not understand ? – Abhishek Kulkarni Mar 24 '20 at 06:52
  • Please provide a working example. In your case even closing braces are missed. – Dmitry Kuzminov Mar 24 '20 at 07:05
  • @AbhishekKulkarni I am not getting the use and meaning of the keywords used. also the functionality. I just want to know what "at" "uniqueKeys()" does. I tried in google but not getting thorough meaning of it – Basavaprabhu Sherikar Mar 24 '20 at 07:20
  • 1
    You state `"eLst is an instance of structure tEins"`. That can't be true. `eLst` looks to be a container ([`std::vector`](https://en.cppreference.com/w/cpp/container/vector)?) of `tEins`. – G.M. Mar 24 '20 at 07:26
  • @Basavaprabhu, you said eLst is an instance of tEIns, but reading the code it seems that it would more likely be a map of , and the "at" you don't understand can be used to get the tEIns corresponding to the key "iEng". The "uniqueKeys()" is uses to eliminate duplicate keys in a hashmap. Please correct your question if you want a formal answer. – aTom Mar 24 '20 at 07:27

1 Answers1

1

eLst would most probably be some kind of sequential container e.g. QVector, QList std::vector or some other variation which is why it's being used in a counter for loop sequentially using an index. You can think of it as a list in Python where you can access an element using an index in subscript operator i.e. list[index].

at() is a method that reutrns the element after performing bounds-checking, otherwise, it throws an exception. It is similar to [] subscript operator where bounds-checking is not performed.

QHash is similar to the dictionary in Python.

uniqueKeys() method returns the list of distinct keys that exist in the QHash. You can think of it as getting all the distinct keys from the dictionary. See: How to return dictionary keys as a list in Python?.

Here's the equivalent simplified code in C++11 (might be helpful):

for ( const auto& e : eLst )
{
    const auto& connKeys = e.connectors.uniqueKeys();
    for ( const auto& k : connKeys )
    {
        const auto& connName =  e.connectors[ k ].name;
        const auto& frPortKeys = e.connectors[ k ].framePorts.uniqueKeys();

Here's the equivalent Python code for your reference (not tested):

for e in eLst:
    connKeys = e.connectors.uniqueKeys()
    for k in connKeys:
        connName = e.connectors[ k ].name
        frPortKeys = e.connectors[ k ].framePorts.uniqueKeys()

The above code snippets are only for your reference. You need to convert the code according to your own data structures and Python APIs.

Azeem
  • 11,148
  • 4
  • 27
  • 40