Rounding Up/Down

Hi,

Would it be possible to have a round up/down to a number of positions?

I have a percentage using the maths plugin but could do with making it a little more readable.

You can do this quickly in a python step

The python will be like this

def run(params={}):
    number_to_round = <insert your object input here>
    return({"number":round(number_to_round, 2)}) # Replace 2 with digits to round to

Then you can use ["python_step"].["number"] where ever you need it.

I don’t believe we have a nice way to do this without python, unfortunately. It’s something we can look into though. As we get more snazzy with our job output and reporting this is something we should do natively.

I tried this, the job failed and in the log was this

rapid7/Python 3 Script:2.0.2. Step name: run
Input: (below)

{}

Function: (below)

def run(params={}):

    number_to_round = 1.2048192771084338

    return({round(number_to_round, 2)}) 

**{1.2} is not of type 'object'**

{{[“Get Positive Percent”].[result]}} is a number

return({round(number_to_round, 2)})

The python step expects an object as a return. So it must be in the form of {"key":"value"} so, try this:

return({"number":"round(number_to_round, 2)"})

Thanks for replying

In the input

function:def run(params={}):

    number_to_round = 1.2048192771084338

return({"MaliciousPercentage":"round(number_to_round, 2)"})
input: Empty

In the log

rapid7/Python 3 Script:2.0.2. Step name: run
Input: (below)

{}

Function: (below)

def run(params={}):

    number_to_round = 1.2048192771084338

return({"MaliciousPercentage":"round(number_to_round, 2)"})

Could not run supplied script. Error: 'return' outside function (<string>, line 5)
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/python_3_script_rapid7_plugin-2.0.2-py3.7.egg/komand_python_3_script/actions/run/action.py", line 21, in run
    exec(func)
  File "<string>", line 5
SyntaxError: 'return' outside function

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/komand-1.0.1-py3.7.egg/komand/plugin.py", line 311, in handle_step
    output = self.start_step(input_message['body'], 'action', logger, log_stream, is_test, is_debug)
  File "/usr/local/lib/python3.7/site-packages/komand-1.0.1-py3.7.egg/komand/plugin.py", line 419, in start_step
    output = func(params)
  File "/usr/local/lib/python3.7/site-packages/python_3_script_rapid7_plugin-2.0.2-py3.7.egg/komand_python_3_script/actions/run/action.py", line 25, in run
    raise Exception('Could not run supplied script. Error: ' + str(e))
Exception: Could not run supplied script. Error: 'return' outside function (<string>, line 5)

Then I thought id maybe to do with tabs / indents, so moved the return under the calculation row…

rapid7/Python 3 Script:2.0.2. Step name: run
Input: (below)

{}

Function: (below)

def run(params={}):

    number_to_round = 1.2048192771084338
	return({"MaliciousPercentage":"round(number_to_round, 2)"})

Could not run supplied script. Error: inconsistent use of tabs and spaces in indentation (<string>, line 4)
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/python_3_script_rapid7_plugin-2.0.2-py3.7.egg/komand_python_3_script/actions/run/action.py", line 21, in run
    exec(func)
  File "<string>", line 4
    return({"MaliciousPercentage":"round(number_to_round, 2)"})
                                                              ^
TabError: inconsistent use of tabs and spaces in indentation

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/komand-1.0.1-py3.7.egg/komand/plugin.py", line 311, in handle_step
    output = self.start_step(input_message['body'], 'action', logger, log_stream, is_test, is_debug)
  File "/usr/local/lib/python3.7/site-packages/komand-1.0.1-py3.7.egg/komand/plugin.py", line 419, in start_step
    output = func(params)
  File "/usr/local/lib/python3.7/site-packages/python_3_script_rapid7_plugin-2.0.2-py3.7.egg/komand_python_3_script/actions/run/action.py", line 25, in run
    raise Exception('Could not run supplied script. Error: ' + str(e))
Exception: Could not run supplied script. Error: inconsistent use of tabs and spaces in indentation (<string>, line 4)

Hi @phil_pearce!
You are right. The first error is because you didn’t indent the return so it was not part of the function.
The second error is because “inconsistent use of tabs and spaces in indentation (, line 4)”.
Means: Use either tabs or spaces to indent, don’t mix them (in the whole script).
The validator is quite strict :slight_smile:

Hope this helps.
Philipp

1 Like

yeah, I get that, but Ive tried both ways. As there is 2 errors in the above example

I just tested this function in my workflow and it succeeded:

def run(params={}):
    number_to_round = 1.2048192771084338
    rounded_number = round(number_to_round, 2)
    return({"MaliciousPercentage": rounded_number})

I think it was a tabs/spaces issue. When I copied the function from the logs above and redid the spacing for each line, it executed properly.