Skip to content

Conversation

@qduk
Copy link
Collaborator

@qduk qduk commented May 14, 2021

No description provided.

@dgjustice
Copy link
Contributor

@qduk, a friend needed a solution to this very problem today, so I put something together that I believe addresses the issues we found along the way. Basically, it separates characters into classes with weights and does a 2-stage sort. Feel free to copy/paste it to your heart's content.

https://gist.github.com/dgjustice/84117af73541728ece78cb33c6d37ecb

@qduk
Copy link
Collaborator Author

qduk commented Jul 26, 2021

@dgjustice Finally had a chance to dive into this. There was a lot I didn't understand at first but what you wrote is super cool! One issue I ran into is that it sorts interface without "/" or "." not in numerical order. For example, if I use the test data of
test_data = ['Port-channel40', 'Port-channel160', "Loopback99", "Loopback101", "Loopback98", "Loopback102"] I get

Loopback101
Loopback102
Loopback98
Loopback99
Port-channel160
Port-channel40

I ended up solving that by sorting the list in place using

for k,v in groupby(test_data, lambda x: "/" not in x and "." not in x):
    if k == True:
        for r,s in groupby(v, lambda x: x.rstrip("0123456789")):
            s = list(s)
            first_index = test_data.index(s[0])
            last_index = test_data.index(s[-1])
            test_data[first_index:last_index+1] = sorted(test_data[first_index:last_index+1], reverse=True)

I'm sure there is a way to do it in the iteration of the tree but the above method seemed to work.

I also ran into an issue where it seemed to be skipping interfaces. When I used this test data
["Gi1/0/1", "Gi1/0/3", "Gi1/0/3.100", "Gi1/0/2", "Gi1/0/2.50", "Po40", "Po160", "Lo10"]
I get this back.

Gi1/0/1
Gi1/0/2.50
Gi1/0/3.100
Lo10
Po160
Po40

I'm not really sure why it's missing the Gi1/0/2 and Gi1/0/3. That's what I'm trying to look into now. Any help/advice would be appreciated! Thanks @dgjustice

@dgjustice
Copy link
Contributor

@qduk I'll submit my own PR with tests.

@qduk qduk requested a review from itdependsnetworks August 16, 2021 21:04
Copy link
Collaborator

@jeffkala jeffkala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple small ones.

@itdependsnetworks itdependsnetworks merged commit 3d7aae9 into networktocode:develop Aug 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants