Salvataggio di un nuovo script per preservare le variabili per dopoCiao,
Ho creato uno script che esegue l'analisi dei dati ma richiede vari input dell'utente per la configurazione. Ci sono casi in cui desidero eseguire un'analisi simile in un secondo momento, ma non è necessario reinserire tutte le impostazioni. Lo script principale può già leggere le variabili esistenti e compilare automaticamente quei campi, quindi se ho un modo per salvare e importare quei valori predefiniti, sarò a posto. Voglio essere in grado di avere più configurazioni diverse e non aver bisogno di modificare lo script stesso per crearne uno nuovo.
Un'idea era quella di salvare una tabella con una colonna di nomi di variabili e una colonna di relativi valori, ma ciò non funzionava bene con diversi tipi di variabili (numeri, stringhe, elenchi).
Un'idea migliore è salvare altri file .jsl che contengono le impostazioni predefinite per diversi casi d'uso in uno spazio dei nomi condiviso, quindi eseguire quello pertinente prima di eseguire lo script principale. Mi piacerebbe essere in grado di creare il file di configurazione dal mio script, anziché crearli manualmente.
Ho trovato questo altro post sul salvataggio di nuovi file jsl. https://community.jmp.com/t5/Discussions/how-do-you-create-a-new-script-file-from-jsl/td-p/34740
Sembra che dovrei essere in grado di impostare una citazione JSL, quindi sostituire i valori correnti delle variabili in quella citazione, ma NameExpr non funziona per questo. Le stringhe vengono sostituite senza virgolette, gli elenchi appaiono semplicemente come Elenco e i numeri vengono ignorati. C'è un leggero cambiamento che potrebbe far funzionare tutto questo o un modo migliore per raggiungere lo stesso obiettivo?
Grazie!
names default to here (1);
//namespace for the shared variables
If( !Namespace Exists( "TestNS" ), New Namespace( "TestNS" ) );
//the variables I want to save for later
TestNS:a = 5;
TestNS:b = "text";
TestNS:c = {1,2,3,4};
TestNS:d = {"a","b","c","d"};
//set up script to save as JSL
script = JSL Quote(
TestNS:a = a_dv;
TestNS:b = b_dv;
TestNS:c = c_dv;
TestNS:d = d_dv;
);
//substitute the dummy values for the current values... this isn't working
substitute Into (script, expr(a_dv), nameexpr (TestNS:a));
substitute Into (script, expr(b_dv), nameexpr (TestNS:b));
substitute Into (script, expr(c_dv), nameexpr (TestNS:c));
substitute Into (script, expr(d_dv), nameexpr (TestNS:d));
show(script);
//save script for later use
Save Text File( "$downloads\MyScript.jsl", script );Automazione e scriptingfinestreRi: Salvataggio di un nuovo script per preservare le variabili per dopoNon sono sicuro di quanto sia universale, ma funziona sul tuo semplice script
names default to here (1);
//namespace for the shared variables
If( !Namespace Exists( "TestNS" ), New Namespace( "TestNS" ) );
//the variables I want to save for later
TestNS:a = 5;
TestNS:b = "text";
TestNS:c = {1,2,3,4};
TestNS:d = {"a","b","c","d"};
//set up script to save as JSL
script = expr(
TestNS:a = a_dv;
TestNS:b = b_dv;
TestNS:c = c_dv;
TestNS:d = d_dv;
);
//substitute the dummy values for the current values... this isn't working
substitute Into (script, expr(a_dv), nameexpr (TestNS:a));
substitute Into (script, expr(b_dv), nameexpr (TestNS:b));
substitute Into (script, expr(c_dv), nameexpr (TestNS:c));
substitute Into (script, expr(d_dv), nameexpr (TestNS:d));
// Move the arguments in the script to a character string
theScript = "";
i=1;
While( char(arg(script,i)) !="Empty()",
theScript = theScript || char(arg(script,i)) || ";";
i++;
);
show(script);
//save script for later use
Save Text File( "$downloads\MyScript.jsl", theScript );Ri: Salvataggio di un nuovo script per preservare le variabili per dopoHai forse preso in considerazione la possibilità di memorizzare tali valori in un array associativo? Molto probabilmente è più facile da gestire e può essere salvato, ad esempio, come JSON, se necessario.
Names Default To Here(1);
//namespace for the shared variables
If(!Namespace Exists("TestNS"),
New Namespace(
"TestNS"
)
);
//the variables I want to save for later
TestNS:settings = Associative Array();
TestNS:settings["a"] = 5;
TestNS:settings["b"] = "text";
TestNS:settings["c"] = [1 2 3 4];
TestNS:settings["d"] = {"a", "b", "c", "d"};
TestNS:settings["e"] = ["key" => 1];
/*
TestNS:settings["a"] = cur_settings["a"];
TestNS:settings["b"] = cur_settings["b"];
TestNS:settings["c"] = cur_settings["c"];
TestNS:settings["d"] = cur_settings["d"];
TestNS:settings["e"] = cur_settings["e"];
*/
myfilepath = Save Text File("$DOWNLOADS/myscript.jsl", As JSON Expr(TestNS:settings));
conf = Load Text File(myfilepath);
loaded_settings = Parse JSON(conf);
Potresti quindi creare alcune funzioni come save_settings, load_settings, ... se necessario.
Ri: Salvataggio di un nuovo script per preservare le variabili per dopoGrazie sia a Jarmo che a Jim, entrambe le soluzioni funzionano. Inizialmente ho utilizzato la soluzione di Jim, che era in grado di integrarsi direttamente con ciò che avevo inizialmente. Dopo qualche revisione, ho adattato l'idea di Jarmo che in effetti era più ordinata da gestire. Ecco cosa ho ottenuto. Con questa configurazione non avevo bisogno dello spazio dei nomi, ma funzionerebbe comunque con gli spazi dei nomi.
names default to here (1);
//initialize variables
var1 = {"a", "b", "c"};
var2 = 100;
var3 = [1, 2, 3, 4];
//initialize variables used for functions
VarsToExport = {"var1", "var2", "var3"};
SettingsAA = Associative Array();
loaded_settingsAA = .;
SelfPath = Get Default Directory();
//expr to get each variable in the VarsToExport list and input the value into an AA, export as JSON
ExportVarsExpr = expr(
for each({varname}, VarsToExport,
//show(varname);
SettingsAA[varname] = eval(as name(varname));
//show(SettingsAA);
);
VarsSavePath = Pick File(
"Save a file containing current script settings",
SelfPath,
{"JMP Script Files|jsl", "All Files|*"},
1,
1,
"Layer_Settings.jsl"
);
Save Text File(VarsSavePath, As JSON Expr(settingsAA));
);
//expr open a jsl file, parse JSON into an AA and extract saved values
ImportVarsExpr = expr(
VarsImportPath = Pick File(
"Select jsl file that contains the saved parameters",
SelfPath,
{"JMP Script Files|jsl", "All Files|*"},
1,
0,
""
);
loaded_settingsAA = Parse JSON(Load Text File(VarsImportPath));
//for each element of AA, get the name of the element and set a variable with that name to the AA value
for each({varname}, loaded_settingsAA,
eval(parse(Evalinsert("\[ "^varname^"n = loaded_settingsAA << get value (varname) ]\")));
//show(varname);
//show(loaded_settingsAA << get value (varname));
);
);
//show original variables
Write("\!N", "Original:");
show(var1, var2, var2);
//export variables
eval(ExportVarsExpr);
//clear the variables
var1 = .;
var2 = .;
var3 = .;
Write("\!N", "\!N", "Variables cleared:");
show(var1, var2, var2);
//import variables
eval(ImportVarsExpr);
//show that the variables have been imported
Write("\!N", "\!N", "Post loading:");
show(var1, var2, var2);