MathGroup Archive 2009

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

Search the Archive

Re: Needs[], BeginPackage[], filename

  • To: mathgroup at smc.vnet.net
  • Subject: [mg101065] Re: [mg101038] Needs[], BeginPackage[], filename
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Tue, 23 Jun 2009 07:04:49 -0400 (EDT)
  • References: <200906220821.EAA09072@smc.vnet.net>

Hi Scot,

May be I miss some sources where alternatives are shown, but your
suggested workaround is exactly what Roman Maeder also suggests as a
possible solution to this very problem in his "Programming in Mathematica"
3-rd edition (p. 50)

So, unless things changed significantly in the package system since 1997,
this is probably it. You can, of course, add your own definition to
BeginPackage, which will itself execute Needs and then call the standard
BeginPackage, Something like this:

With[{filepattern = (Rule | RuleDelayed)[_String, _String]},
  Unprotect[BeginPackage];
  Clear[BeginPackage];
  BeginPackage[context_String, dependson : {(_String | filepattern) ..}] /;
    MemberQ[dependson, filepattern] :=
   With[{filesAndContexts = Cases[dependson, filepattern]},
    Needs[#2, #1] & @@@ filesAndContexts;
    BeginPackage[context,
     dependson /. (Rule | RuleDelayed)[file_String, cont_String] :> cont]];
  Protect[BeginPackage]
];

This adds a new syntax that allows you to intersperse the context names with
rules that tell BeginPackage which file names to associate with which
context.

Example of use (insert your own context names):

BeginPackage[
  "TestContext`",
{"OptionPipe`",
   "AttributesOfPureFunctions.m" :> "AttributesOfPureFunctions`",
   "NbToHTML`", "CheckOptions.m" -> "CheckOptions`"}];

EndPackage[]

But, a disclaimer :) - I can not guarantee that this will always work
properly.
I tried a few simple cases and it seems to work for me, but I hope I won't
be flamed for this :)

Hope this helps.

Regards,
Leonid





On Mon, Jun 22, 2009 at 1:21 AM, Scot T. Martin <smartin at seas.harvard.edu>wrote:

> Needs[] allows the context and filename to be specified.
>
> BeginPackage[] allows a context and not a file name.
>
> Then, my packages require two steps, as in the following example:
>
> ** start **
>
> Needs["sizedistribution`", "size functions.m"]
>
> BeginPackage["tandemdma`", {"sizedistribution`"}]
>
> ** stop **
>
> Are two steps really necessary or am I missing something very simple?
>
> My goal would be to just forego Needs[] and handle everything in one step
> with BeginPackage, but I cannot figure out a way to feed the filename to
> BeginPackage[].
>
> I had a search on the MathArchives, but I did not succed in locating any
> material addressing this question.
>
> Thanks for any quick hints!
>
>
>
>


  • Prev by Date: Re: Two questions about modification of a matrix.
  • Next by Date: Re: from the list select
  • Previous by thread: Re: Needs[], BeginPackage[], filename
  • Next by thread: Re: Needs[], BeginPackage[], filename