Reputation: 8297
def detect_long_method(directory):
path, dirs, files = next(os.walk(directory))
output = lm.output_long_methods(directory)
# print (output.stdout)
split_lines = output.splitlines()
output_lines = [output.decode('utf-8') for output in split_lines if len(output) > 3 and\
('R0915' in output.decode('utf-8') or 'R0913' in output.decode('utf-8') or 'R0912' in output.decode('utf-8') or \
'R0904' in output.decode('utf-8') or 'R0902' in output.decode('utf-8'))]
return output_lines
What this function does is, it runs a command line command and gets the stdout and assigns it to the variable output
. It splits into lines using the .splitlines()
function.
In the output_lines
, it checks whether each line (they are in bytes) contains the word R0915..etc.
and if the length of the line is over 3, then finally converts them into a string.
However, this is pretty slow and I feel like there is a way to write this more neatly and more efficiently.
Any help?
Upvotes: 1
Views: 99
Reputation: 4787
Here is a more efficient script:
import os
import re
def detect_long_method(directory):
path, dirs, files = next(os.walk(directory))
output = lm.output_long_methods(directory).decode('utf-8')
# print (output.stdout)
split_lines = output.splitlines()
output_lines = [output for output in split_lines if len(output) > 3 and\
re.search("(R0915|R0913|R0912|R0904|R0902)", output) is not None]
return output_lines
As juanpa.arrivillaga suggested, using regular expressions, and calling decode('utf-8')
once should lead to faster execution time.
Upvotes: 2