Created
January 25, 2011 00:04
-
-
Save wojdyr/794253 to your computer and use it in GitHub Desktop.
Updates older fityk scripts to the version 0.9.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Updates older fityk scripts to the version 0.9.5. | |
# Should work with scripts for versions 0.8.6-0.9.4 | |
# Feedback is welcome ([email protected]). | |
usage = """\ | |
Usage: | |
fityk-script-conv-to-0.9.5.py old-script.fit [new-script.fit] | |
or | |
cat old-script.fit | fityk-script-conv-to-0.9.5.py >new-script.fit | |
""" | |
import re, sys | |
dash_to_underscore = """\ | |
on-plot-change | |
on-fit-iteration | |
exit-on-warning | |
data-default-sigma | |
pseudo-random-seed | |
formula-export-style | |
info-numeric-format | |
cut-function-level | |
can-cancel-guess | |
height-correction | |
width-correction | |
guess-at-center-pm | |
fitting-method | |
max-wssr-evaluations | |
refresh-period | |
variable-domain-percent | |
lm-lambda-start | |
lm-lambda-up-factor | |
lm-lambda-down-factor | |
lm-stop-rel-change | |
lm-max-lambda | |
nm-convergence | |
nm-move-all | |
nm-distribution | |
nm-move-factor | |
Levenberg-Marquardt | |
Nelder-Mead-simplex | |
Genetic-Algorithms | |
fit-history | |
first-line-header | |
last-line-header | |
""" | |
to_lower = """\ | |
Levenberg_Marquardt | |
Nelder_Mead_simplex | |
Genetic_Algorithms | |
""" | |
reg_subst = [ | |
# %f = guess ... -> guess %f = ... | |
(r"(%\w+\s*=\s*)(g|gu|gue|gues|guess)\s", r"\2 \1"), | |
# guess Gaussian [:] center=~30 -> guess Gaussian(center=~30) [:] | |
(r"((?:g|gu|gue|gues|guess)\s+(?:%\w+\s*=)?\s*[A-Z]\w+)(\s*(?:\[.*\])?\s*)((\s*\w+\s*=[^,;#\r\n]+,?)+)", | |
r"\1(\3)\2"), | |
# ... in @n, ... -> @n, ...: ... | |
(r"^(.*)\s+in\s*((\s*@[\d*]+(\s*,)?)+)(?=\s*(#.*)?$)", | |
lambda m: m.group(2).replace(", "," ").replace(","," ") + ": " + m.group(1)), | |
# set @n.title = ... -> @n: title = ... | |
(r"set\s*(@\d+)\.title", r"\1: title"), | |
# set info_numeric_format = %g -> set info_numeric_format = '%g' | |
(r"(set\s+info_numeric_format\s*=\s*)([^\s'#]+)", r"\1'\2'"), | |
# simplified option names | |
("info_numeric_format", "numeric_format"), | |
("data_default_sigma", "default_sigma"), | |
("cut_function_level", "function_cutoff"), | |
("variable_domain_percent", "domain_percent"), | |
# refresh-delay was used only in svn code | |
("refresh-delay", "refresh_period"), | |
("(autoplot\s*=\s*)never", r"\g<1>0"), | |
("(autoplot\s*=\s*)on_plot_change", r"\g<1>1, fit_replot=0"), | |
("(autoplot\s*=\s*)on_fit_iteration", r"fit_replot=1"), | |
("(verbosity\s*=\s*)quiet", r"\g<1>-1"), | |
("(verbosity\s*=\s*)normal", r"\g<1>0"), | |
("(verbosity\s*=\s*)verbose", r"\g<1>1"), | |
("(verbosity\s*=\s*)debug", r"\g<1>2"), | |
# removed options | |
("((s|se|set)\s+(can_cancel_guess|formula_export_style|guess_at_center_pm))", | |
r"#\1"), | |
# dump -> info state | |
(r"^\s*dump\b", "info state"), | |
# commands < file -> exec file | |
(r"(c|co|com|comm|comma|comman|command|commands)\s*<\s*", "exec "), | |
# commands ! shell-command -> exec ! shell-command | |
(r"(c|co|com|comm|comma|comman|command|commands)\s*!\s*", "exec ! "), | |
# commands > shell-command -> set logfile= | |
(r"(c|co|com|comm|comma|comman|command|commands)\s*>\s*", "set logfile="), | |
# commands+ > shell-command -> set log_full=1, logfile= | |
(r"(c|co|com|comm|comma|comman|command|commands)\+\s*>\s*", | |
"set log_full=1, logfile="), | |
# numarea(%f, 10, 30, 100) -> %f.numarea(10, 30, 100) | |
(r"(numarea|findx|extremum)\s*\(([^,]+),\s*", r"\2.\1("), | |
# info > 'file' -> info >> 'file' | |
(r"(\bi.*[^>]>)(\s*')", r"\1>\2"), | |
# F.param = $variable -> F[*].param = $variable | |
(r"\bF(\.[a-z][a-z0-9_]*\s*=)", r"F[*]\1"), | |
] | |
def main(): | |
if len(sys.argv) == 1: | |
input_script = sys.stdin | |
output_script = sys.stdout | |
elif len(sys.argv) == 2: | |
input_script = open(sys.argv[1]) | |
output_script = sys.stdout | |
elif len(sys.argv) == 3: | |
input_script = open(sys.argv[1]) | |
output_script = open(sys.argv[2], "w") | |
else: | |
print usage | |
sys.exit(1) | |
dashed = [name.strip() for name in dash_to_underscore.split("\n") if name] | |
upper = [name.strip() for name in to_lower.split("\n") if name] | |
res = [(re.compile(p), r) for p,r in reg_subst] | |
for line in input_script: | |
for d in dashed: | |
if d in line: | |
line = line.replace(d, d.replace("-", "_")) | |
for u in upper: | |
if u in line: | |
line = line.replace(u, u.lower()) | |
for p,r in res: | |
line = p.sub(r, line) | |
output_script.write(line); | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment