Re: Changing default options of functions in a package when loading it
- To: mathgroup at smc.vnet.net
- Subject: [mg117426] Re: Changing default options of functions in a package when loading it
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Fri, 18 Mar 2011 06:00:08 -0500 (EST)
Hi Heiko,
one possibility would be to create another package where you store the
parameters,
and which has getter and setter methods for them (similar to Java beans in
Java), and import such
package privately into your package where your options must be set. Here is
a minimal
example:
(* Package with parameters *)
BeginPackage["MyParameters`"];
setUserName::usage = "setUserName[name_] is a setter for user name";
getUserName::usage = "getUserName[] is a getter for user name";
Begin["`Private`"];
userName = "someUserName";
setUserName[uname_] := userName = uname;
getUserName[] := userName;
End[];
EndPackage[];
(* Main package *)
BeginPackage["MyPackage`"];
myfun1;
myfun2;
UserName;
Begin["`Private`"];
Needs["MyParameters`"];
Options[myfun1] = {UserName :> getUserName[]};
Options[myfun2] = {UserName :> getUserName[]};
myfun1[opts : OptionsPattern[]] :=
Print["The user name is ", OptionValue[UserName]];
myfun2[opts : OptionsPattern[]] :=
Print["The user name length is ", Length@OptionValue[UserName]];
End[];
EndPackage[];
You can execute this code also in the front-end. Note that I used delayed
rules in options. Now,
myfun1[]
The user name is someUserName
setUserName["anotherUserName"];
myfun1[]
The user name is anotherUserName
So, by changing the parameter with a setter method, you control the option
settings at run-time, even
perhaps after the package has been loaded. As for the parameter package, it
can be generated programmatically. It does not have to necessarily be a file
with hard-coded parameters, all that matters
is that its context is on the $ContextPath and in $Packages - then Needs
will not attempt to load it
from the file but will simply put it on the $ContextPath when called. And in
any case, by using delayed rules
in option definitions and exposing setter methods for the parameters, you
can change them at any time
and the default values for options will be automatically triggering these
changes. This should give you
enough flexibility, I guess.
Regards,
Leonid
On Thu, Mar 17, 2011 at 2:31 PM, heiko <heiko.damerau.news at web.de> wrote:
> Hello,
>
> For a package containing functions with options, I'm looking for a
> clever technique to set the default values of these options when
> loading the package. In fact my package contains several functions
> having an option to define a user name. Is there any possibility to
> define a parameter like user name when loading the package such that
> it is then automatically taken as default value for the options of the
> functions inside the package? Is there any way to specify further
> parameters when loading a package in Mathematica (like options are
> optional parameters to functions), i.e. options of a package?
>
> Thanks in advance for any idea.
>
> Best regards,
> Heiko
>
>