Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Init.m considerations and good practices

As I never found (or perhaps I never search for it enough) a good article about how to manage the init.m files, I ended up developing my own "standard", but I wonder how bad I did it.

For example, my usual init.m is stored in C:\Documents and Settings\All Users\Application Data\Mathematica\Kernel\init.m (Windows) and I edit it using a text editor.

As I don't want the definitions to go into the Global context, the content is something like:

(** User Mathematica initialization file **)
Begin["MyInitContext`"];

Cl:=Clear["Global`*"];
(* Other definitions in this Context *)

End[]; (* End Context *)
$ContextPath = Prepend[$ContextPath,"MyInitContext`"];

I don't load packages from the init.m, because I want strict control over what I load, so I only define here shortcuts to utility functions I use on a daily basis and some options.

So: Any references to good practices? Better ways to achieve this kind of behavior? Any caveats?

like image 221
Dr. belisarius Avatar asked Mar 13 '11 23:03

Dr. belisarius


People also ask

Where should you place the __init__ method in a project?

This is where readers should look for the __init__ method. If you have class attributes, you should place the __init__ method after these class attributes. You must do it consistently across all the classes that you define for your project so that readers won’t be confused.

What are the disadvantages of __init__?

__init__.py can end up very cluttered if there are many modules with many functions. Like the general store, a convenience store that is too cluttered will be harder for customers to navigate. When new features are added to a module (i.e., new class or functions), they have to be explicitly added to the __init__.py file too.

What is the use of __init__ in Python?

As the name indicates, __init__ means initialization, which refers to the process of setting the initial state of the newly created instance object. As a convention, it’s important for you to name the first parameter as self, although it’s not required to do so.


2 Answers

Firstly, I would strongly recommend against putting anything significant init.m, since this invariably results in old stuff being broken when you come back to it after a few years. Much better to put your customizations on the path so you can quickly load it at the head of each notebook: That way the context is explicitly stated and you can easily change versions without breaking old stuff.

My current setup is to start with Needs["Janus`"] where the Janus directory has a custom init.m file that loads every file in the directory into the context. This means I can add utility functions in each their own file like this one (clear_cache.m):

ClearCache::usage="ClearCache[f] unsets all numeric-only downvalues of f, \
  see http://stackoverflow.com/questions/5086749"     

Begin["`Private`"];
ClearCache[f_Symbol] := 
  DownValues[f] = DeleteCases[DownValues[f], _?(FreeQ[First[#], Pattern] &)]
End[]

Here is the file Janus/init.m. Note that it prints out the name of the loaded extensions, all in the spirit of keeping the context explicit without too much hassle.

Module[{packageName,packageFileName,fileNames},
  (* $Input is set to Foo.m when evaluating Foo/init.m *)
  If[$Input=="", Print["init.m cannot run interactively"];Abort[]];
  packageName=StringDrop[$Input,-2];
  packageFileName=FindFile[packageName<>"`"];
  If[packageFileName==$Failed, Print["Unable to find package "<>packageName];Abort[]];
  fileNames=Select[
    FileNames["*.m",{DirectoryName@packageFileName},1],
    FileBaseName[#]=!="init"&];
  Print["Loading extensions from "<>DirectoryName@packageFileName<>" to context "<>packageName<>"`:"];
  BeginPackage[packageName<>"`"];
  Do[Print["Loading "<>fn]; Get@fn, {fn,fileNames}];
  EndPackage[]]
like image 184
Janus Avatar answered Oct 23 '22 17:10

Janus


My Kernel/init.m looks like this:

AppendTo[$Path, Environment["MMA_LIB"]]
Needs["WRUtil`"]

WRUtil contains all of my little utilities and performs other initialization that takes into account the platform and Mathematica version. MMA_LIB is an environment variable that points to a directory full of Mathematica packages. That directory is kept under version control and can be shared by multiple Mathematica instances. I like to keep init.m short so that moving into a new Mathematica installation is as simple as typing two lines that I have committed to memory -- it is surprising how often I seem to have to do this.

like image 26
WReach Avatar answered Oct 23 '22 17:10

WReach