更改后更新(重新加载)mathematica 包

发布于 2024-12-20 06:51:07 字数 604 浏览 2 评论 0原文

我正在尝试找到以下循环的快捷方式。开发 mathematica 包时,需要对 .m 文件代码进行更改,然后想要在另一台笔记本中测试更改。这是一个无限循环...

因此,我们有一个包 package.m 和一个笔记本 test_package.nb 来完成测试。目前,当我更改 .m 文件中的某些内容时,我必须:

Quit[]
Needs["package`"]

在笔记本中使更改可用,以便我可以测试它们。看起来如果没有 Quit[] 它就不起作用。

有没有更短的方法来迭代这个开发循环?特别是避免戒烟?

后果

如果可以的话,我会接受 ruebenko 和 Leonid 的答案,因为两者都有帮助并解决了我的问题。然而,由于只能选择一个答案,我选择了 ruebenko 的,因为它简单易用 - 通过简单地将 Needs 更改为 Get 我克服了我的问题。尽管如此,Leonid 的软件包肯定更复杂,并且最终可能会产生更好的结果,但必须安装、加载等。

I'm trying to find a shortcut to the following loop. When developing a mathematica's package one is implementing changes to the .m file's code and then want to test the changes in another notebook. This is an infinite loop...

So, we have a package package.m and a notebook test_package.nb where testing is done. Currently, when ever I change something in the .m file, I then have to:

Quit[]
Needs["package`"]

in the notebook for the changes to become available, so I can test them. It seems like without the Quit[] it doesn't work.

Is there a shorter way to iterate this development loop? In particular avoid the quitting?

Aftermath

If I could, I'd accept both ruebenko and Leonid's answers as both are helpful and solve my problem. However, as only one answer can be chosen, I picked ruebenko's due to is simplicity and easiness - by simply changing the Needs to Get I overcame my problem. Nevertheless, Leonid's package is for sure more sophisticated and probably at the end of the day yields better results, but it has to be installed, loaded etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋千易 2024-12-27 06:51:07

使用获取;仅当根本未加载包时才需要加载该包。有时您需要小心,并对包中的变量使用 ClearAll。

Use Get; Need only loads the package if is not loaded at all. Sometimes you need to be careful and use ClearAll on the variables in the package.

记忆之渊 2024-12-27 06:51:07

作为使用简单的 Get 的替代方案,正如 @ruebenko 所建议的(这是一个很好的建议),您可以尝试使用我编写的名为 PackageManipulations` 的包,它可以执行以下操作:你想要的。它位于此处。有一个笔记本,其中包含说明其工作原理的示例,也可以在我链接的页面上找到。包可以重新加载、清除或删除给定的包(上下文)。函数PackageReload将专门执行您想要的操作。

它确实在底层使用 GetNeeds,但除此之外,它还会跟踪所谓的“转义符号”(公开导出但没有使用消息的符号,并且可能“泄漏”实现细节),并且可以选择解决遮蔽问题,以有利于重新加载包。请注意,PackageReload 将自动对包的符号调用 UnprotectClearAll,从而解决 @ruebenko 提到的问题。您可以在软件包附带的示例笔记本中找到更多详细信息。

请注意,虽然我自己多次使用它,但我并不声称它没有错误:)

As an alternative to using simple Get, as sugested by @ruebenko (which is a good advice), you can try using a package I wrote called PackageManipulations`, which does what you want. It is available here. There is a notebook with examples illustrating how it works, also availabe at the page I linked. The package can reload, clear or remove a given package (context). The function PackageReload specifically will do what you want.

It does use Get or Needs under the hood, but in addition it will track the so-called "escaping symbols" (symbols which are publicly exported but have no usage messages, and may be "leaking" the implementation details), and optionally can resolve the shadowing problems in favor of the package being re-loaded. Note that PackageReload will automatically call Unprotect and ClearAll on package's symbols, therefore addressing the issue mentioned by @ruebenko. You can find more details in the example notebook which comes with the package.

Note that, while I used it myself many times, I don't claim it is bug-free :)

落在眉间の轻吻 2024-12-27 06:51:07

最简单的方法是使用 Clear 或者更好然而,ClearAll 在你的包中公开的符号上在你的一开始包,如下所示

BeginPackage["SomePackage`"];
ClearAll[ ASymbol, AnotherSymbol, ...];

这可以确保您每次加载它时都有一个干净的状态。一旦包变得更加稳定,这会更容易,但我发现如果我在最初开发包时不这样做,它就不会完成。

The easiest method is to use Clear or, better yet, ClearAll on the exposed symbols in your package at the very beginning of your package, as follows

BeginPackage["SomePackage`"];
ClearAll[ ASymbol, AnotherSymbol, ...];

This ensures that you have a clean slate whenever you load it. This is easier once the package is more stable, but I find if I don't do this when I'm initially developing a package, it doesn't get done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文