# Transfer of symbols between contexts

As many others (I guess) I'm struggling with the handling of contexts within Mathematica. I'm working on a new package definition with the usual syntaxt specified by the Mathematica documentation:

BeginPackage["example"];

<Entry Points>

Begin["Private"]

<Definitions>

End[];

EndPackage[];


Now I want to use symbols generated in another Notebook and saved with DumpSave for efficiency. The code in this other Notebook is thus

DumpSave[<some mx file>,{symbol1,symbol2,...,symboln}]


when I import these symbols into my package it looks like this:

BeginPackage["example"];

<Entry Points>

Begin["Private"]

DumpGet[<some mx file>];
<Definitions>

End[];

EndPackage[];


there is already the catch: The Symbols are not automatically loaded into the private context of the package (which is examplePrivate in the above code snippet) but into the original context of the Notebook where the mx file has been generated (which is Global in my case)! Thus I need to convert the symbols either into the private context in the package definition (which would the following statements

examplePrivatesymbol1=Globalsymbol1;
examplePrivatesymbol2=Globalsymbol2;
.
.
.
examplePrivatesymboln=Globalsymboln;
Remove[Globalsymbol1,Globalsymbol2,...,Globalsymboln])


or save the symbols already into the right context by using

examplePrivatesymbol1=symbol1;
examplePrivatesymbol2=symbol2;
.
.
.
examplePrivatesymboln=symboln;
DumpSave[<some mx file>,{examplePrivatesymbol1, examplePrivatesymbol2,
..., examplePrivatesymboln}]


in the generating Notebook (which would be my preferred solution). I need to do all of this clumsy setup because I do not want to have the symbols in the Global context because then the symbols are visible outside of the package, which is not necessary and may cause shadowing problems too. Now my question: Is there a quick way to copy symbols over from one context into another? Something like

Set[Evaluate[examplePrivate#],Global#]&/@{symbol1,symbol2,...,symboln}


would be preferrable. However above statement does not work...

Any ideas?

• Mar 14, 2013 at 8:14
• A simple question: why do you need to load definitions from an MX file in your package? Wouldn't it be better to put everything in plain text and then save the complete package as a .MX file if loading speed is important? Mar 14, 2013 at 8:17
• Second question: you show copying definitions e.g. examplePrivatesymbol1 = Globalsymbol1; -- why not simply change the Context to examplePrivate  before you define those symbols in the first place? Mar 14, 2013 at 8:19
• @Mr.Wizard question 1: I'm using a Notebook with the autogenerated package option to define the package contents. I prefer this method because the package code is much better readable, because of the excellent formatting possibilities for Notebooks compared to .m files. Furthermore what goes into the package can be easily defined through the initialization cell flag. However the parameters I'm talking about are interpolation statements which are generated with a lot of data-crunching from measured data where I simply do not want to burden the package definition with, just for size reasons. Mar 14, 2013 at 13:25
• @Mr.Wizard question 2: Fair point. I initially used XML files for the transfer of the symbols and there the context is not preserved (Importing symbols from XML files just read in the short form of the symbol name and thus drop the context definition). However with the growing amout of XML files the Import run time increased too. Thus I was forced to switch over to .mx files. Since there are numerous symbols to export/import I would need to correct the context at multiple positions in the Notebook. Change it at one place (at the export) seemed to me the more efficient way. Mar 14, 2013 at 13:34

I solved my problem now differently. According to my last comment to my question I now just save the interpolation statements within the package into an mx file

DropFileExtension[datafile_] :=StringJoin[Riffle[Most[StringSplit[datafile, "."]], "."]];
DumpMX[datafile_, symbol_: dummy] := Module[{filename},
filename =DropFileExtension[datafile] <> "_"
<>StringReplace[$System, " " -> ""] <> "_" <> ToString[NumberForm[$VersionNumber, {2, 1}]] <> ".mx";
If[! MatchQ[FindFile[filename], $Failed], DeleteFile[filename]];(*Delete File if it is already existing*) symbol = ReadMX[datafile, True]; DumpSave[filename, symbol]; ]; ReadMX::MXFileError = "MX file 1 of optical database not found."; ReadMX[datafile_, symbol_: dummy] := Module[{filename, fields}, filename = DropFileExtension[datafile] <> "_" <> StringReplace[$System, " " -> ""] <> "_"
<> ToString[NumberForm[$VersionNumber, {2, 1}]] <> ".mx"; If[MatchQ[FindFile[filename],$Failed],
Return[symbol];
];
]


Any symbol (defined default globally as dummy) is saved into an MX file including the System and Mathematica Version information (since the binary MX file is not transferrable between different systems and versions) in the name.

DumpMX["test.mx",dummy]


ReadMX["test.mx",dummy]