MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: notebooks default context

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112278] Re: notebooks default context
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Tue, 7 Sep 2010 06:08:02 -0400 (EDT)
  • References: <i64kfg$39a$1@smc.vnet.net>

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


  • Prev by Date: Re: locating overlow/underflow (and the issue of accuracy)
  • Next by Date: Re: FindRoots?
  • Previous by thread: notebooks default context
  • Next by thread: Re: notebooks default context