Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to return a value from a python script

I wrote a script in python that takes a few files, runs a few tests and counts the number of total_bugs while writing new files with information for each (bugs+more).

To take a couple files from current working directory:

myscript.py -i input_name1 input_name2

When that job is done, I'd like the script to 'return total_bugs' but I'm not sure on the best way to implement this.

Currently, the script prints stuff like:

[working directory] [files being opened] [completed work for file a + num_of_bugs_for_a] [completed work for file b + num_of_bugs_for_b] ... [work complete] 

A bit of help (notes/tips/code examples) could be helpful here.

Btw, this needs to work for windows and unix.

like image 623
ofer.sheffer Avatar asked Aug 14 '13 12:08

ofer.sheffer


People also ask

How do you return a value from a Python script?

A return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned.

Can a Python main return a value?

It is possible. You could, for instance, return a dictionary (or list, or tuple, or whatever) in utility.

How do you return a value from another function in Python?

Using a value returned from a function Instead in your first function, check_channel_number() , you can have it return user_channel_number . You can then call that function inside of delete_events() , like the following: def check_channel_number(): user_channel_number = ...


1 Answers

If you want your script to return values, just do return [1,2,3] from a function wrapping your code but then you'd have to import your script from another script to even have any use for that information:

Return values (from a wrapping-function)

(again, this would have to be run by a separate Python script and be imported in order to even do any good):

import ... def main():     # calculate stuff     return [1,2,3] 

Exit codes as indicators

(This is generally just good for when you want to indicate to a governor what went wrong or simply the number of bugs/rows counted or w/e. Normally 0 is a good exit and >=1 is a bad exit but you could inter-prate them in any way you want to get data out of it)

import sys # calculate and stuff sys.exit(100) 

And exit with a specific exit code depending on what you want that to tell your governor. I used exit codes when running script by a scheduling and monitoring environment to indicate what has happened.

(os._exit(100) also works, and is a bit more forceful)

Stdout as your relay

If not you'd have to use stdout to communicate with the outside world (like you've described). But that's generally a bad idea unless it's a parser executing your script and can catch whatever it is you're reporting to.

import sys # calculate stuff sys.stdout.write('Bugs: 5|Other: 10\n') sys.stdout.flush() sys.exit(0) 

Are you running your script in a controlled scheduling environment then exit codes are the best way to go.

Files as conveyors

There's also the option to simply write information to a file, and store the result there.

# calculate with open('finish.txt', 'wb') as fh:     fh.write(str(5)+'\n') 

And pick up the value/result from there. You could even do it in a CSV format for others to read simplistically.

Sockets as conveyors

If none of the above work, you can also use network sockets locally *(unix sockets is a great way on nix systems). These are a bit more intricate and deserve their own post/answer. But editing to add it here as it's a good option to communicate between processes. Especially if they should run multiple tasks and return values.

like image 98
Torxed Avatar answered Oct 05 '22 15:10

Torxed