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
[0,
1,
2,
3,
4,
5,
6,
7]
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).
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.
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…
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
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.
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.