MusterübereinstimmungHallo,
Ich versuche, einen Teil des Textes zu ersetzen und wäre für Hilfe mit dem Code dankbar.
Mir ist aufgefallen, dass das Ausführen von JSL mit der alten Spaltennamensyntax die Laufzeit verkürzt.
Ich möchte ein Skript erstellen, das meinen aktuellen JSL-Code übernimmt und ihn wie folgt ändert:
:Name( "col_name" )
Hierzu:
:"col_name"n
Ich habe versucht, Regex und Patt Match zu verwenden, aber ohne Erfolg.
Weiß jemand, wie das geht?
Automatisierung und SkriptingBetreff: MusterübereinstimmungVersuchen Sie, dies mit einem Suchskript zu tun?

Nach dem Ersetzen

:Name\( (.+?) \)
:\1n
Betreff: MusterübereinstimmungNein, ich versuche, genau dasselbe mit Regex- oder Patt-Match-Funktionen zu tun.
Betreff: MusterübereinstimmungNach einigen Tagen T/S habe ich festgestellt, dass es keine Möglichkeit gibt, dies in JSL zu tun, ohne die Kommentare im neu geschriebenen JSL-Code zu verlieren.
Also habe ich einen Code in Python geschrieben, der das stattdessen macht
# This code will replace code in .jsl files that match the Regex in parameters dictionary (pattern(1) and repl(2)) below.
# It is based on the simple def replace(source, dest) function, with pattern now being the source and the repl as the dest.
# The pattern in this code is to replace old JMP column naming syntax to the new JMP column naming syntax,
# e.g :Name( "variable@property" ) into :"variable@property"n
# for jsl code to run more efficiently in JMP17 and above
import os
import re
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
WORKING_DIR = filedialog.askdirectory()
# pattern(1)
pattern = r':\s*Name\s*\(\s*"([^"]+)"\s*\)'
# repl(2)
repl = r':"\1"n'
parameters = {pattern: repl}
def pat_replace(pat, rep):
for dname, dirs, files in os.walk(WORKING_DIR):
for fname in files:
if fname.endswith(".jsl"):
fpath = os.path.join(dname, fname)
with open(fpath, encoding="utf8") as f:
s = f.read()
s_out = re.sub(pat, rep, s)
with open(fpath, "w", encoding="utf8") as f:
f.write(s_out)
for i, (key, value) in enumerate(parameters.items()):
pat_replace(key, value)Betreff: MusterübereinstimmungWenn Sie ein Skript wie dieses haben
Names Default To Here(1);
// this is comment
:Name("col_name");
:Name("col_name2");
Sie können diese Zeichenfolgen vom Typ :Name("colname") definitiv ersetzen, ohne die Kommentare zu entfernen.
Script.jsl

Script2.jsl

Names Default To Here(1);
path_to_file = "$DOWNLOADS/Script.jsl";
path_to_new_file = "$DOWNLOADS/Script2.jsl";
str = Load Text File(path_to_file);
new_str = Regex(str, ":Name\( (.+?) \)", ":\1n", GLOBALREPLACE);
// Write(new_str);
Save Text File(path_to_new_file, new_str);
Betreff: Musterübereinstimmung...und das vollständige Skript könnte so aussehen
WORKING_DIR = Pick Directory( "Select a directory" );
pattern = "\[:\s*Name\s*\(\s*"([^"]+)"\s*\)]\";
rep = "\[:"\1"n]\";
pat_replace = Function( {pat, rep},
For Each( {f}, Files In Directory( WORKING_DIR, recursive(1) ),
If( Ends With( f, ".jsl" ),
f = WORKING_DIR || f;
text = Load Text File( f );
newtext = Regex( text, pat, rep, GLOBALREPLACE );
If( newtext != text,
Save Text File( f || ".new.jsl", newtext );
);
)
)
);
pat_replace( pattern, rep );
/*
the "\[...]\" escape in JSL is NOT a perfect replacement for the
python r-string, though it does work for this example. In particular,
if the regex contains ]\ it will not work out without more escaping
magic.
The file is renamed when saved so if something goes wrong...you could
save to another directory, etc.
Recursive(1) includes subdirs, change as needed.
*/