Re: notebooks default context
- To: mathgroup at smc.vnet.net
- Subject: [mg112344] Re: notebooks default context
- From: David Reiss <dbreiss at gmail.com>
- Date: Fri, 10 Sep 2010 04:48:40 -0400 (EDT)
- References: <firstname.lastname@example.org> <email@example.com> <firstname.lastname@example.org>
On Sep 9, 4:21 am, magma <mader... at gmail.com> wrote: > On Sep 7, 12:07 pm, Albert Retey <a... at gmx-topmail.de> wrote: > > > > > > > Hi, > > > > Firstly a problem, my packages (.m) keep having their context reset > > > to global every time I open them to edit rather than "unique to each > > > cell" to which I have previously set them to. > > > > THIS IS THE QUESTION (rest waffle) > > > I would like to set a notebooks default context to "Unique to each > > > cell" in a notebook initialisation cell. Usually I use "Unique to this > > > notebook" from the menu (which is fine and persists), however on > > > occasion I would like to tighten things up. > > > > ------------ > > > WAFFLE > > > ------------ > > > I think I don't clearly understand the use of the Package` context > > > instructions. My basic package setup is as follows (which works): > > > > BeginPackage[ "Package`"] > > > Begin[ "Public`"] > > > preProcess`outNB[nb_] = "something"; > > > End; > > > Begin[ "Private`"] > > > End > > > End > > > > so this is could be one source of the problem. > > > > This is part of a general question on good programming technique in > > > mathematica. My instinct is that for non-trivial notebooks and > > > packages "unique to cell" is the best method and the results should > > > then be put into a context for the notebook to make them available to > > > other cells. > > > I think as long as your work is all in one notebook, you usually don't > > really need to take too much care about namespaces (Contexts) at all. > > The use of Contexts only makes sense if your code gets complex enough so > > that the use of different namespaces for different encapsulated parts of > > your code is necessary to keep it maintainable. In these cases I think > > the "best practice" is to create a single file for each of these parts, > > and create a package file (*.m) which contains the BeginPackage/Begin > > stuff that controls in which Context the various symbols will be created. > > > You would then use Needs or Get in the notebook(s) that make use of the > > packages. The use of the settings for default contexts is actually a > > relatively new feature that seems to be used mainly in the documentation > > notebooks. It could probably be used also when developing and debugging > > packages or when creating graphical user interfaces, but I don't think > > that there are many people actually using it, and it is not really > > necessary and certainly an advanced feature. I would suggest that you > > don't care about it until you really are sure you know how to use > > contexts and packages in a more standard way. > > > Concerning your package structure: you are using a Begin["Public`"] > > which is very unusual, do you have a good reason to do so? The usual > > setup is: > > > BeginPackage["MyPackage`"] > > publicfunction::usage = "publicfunction..."; > > Begin["`Private`"] > > publicfunction[x_,y_]:=privatefunction[x]+y; > > privatefunction[x_]:=x^2; > > End > > End > > > Every symbol that is mentioned between the BeginPackage and Begin will > > be public, everything else will be private. When you load such a > > package, you will usually not need to explicitly use the package context > > anywhere in your code, since the package context will be on > > $ContextPath, so all public symbols of the package can be addressed > > without explicit context prefixes. > > > Also note that only with a leading ` the "`Private`" context will be > > "MyPackage`Private`", otherwise the private symbols of all your packages > > will live in the same context, "Private`", which is usually exactly what > > you try to avoid by using contexts. > > > > Regarding a previous post on fonts I now use: > > > > SetOptions[$FrontEnd, FontFamily->"Arial"]; > > > SetOptions[$FrontEnd, FontSize->10]; > > > > as soon as you ask the question, the solution suddenly becomes clear! > > > If this works for you, I don't know an important reason to not do so, > > but I think the usual way to control the appearance of fonts is to > > create or change stylesheets. You usually would create your own style > > sheet, probably inherting from one that is close to what you want, and > > then, if you want it to be used as a default, set the > > DefaultStyleDefinitions option in the option inspector to use that > > stylesheet. > > > hth, > > > albert > > apjs64, > Albert preceded me in replying to you, but here is my 2 cents advise > anyway. > > FORGET about notebook contexts or cell contexts! > These are things used by WRI developers or by people writing complex > documentation which needs to be kept somehow separated from a normal > Mathematica session. > Common mortals, including common developers, FORGET IT. > > What is left? Normal packages and contexts. Do you understand them > completely? I think not yet, judging by the example Package you wrote. > Albert gave you a template package. Stick to it. Even if you do not > fully understand why it is written this way. Stick to it. > But even better, try to understand why it works. > I suggest you read Maeder's "Programming in Mathematica" (3rd ed., but > the 2nd is good enough. I use the second). It covers Mathematica 3.x or > earlier versions, but that is NOT important. > It explains the Package/Context thing "frame by frame", so to speak. > So you understand exactly what happens when Mathematica reads a Package. > Other more recent programming Mathematica books may explain the process too, > but Maeder's book is the definitive resource for me. > So to go back to your example package, you seem to attach a semantic > (that is: a meaning) to the context name. You thing a context is > public if it is called Public and private if it is called Private. > That is NOT so! You might call them Bingo and Bongo and they still > would be private. Both of them. > > So again, stick to Albert's example package format and try to get > Maeder's book. I'd like to point out that I completely agree with this post.... It is the wisest of advice...