I am currently working on the development of a keyword that allows to separate the contents of a text file into 2 parts and create 2 new files containing each of these 2 parts. The file format is similar to a CSV file, except that the separator is a '|' (pipe). The file size is relatively large. We are talking here about 125 000 lines containing 10+ columns. I tried to write the keyword directly in Robot Framework as follows:
Split Files
[Arguments] ${filePath} ${nbOfColumns} ${separator}=|
OperatingSystem.File Should Exist ${filePath} msg=The file ${filePath} doesn't exist!
${fileContent} = OperatingSystem.Get File ${filePath} encoding_errors=ignore
@{lines} = String.Split To Lines ${fileContent}
: FOR ${line} IN @{lines}
\ ${rest} ${last} = CsvDiffLibrary.Split String From Right ${line} 2
\ OperatingSystem.Append To File ${filePath}_old ${rest}${\n}
\ OperatingSystem.Append To File ${filePath}_new ${last}${\n}
The problem is that the execution of this keyword takes about 5 seconds for a file of 1000 lines ... and 10 seconds for a file of 2000 lines ... which means that it would take around 10 minutes for the entire file. It is not at all powerful and far from acceptable. (For information, for the treatment of the whole file, I forced the test to stop after 5 minutes of execution)
So I decided to write the keyword in Python, but also reusing the same Python libraries from Robot Framework:
def split_file(self, path, nb_of_columns):
OperatingSystem().file_should_exist(path)
file_content = OperatingSystem().get_file(path, encoding_errors='ignore')
lines = String().split_to_lines(file_content)
for line in lines:
rest, last = self.split_string_from_right(line, nb_of_columns)
OperatingSystem().append_to_file(path + "_old", rest + "\n")
OperatingSystem().append_to_file(path + "_new", last + "\n")
You will notice that it is exactly the same thing, but in this way, the processing of 1000 lines is instantaneous and the whole file (123K lines) is processed in 20 seconds.
So my question is simple... why? Why so much difference between the keyword written in Robot Framework and the one written in Python? Because to my knowledge, Robot Framework is fully written in Python. There should not be so much overhead.
Thanks,