Prev: Packages designed for use in player pro
Next: Problems compiling addrow with gcc-mingw (windows).
From: Nate Dudenhoeffer on 27 Jul 2010 07:53 Is there a way to SetAttributes for all of the functions in a package? I would like them all to be Locked and ReadProtected. Thanks, Nate
From: David Reiss on 28 Jul 2010 02:54 This would be quite simple if SetAttrubutes accepted string arguments. If it did then you could simply execute SetAttributes[#,{Locked, ReadProtected}]&[Names["mycontext`*]] where mycontext is your package context. Howe er SetAttributes does not work this way (but it should...). So one has to work around this. I had to do this years ago when creating a password protection scheme for http://scientificarts.com/worklif= e .. Here is the code from within that package: UnevaluatedNamesList[s_String] := ToExpression["Unevaluated[" <> # <> "]"] & /@ Names[s]; UnevaluatedNamesList[s : {_String ..}] := ToExpression["Unevaluated[" <> # <> "]"] & /@ s; SetAttributesString[s_String, attribute_] := Scan[SetAttributes[#, attribute] &, UnevaluatedNamesList[s]]; SetAttributesString[s : {_String ..}, attribute_] := Scan[SetAttributes[#, attribute] &, UnevaluatedNamesList[s]]; With this you can then execute something like: SetAttributesString["mycontext`*", ReadProtected] A word of warning though. Remember that if you set the Locked attribute for a function that has options then SetOptions will no longer work with that function. Also, if you have http://scientificarts.com/worklife then you should name your function something other than SetAttributesString since it is defined in that package and would cause shadowing conflicts and other bad things. Hope this helps: --David http://scientificarts.com/worklife On Jul 27, 7:53 am, Nate Dudenhoeffer <ndudenhoef...(a)gmail.com> wrote: > Is there a way to SetAttributes for all of the functions in a package? = I > would like them all to be Locked and ReadProtected. > > Thanks, > Nate
From: Albert Retey on 28 Jul 2010 02:55 Hi, > Is there a way to SetAttributes for all of the functions in a package? I > would like them all to be Locked and ReadProtected. you can get a list of all symbol names in your package with: Names["MyContext`*"] using SetAttributes on that list should work alright (I haven't tested the following but done things like this before): SetAttributes[ToExpression[Names["MyContext`*"]],{ReadProtected,Locked}] if you have symbols that have OwnValues defined in your package, you might need to delete those from the list of all symbols or need to be more careful when creating symbols from the symbol names. You also might want to protect the private functions of your package, if there are any... hth, albert
From: Carl K. Woll on 29 Jul 2010 06:42 On 7/28/2010 1:55 AM, Albert Retey wrote: > Hi, > > >> Is there a way to SetAttributes for all of the functions in a package? I >> would like them all to be Locked and ReadProtected. >> > you can get a list of all symbol names in your package with: > > Names["MyContext`*"] > > using SetAttributes on that list should work alright (I haven't tested > the following but done things like this before): > > SetAttributes[ToExpression[Names["MyContext`*"]],{ReadProtected,Locked}] > > if you have symbols that have OwnValues defined in your package, you > might need to delete those from the list of all symbols or need to be > more careful when creating symbols from the symbol names. You also might > want to protect the private functions of your package, if there are any... > > hth, > > albert > An alternative is something like: ToExpression[Names["MyContext`*"], InputForm, Function[x, SetAttributes[x, {ReadProtected, Locked}], HoldAll]] so that OwnValues aren't a problem. Carl Woll Wolfram Research
From: Bill Rowe on 29 Jul 2010 06:43 On 7/28/10 at 2:54 AM, dbreiss(a)gmail.com (David Reiss) wrote: >This would be quite simple if SetAttrubutes accepted string >arguments. If it did then you could simply execute >SetAttributes[#,{Locked, ReadProtected}]&[Names["mycontext`*]] >where mycontext is your package context. Howe er SetAttributes does >not work this way (but it should...). >So one has to work around this. I had to do this years ago when >creating a password protection scheme for >http://scientificarts.com/worklife You didn't say what your work around was. I note the following In[1]:= BeginPackage["test`"]; f[x_] := 2 x g[x_] := 1/x EndPackage[]; In[5]:= SetAttributes[#, {Locked, ReadProtected}] & /@ Names["test`*"]; In[6]:= Attributes[g] Out[6]= {Locked,ReadProtected} In[7]:= Attributes[f] Out[7]= {Locked,ReadProtected} Given this does what you seem to want and requires about the same number of keystrokes to enter, if you consider this a "work around" I contend it doesn't involve any significant effort. And given this does the job, it isn't clear to me any thing needs to be changed.
|
Next
|
Last
Pages: 1 2 Prev: Packages designed for use in player pro Next: Problems compiling addrow with gcc-mingw (windows). |