• What does this operator -- means? [Python]
    7 replies, posted
Hello. I have a question regarding Python codes. But first, let me say I suck at this, so please bear with me. I'm using this code in a terminal to execute a python script. python ncs_realtime_objecdetection.py --graph graph --display 1 This code uses a python script, and a file named 'graph' which is in the same directory. But, what does the -- means? -- What do we call it? What does it do exactly? Can we subsitute it with something else? Can we break down the code into its elementary form? I'm trying to execute the code from another python script, using subprocess module. But it just wont work. I think I need to break down the code into its elementary form first. Any help is appreciated. Thank you in advance.
Most likely it's using the argpase package. I tackled this yesturday, by importing the script directly into my own. Alternatively, you could call the fuction with an array of strings to fake an argv. Here's exactly what I'm using https://github.com/panzi/u4pak/blob/master/u4pak.py#L1473 Directly though, I wanted to list the files from a pak, so the argument was list <filename> This input is filtered in on line 1570 if args.command == 'list': with open(args.archive,"rb") as stream: pak = read_index(stream,args.check_integrity) pak.print_list(args.details,args.human,delim,args.sort_key_func,sys.stdout) And that's all I needed technically (I didn't need to print the list since this just prints to stdout) So, fucking up this code, I was just able to do this import u4pak for file in glob.glob(dst + "\\Athena\\Content\\Paks\\*.pak"):     with open(file, 'rb') as stream:         pak = u4pak.read_index(stream, False)         uassets = []         for uasset in pak.records:             uassets.append(uasset.filename) # ...etc
Thats sounds promising, but unfortunately I didnt understand how to do it entirely. Could you provide a pointer on how to modify this code to use the solution you described in your example? This code is ran in a python script: from subprocess import call call(["sudo", "python", "ncs_realtime_objecdetection.py", "--graph", "graph", "--display", "1"])
I made a simple modification to the original to provide a bit more support for having it as a package then a subprocess. It might help to thread it but it's not something I'm giving much effort on. by importing this script (having it in your pythonpath or otherwise). I've thrown parsed arguments into the new main function, which is just a dict ncs_realtime_objectdetection.main({'confidence': 0.5, 'display': 1, 'graph': 'graph'}) https://gist.github.com/Scrxtchy/8098315378154e4e843eabe0b01f3786
Thanks a million for the explanation, and your effort to find the original script. I see, so we just treat the script as a library instead. I'm not familiar with this method, so your explanation really clears up things. This looks doable. I will try this method, if the NCS really proved to be unable to be restarted intra-scriptly . Currently I'm modifying the NCS script as a last try to restart the NCS intra-script. Thank you again
Pretty much, using if __name__ == '__main__': Allows us to run code when it's, simply put, the main script (generally run from the command line). Whereas, the __name__ would be the name of the script/module that imported it. There's a stackoverflow post about it here: https://stackoverflow.com/questions/419163/what-does-if-name-main-do So, this means that we can put the arg parser into the __main__ check, and hand these into the main function. If this were to be improved, I would move the main() to not use use an args object, instead proper arguments. Too lazy tbh
Another question: What would happen if I put the if __name__ == '__main__': inside a while loop, if the NCS script is already running? Will the call to the NCS script fail if its already running? Or, Will the NCS script get restarted each time its called? What I'm trying to do: Because normally, the NCS script will crash at some point due to hardware issues. Currently i remdied it somewhat by restarting USB hubs, but after a few hours, even that will fail to restart the NCS script. The script will only work again if its restarted. So, I planned to put that line of code you mentioned in a python script, in a while loop that will constantly tries to start the NCS script. It would be great if the call to the NCS script will fail if its already running.
should be fine. Since it's not indented, the code is run any time it's imported So, benefit of the doubt, if you're only running main() every time it crashes, you won't hit that block at all after execution Pretty old script, so I don't advise doing exactly this ever, but I use something like this to handle passwords to my router https://files.facepunch.com/forum/upload/1755/e4df1307-117b-4a4b-a690-ba96eb88165a/image.png Since setPassword() is not inside a function, practically just on the script, and outside a if __name__ == "__main__":, it will run at any time the package is imported. So in this example, this enforces me to type the password of my router at runtime
Sorry, you need to Log In to post a reply to this thread.