Function Repository Resource:

GetDefinitions

Source Notebook

Use Get without modifying the $ContextPath

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["GetDefinitions"][file]

is equivalent to Get[file], but the original value of $ContextPath is restored after evaluation.

ResourceFunction["GetDefinitions"]["file","key"]

reads a file that has been encoded using Encode["source","file","key"].

Details and Options

Symbols defined in the package loaded by ResourceFunction["GetDefinitions"] can be used by their fully qualified names.
If name is the name of a Wolfram Language context ending with a ` context mark character, then ResourceFunction["GetDefinitions"] will process this name to find the file to read.
For a name of the form "context`", ResourceFunction["GetDefinitions"] will by default search for the following files:
context.mxfile in DumpSave format
context.mx/$SystemID/context.mxfile in DumpSave format for your computer system
context.wlfile in the Wolfram Language source format
context/Kernel/init.wlkernel initialization file for a particular directory
context/init.wlgeneral initialization file for a particular directory
context.mfile in the Wolfram Language source format
context/Kernel/kernel initialization file for a particular directory
context/general initialization file for a particular directory
For a name of the form "context`subcontext`", ResourceFunction["GetDefinitions"] will by default search for "subcontext`" inside a directory named "context".
If name is the name of a file, any extension must be included explicitly.
The following options can be given:
CharacterEncoding$CharacterEncodingwhat raw character encoding to use
MethodAutomaticmethod to use for reading streams
Path$Pathdirectories in which to search for the given file
Syntax errors in Wolfram Language input files are reported in the standard form filename: line: syntax error in expr. ResourceFunction["GetDefinitions"] continues attempting to read a file even after a syntax error has been detected. However, if an error is detected, $Context and $ContextPath are reset to the values they had when ResourceFunction["GetDefinitions"] was called.
ResourceFunction["GetDefinitions"] can read .nb notebook files, returning the low-level box constructs that represent them.
ResourceFunction["GetDefinitions"][CloudObject[]] can be used to get files from the cloud.
ResourceFunction["GetDefinitions"][LocalObject[]] can be used to get files from local persistent storage.
ResourceFunction["GetDefinitions"][Databin[]] gets the contents of a databin in the Wolfram Data Drop.
When operating on a local file, the global variables $Input and $InputFileName are set to the file name and the full path of the file being read, respectively, during the execution of ResourceFunction["GetDefinitions"].
With the Method option, the stream is opened using the given input stream method. This overrides the default way that ResourceFunction["GetDefinitions"] resolves file names. The value of the method option can be any member of $InputStreamMethods.

Examples

Basic Examples (2) 

Load a package:

In[1]:=
ResourceFunction["GetDefinitions"]["EquationTrekker`"]

This has set up definitions for several symbols in the EquationTrekker` context:

In[2]:=
Names["EquationTrekker`*"]
Out[2]=

Note that the symbols are fully qualified names, since EquationTrekker` is not added to $ContextPath:

In[3]:=
$ContextPath
Out[3]=

Compare to using Get on the package:

In[4]:=
Get["EquationTrekker`"]

The EquationTrekker` context and other contexts it loads are added to $ContextPath:

In[5]:=
$ContextPath
Out[5]=

Symbols are accessible by name without requiring context:

In[6]:=
Names["EquationTrekker`*"]
Out[6]=

Get Wolfram Language input from a file:

In[7]:=
FilePrint["ExampleData/language"]
In[8]:=
ResourceFunction["GetDefinitions"]["ExampleData/language"]
Out[8]=

The input is evaluated:

In[9]:=
Block[{a = 1, b = 2, c = 3, d = 4}, ResourceFunction["GetDefinitions"]["ExampleData/language"]]
Out[9]=

Scope (2) 

Local objects can be used with Put and GetDefinitions to store expressions persistently:

In[10]:=
Put[10!, LocalObject["myval"]]
Out[10]=
In[11]:=
ResourceFunction["GetDefinitions"][%]
Out[11]=

Cloud objects can be used with Put and GetDefinitions to store expressions in the cloud:

In[12]:=
Put[10!, CloudObject["cval"]]
Out[12]=
In[13]:=
ResourceFunction["GetDefinitions"][%]
Out[13]=

Options (8) 

CharacterEncoding (4) 

Create a package in the encoding "ISO8859-7":

In[14]:=
package = Close[BinaryWrite[CreateFile[], ToCharacterCode["\[Alpha]\[Beta]\[Gamma]", "ISO8859-7"]]];

Read the package back in the same encoding:

In[15]:=
ResourceFunction["GetDefinitions"][package, CharacterEncoding -> "ISO8859-7"]
Out[15]=

By default, the value of $CharacterEncoding is used, which may produce errors and different results:

In[16]:=
ResourceFunction["GetDefinitions"][package]
Out[16]=

Delete the package file:

In[17]:=
DeleteFile[package]

Method (2) 

Force the first argument to be interpreted as a string containing a package:

In[18]:=
ResourceFunction["GetDefinitions"]["2+2", Method -> "String"]
Out[18]=

With the default setting of Automatic, it would be interpreted as a file name:

In[19]:=
ResourceFunction["GetDefinitions"]["2+2", Method -> Automatic]
Out[19]=

Path (2) 

By default, all directories on $Path are consulted when searching for a file:

In[20]:=
ResourceFunction["GetDefinitions"]["ExampleData/language"]
Out[20]=

Force only the current directory to be searched:

In[21]:=
ResourceFunction["GetDefinitions"]["ExampleData/language", Path -> "."]
Out[21]=

Properties & Relations (2) 

FindFile["context`"] gives that file that GetDefinitions["context`"] would load:

In[22]:=
FindFile["ComputerArithmetic`"]
Out[22]=

Load the file:

In[23]:=
ResourceFunction["GetDefinitions"][%]

Verify that the ComputerArithmetic package was indeed loaded:

In[24]:=
Names["ComputerArithmetic`*"]
Out[24]=

Ensure that an initialization file is read only once:

In[25]:=
Once[ResourceFunction["GetDefinitions"]["init.m"]]

Applications (3) 

Write a function that loads definitions in another package while avoiding naming conflicts:

In[26]:=
MeanCI[data_] := (Once[
    ResourceFunction["GetDefinitions"]["HypothesisTesting`"]]; Interval[HypothesisTesting`MeanCI[data]]);
In[27]:=
MeanCI[{1, 2, 4, 6, 3}]
Out[27]=

Since HypothesisTesting` is kept off the context path, there is no naming conflict with MeanCI:

In[28]:=
Names["*`MeanCI"]
Out[28]=

An equivalent method would be to use the resource function NeedsDefinitions:

In[29]:=
MeanCI[data_] := (ResourceFunction["NeedsDefinitions"][
    "HypothesisTesting`"]; Interval[HypothesisTesting`MeanCI[data]]);

Version History

  • 1.0.0 – 27 May 2020

Related Resources

License Information