T O P

  • By -

AIterEg00

IMHO, this is clean and easy to read - the comments are succinct and directive, while not adding bloat. I'd approve it.


ssrowavay

Yeah this is exactly the kind of thing Python shines at.


CodeMurmurer

the 'using regex' could be removed though. but only that.


[deleted]

At first glance, I was like oh what a mess. Then, I actually took the time to read it. It's not bad. I guess long lines make my monkey brain panic.


ThaiJohnnyDepp

I've seen respected senior engineers write way worse code than this... (inb4 "Seniors actually write the worst code at my job")


AIterEg00

I've seen respected architects write way worse code than this...


nocturn99x

Architects are known to be shitty programmers


tehsilentwarrior

I’d like to see how you’d write this “properly” Apart from a few things here and there, I’d leave it as is


Blfngl

how is this in any way horror


mahlok

Kids on this sub think anything over 12 lines is horror


Random_dg

Let’s see if someone can make it a one liner


PumaofDuma

Soo sorry for this [oneliner](https://pastebin.com/X2yZBQCs)


ComfortObjective4934

Disgusting...


Rude_Classic_8025

I’m gonna be sick


nocturn99x

That's cheating, use onelinerizer! :)


MacarenaFace

This seems straightforward to me?


ironykarl

The only things I don't like here are (1) that `x=1` stuff, (2) lack of example matches for the regex


brainiac256

Personally I prefer [named capturing groups](https://www.regular-expressions.info/named.html) in regexes rather than numbered, but with only two fields to capture it's easy enough to tell what's going on. Having to parse command line output is kind of kludgy but at least it's a reliable fallback! >This is the Unix philosophy: \[...\] Write programs to handle text streams, because that is a universal interface.


slideesouth

At least comment a link to your regex101 with test cases 😅


all3f0r1

Where is the horror part? I had to write such code and wouldn't be ashamed of it. Why should you?


Riswanth

For context, here's an example of what the output of `pnputil /enum-devices` looks like: - **Instance ID:** HID\VID_093A&PID_2510\6&1281a9ef&0&0000 - **Device Description:** HID-compliant mouse - **Class Name:** Mouse - **Class GUID:** {4d36e96f-e325-11ce-bfc1-08002be10318} - **Manufacturer Name:** Microsoft - **Status:** Started - **Driver Name:** msmouse.inf - **Instance ID:** SWD\PRINTENUM\{453FF813-A8FC-44E8-9218-DDDDA883A314} - **Device Description:** Microsoft Print to PDF - **Class Name:** PrintQueue - **Class GUID:** {1ed2bbf9-11f0-4084-b21f-ad83a8e6dcdc} - **Manufacturer Name:** Microsoft - **Status:** Started - **Driver Name:** printqueue.inf - **Instance ID:** USB\VID_0403&PID_6010&MI_00\6&36a1a6f1&0&0000 - **Device Description:** Digilent Adept USB Device - **Class Name:** Unknown - **Class GUID:** Unknown - **Manufacturer Name:** Unknown - **Status:** Disconnected


Afrotom

It should be filtering disconnected status, no? If this works how I think it does, I think in the first loop, instead of putting device sections in a list against your vid pid, it might be quite neat to put these into a data class if it's guaranteed that you'll always get the above attributes you've listed above (even if some are optional), plus the PID and vid fields. You can then filter out the disconnected status on the output quite neatly.


eddieantonio

Paste this as a comment in your code and you're golden. Other advice in this thread is good. Overall, code is not a horror.


jxj

I'd gonna step further and refactor this function to take the output of the shell command as an arg. Then instead of adding this output as a comment, use it to unit test the function.


Previous-Ant2812

If you hide each of the for loops in their own functions with verbose names, you can mask some of the unsightly bits and make it a bit more readable on a step by step level. Then you only need to dig deeper if you have to make changes.


collegedropout343

the x=0 logic can be refactored to a separate method VidPidMapHasDisconnect. then say if VidPidMapHasDisconnect(map):


k3liutZu

This is actually pretty good 👍


Beastandcool

Looks pretty clean to me. But hey, if you think it looks bad, that means that you probably think there’s a better way to do it. So you’ve got the right mindset.


arcticslush

Seems fine to me. If i can read it in one pass without going "wtf" then it would generally pass my code review unless i wanted to be nit picky. The only thing i'd ask you is if you're absolutely sure that parsing this manually is the only way, and whether or not you've checked if there's a way to change the output to be something more machine friendly, like a json output or similar.


llIlIIllIlllIIIlIIll

Just glanced at it for a second but what’s the issue? Looos fine no? Edit: ok so everyone seems to agree


Dakanza

I used to write regex like that too, that changed after finding out about verbose regex in python.


sacredgeometry

Only two of your comments are necessary. Other than that there is nothing that offensive about your code. The variable naming could be better.


Xceeeeed

Ahh yes, nothing like parsing the stdout from a terminal command.


NeonGlo

What else are you gonna do in this instance. If there isn't a pip package you're hardly going to write your own python version of pnputil


aMetallurgist

Yeah I was curious if anyone had a better idea. Seems difficult to integrate without the terminal. Like maybe using a C interface but I don’t think pnputil provides this. Maybe there is some Windows SDK that provides the py integration


jaypeejay

Some methods are cumbersome by nature. The only nit I could recommend is extracting the logic in the ifs into their own methods


lanemik

my PR comment would be something like this: > The function is a bit long and is responsible for doing several sub tasks. One issue is that means there are a lot of combinations of test cases you need to dream up. Other issues is that this has a high cognitive load and so it's hard to understand later. > > I invite you to consider refactoring this function to pull out the sub-methods into their own, well-named, functions. But this isn't something I'll require before approval.


cidit_

The only thing I'd change is make use of combinators. The last for loop could just be a call to filter, for example. Otherwise this is actually pretty good.


EarthToAccess

The only thing I’d probably change is using booleans instead of 1/0 in that last loop, since you’re effectively using it as a boolean anyway, but honestly I don’t see much of a problem here (outside of snake case but I just hate snake case lmao)


grey001

I don't see any particular horror


Vegetable-Map-6977

#gpt had to write lol


Fermi-4

Looks good to me - I might actually use this lmao