You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The python_filter function which converts {:, :} and {::} into newlines and indentation is slower than it has to be. It takes a significant amount of time when the operations used and the evaluation is fast. Replacing the per-char for loop with calls to builtins and a per-line for loop is faster.
See my changes below:
--- a/PonyGE2/src/utilities/representation/python_filter.py+++ b/PonyGE2/src/utilities/representation/python_filter.py@@ -5,20 +5,21 @@ def python_filter(txt):
it's not possible to specify indentation correctly in a BNF
grammar without this type of scheme."""
+ # Split input into lines+ split_code = (txt+ .replace("{::}", "\n")+ .replace("{:", "{:\n")+ .replace(":}", ":}\n")+ .split("\n"))++ new_line_list = []
indent_level = 0
- tmp = txt[:]- i = 0- while i < len(tmp):- tok = tmp[i:i + 2]- if tok == "{:":- indent_level += 1- elif tok == ":}":- indent_level -= 1- tabstr = "\n" + " " * indent_level- if tok == "{:" or tok == ":}":- tmp = tmp.replace(tok, tabstr, 1)- i += 1- # Strip superfluous blank lines.- txt = "\n".join([line for line in tmp.split("\n")- if line.strip() != ""])- return txt+ for line in split_code:+ if line != ":}" and line != "":+ clean_line = line.replace("{:", "").replace(":}", "")+ new_line_list.append(" " * indent_level + clean_line)+ indent_level += line.count("{:")+ indent_level -= line.count(":}")++ new_code = "\n".join(new_line_list)+ return new_code
The text was updated successfully, but these errors were encountered:
MihaiBabiac
changed the title
Post-processing python is slow
Post-processing python phenotypes is slow
May 4, 2022
The
python_filter
function which converts{:
,:}
and{::}
into newlines and indentation is slower than it has to be. It takes a significant amount of time when the operations used and the evaluation is fast. Replacing the per-char for loop with calls to builtins and a per-line for loop is faster.See my changes below:
The text was updated successfully, but these errors were encountered: