How do I change some values obtained from the list to the values I want with a smart way?

Hi all, I’m trying to develop a software that can use to display related information of network cards.
One of the columns used to display the port number of the pcie slot corresponding to the network card.
For example: Slot A is plugged in a network card with 8 ports. The result displayed in the column is
All the relevant information I can obtain is in the folder under the pcie path configured by the operation system.
The list “iface” in the figure (A) is the related information of the network cards.

My current approach is to display the port number based on the pcie’s BDF (Bus, Device and function) in the folder.
Here is the code of displaying port number and the result is shown as figure (B).

def port_number(self, pci_dir_path)
sys_dev_pci_dir_path = os.path.realpath(pci_dir_path)
port_index_path = sys_dev_pci_dir_path.split('/')[-1].split('.')[-1]      
return port_index_path

But the expected result of port number I want is same as the picture (C).
I’m thinking that try to use the same values in the list iface with a smart way that can automatically divide the value into 0-7, but I don’t know how to implement it… or are there any libraries that can help me implement it?
Any advice would be appreciate, thanks. :slight_smile:

Could you give us the list of sys_dev_pci_dir_path?

Hi, @Nineteendo
The following figure is the sys_dev_pci_dir_path.

Hmm, the last number is the function and not the port number: Bus:Device.Function (BDF) Notation - Xen

Hi @Nineteendo
Yes I know the last number is the function and not the port number.
I used it as port number because I do not have better ideas to display the port number with other information… :slightly_frowning_face:

Hmm, I’m not sure if that’s what you want, but are you trying to extract the value I marked bold bellow?


Hi @FelixLeg
No, the value you marked not what I want.

Do you care about how these values are matched up with the input, or are you just trying to cycle through 0-7?

If it’s the former, you’ll need to specify some relationship between the input path and the output integer.

If all you want is to cycle through 0...7, I’d use enumerate(your_list) to get a running index and i % 8 (modular arithmetic) to convert that number to the desired range.

Hi @jamestwebber
I do care about the values are matched up with the input, I have keep trying to find relationship between the input path and the output integer, and I also had tried to cycle through 0-7 before by using for loop.

I have questions about the cycle through 0-7, I don’t understand that using i % 8 (modular arithmetic) to convert that number to the desired range, I tried the enumerate() as following code and get all 0s in following picture, the result is the same as I use for loop to cycle through 0-7 before…

     def port_index_get(self, pci_dir_path):
         sys_dev_pci_dir_path = os.path.realpath(pci_dir_path)
         port_index_path = sys_dev_pci_dir_path.split('/')[-1].split('.')[-1]
         port_index_path = enumerate(port_index_path)
         for port_index, test in port_index_path:
             return port_index

Did I do something wrong here? :joy:
Thank you.

I’m confused: what is the relationship you want between the devices and the output? Can you post a full list of inputs and desired outputs?

From the images you posted, it seemed like you just want to number them in a loop.

I was suggesting using it outside loop you call port_index_get in, like

for i, pci_dir_path in enumerate(list_of_pci_dir_paths):
    port_index = i % 8  # port_index will cycle through 0..7
    ... do whatever it is you are trying to do ...

And you don’t need a port_index_get function at all, at that point.

This is assuming your code has some list of paths that you’re trying to process one at time.

Hi @jamestwebber,
Thanks for your advice and sorry for the late reply.

The information shown on the image all obtained in the path /sys/bus/pci/device/BDF_id/… as following picture to the MAC address.

So I was thinking maybe I can get the port index I want in the path.
But because of there is no any information can let me use for displaying as port index so I fail.

This solution is suitable for solving the problem of the images.
But if the port index I wanted is displayed as follow, this solution won’t work.

Onboard	   0
Onboard    1
Onboard    2
Onboard    3
SLOTA 	   0
SLOTA 	   1
SLOTA 	   2
SLOTA 	   3
SLOTA 	   4
SLOTA 	   5
SLOTA 	   6
SLOTA 	   7
SLOTB      0
SLOTB      1
SLOTB      2
SLOTB      3

I had change the way to modify the result in a file, if you are interested, please feel free to discuss it together.
Thank you.