cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
rcookie
Level III

How to include specific functions instead of whole script

Hello,

 

Is there a way to import only specific functions from a JSL file?

For example in python you can write: "from pandas import read_csv" to import only the read_csv function. Currently in JMP I always import a whole JSL file with many functions defined in it, and I think it would be better to import just the functions I need for the sake of clarity (and maybe performance).

 

Thanks

Richard

1 ACCEPTED SOLUTION

Accepted Solutions
ih
Super User (Alumni) ih
Super User (Alumni)

Re: How to include specific functions instead of whole script

Sorry my mistake, a name expr( function is needed when copying the function.  Here is a revised example:

 

included.jsl:

f = function({x,y}, x + y);

Script to load function:

names default to here(1);

//load the new file in a local here namespace
Eval( Eval Expr( local here(
	include("included.jsl");
	
	//Reference the original script's here namespace in this new here namespace
	//Eval, Eval Expr, and Expr cause the namespace("here") to be evaluated
	//outside of this 'local here' context.
	//from the other.
	ns = Expr( namespace("here") );
	
	//copy and functions you want to keep
	ns:f = Name expr( here:f );
) ) );

//Now functions are in here namespace
f(1,2);

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: How to include specific functions instead of whole script

I am not aware of any way to include only a subset from an include file.
Jim
ih
Super User (Alumni) ih
Super User (Alumni)

Re: How to include specific functions instead of whole script

I don't know of anything as straight forward as python offers either, but here are two options that get the job done:

 

Consider a file called included.jsl:

a = function({}, 1);
b = function({}, 2);

To load the function directly in the context of your script:

names default to here(1);

//load the new file in a local here namespace
Eval( Eval Expr( local here(
	include("included.jsl");
	
	//Reference the original script's here namespace in this local here namespace
	//Eval, Eval Expr, and Expr cause the namespace("here") to be evaluated
	//outside of this 'local here' context.
	ns = Expr( namespace("here") );
	
	//copy and functions you want to keep
	ns:a = here:a;
) ) );

//Now functions are in this namespace
a();

 

Or, to keep the function in a separate namespace:

names default to here(1);

//create namespace to hold functions you want to keep from included file
ns = new namespace();

//load the new file in a local here namespace
Eval( Eval Expr( local here(
	include("included.jsl");
	
	//Reference the ns namespace in this new here namespace
	ns = Expr( ns );
	
	//pick and choose any functions you want to keep
	ns:a = here:a
	
) ) );

//Now either call the functions from the ns namespace
ns:a();

//or copy them to the here namespace
a = ns:a;
a();

 

rcookie
Level III

Re: How to include specific functions instead of whole script

Thanks for your help, this definitely works for functions without any argument.

Unfortunately I run into an error when trying to import a function with two arguments, the error states I don't give any argument when the function requires 2 of them (which is true). Here is the sample code:

 

names default to here(1);
MyRejet = 1;
Lot_nom = "G123456";

//load the new file in a local here namespace
Eval( Eval Expr( local here(
	Include("include.jsl");;
	
	//Reference the original script's here namespace in this local here namespace
	//Eval, Eval Expr, and Expr cause the namespace("here") to be evaluated
	//outside of this 'local here' context.
	ns = Expr( namespace("here") );
	
	print(ns);
	
	//copy and functions you want to keep
	ns:DataExtraction = here:DataExtraction;
) ) );

//Now functions are in this namespace

DataExtraction(Lot_nom, MyRejet);

Do you know where the error is? I tried specifying arguments  when copying the functions but it does not seem to solve the issue.

 

Thanks

Richard

ih
Super User (Alumni) ih
Super User (Alumni)

Re: How to include specific functions instead of whole script

Sorry my mistake, a name expr( function is needed when copying the function.  Here is a revised example:

 

included.jsl:

f = function({x,y}, x + y);

Script to load function:

names default to here(1);

//load the new file in a local here namespace
Eval( Eval Expr( local here(
	include("included.jsl");
	
	//Reference the original script's here namespace in this new here namespace
	//Eval, Eval Expr, and Expr cause the namespace("here") to be evaluated
	//outside of this 'local here' context.
	//from the other.
	ns = Expr( namespace("here") );
	
	//copy and functions you want to keep
	ns:f = Name expr( here:f );
) ) );

//Now functions are in here namespace
f(1,2);
rcookie
Level III

Re: How to include specific functions instead of whole script

Indeed it works, thanks a lot!