Init.m 注意事项和良好做法 [英] Init.m considerations and good practices
问题描述
因为我从来没有找到(或者我搜索的次数不够多)一篇关于如何管理 init.m 文件的好文章,所以我最终开发了自己的标准",但我想知道我做得有多糟糕.
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.
例如,我通常的 init.m 存储在 C:\Documents and Settings\All Users\Application Data\Mathematica\Kernel\init.m
(Windows) 中,我使用文本编辑器.
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`"];
我不从 init.m 加载包,因为我想严格控制我加载的内容,所以我在这里只定义了我每天使用的实用函数的快捷方式和一些选项.
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?
推荐答案
首先,我强烈建议不要放置任何重要的 init.m
,因为这总是会导致你来的时候旧东西被破坏几年后回到它.更好地将您的自定义放在路径上,以便您可以在每个笔记本的开头快速加载它:这样上下文就被明确说明,您可以轻松地更改版本而不会破坏旧内容.
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.
我当前的设置是从 Needs["Janus`"]
开始,其中 Janus
目录有一个自定义的 init.m
文件将目录中的每个文件加载到上下文中.这意味着我可以在每个自己的文件中添加实用函数,就像这样一个 (clear_cache.m
):
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[]
这是文件 Janus/init.m
.请注意,它会打印出已加载扩展的名称,所有这些都本着保持上下文明确的精神,而不会有太多麻烦.
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[]]
这篇关于Init.m 注意事项和良好做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!