如何在输入流上执行 Get 操作?

发布于 2024-12-01 15:59:23 字数 246 浏览 5 评论 0原文

编码包的一件烦人的事情是它们必须位于单独的文件中。如果我们想分发一个简单的自包含应用程序(编码),我们需要提供两个文件:应用程序“界面”和应用程序包。

如果我将编码文件的所有内容放入一个字符串中,并将该字符串转换为 InputStream,那么我就将该包内容视为文件了。

但是据我所知,Get 是唯一具有解码功能的操作(也被 Needs 使用),在 Streams 上不起作用。它仅适用于真实文件。

有人能找到一种获取流的方法吗?

One annoying thing of encoded packages is that they have to be in a separate file. If we want to distribute a simple self contained app (encoded), we need to supply two files: the app "interface", and the app package.

If I place all the content of the encoded file inside a string, and transform that string into an InputStream, I'm halfway to view that package content as a file.

But Get, that to my knowledge is the only operation (also used by Needs) that has the decoding function, doesn't work on Streams. It only works on real files.

Can someone figure out a way to Get a Stream?

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

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

发布评论

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

评论(3

如此安好 2024-12-08 15:59:23

等待 Mathematica 出现在我的 iPhone 上,因此无法测试任何内容,但为什么不将字符串写入临时文件并获取它呢?

更新

操作方法如下:

encoded = ToFileName[$TemporaryDirectory, "encoded"];

Export[encoded, "code string", "Text"]; (*export encrypted code to temp file *)

使用 ASCII 编辑器从包含编码代码的 ASCII 文件中复制代码字符串的内容并将其粘贴到现有的空引号 ("") 之间非常重要。然后,Mathematica 将自动转义代码中可能存在的反斜杠和引号。该文件是之前使用 Encode 创建的。无法在示例代码中执行此操作,因为 SO 的 Markdown 会混淆字符串。

Get[encoded] (* get encrypted code and decode *) 

DeleteFile[encoded] (* Remove temp  file *)

最终答案

Get 似乎对于解码来说不是必需的。 ImportString 也能正常工作:

ImportString["code string", "NB"] 

如上所述,将 ASCII 编辑器中编码的 tekst 直接粘贴到“”之间,然后让 MMA 进行转义。

在此处输入图像描述

Waiting for Mathematica to arrive on my iPhone so couldn't test anything, but why don't you write the string to a temporary file and Get that?

Update

Here's how to do it:

encoded = ToFileName[$TemporaryDirectory, "encoded"];

Export[encoded, "code string", "Text"]; (*export encrypted code to temp file *)

It's important to copy the contents of the code string from the ASCII file containing the encoded code using an ASCII editor and paste it between existing empty quotes (""). Mathematica will then do automatic escaping of backslashes and quotes that may be in the code. This file has been made earlier using Encode. Can't do it here in the sample code as SO's Markdown messes with the string.

Get[encoded] (* get encrypted code and decode *) 

DeleteFile[encoded] (* Remove temp  file *)

Final Answer

Get doesn't appear to be necessary for decoding. ImportString does work as well:

ImportString["code string", "NB"] 

As above, paste your encoded tekst from an ASCII editor straight between the "" and let MMA do the escaping.

enter image description here

゛时过境迁 2024-12-08 15:59:23

我不知道获取流的方法,但您可以将编码数据存储在单个包中,将其写入临时文件,然后使用 Get 读回临时文件。

I don't know of a way to Get a Stream, but you could store the encoded data in your single package, write it out to a temp file, then read the temp file back in with Get.

清眉祭 2024-12-08 15:59:23

只是为了保持最新状态:

自 V9.0 起,Get 可与配合使用。

Just to keep things up to date:

Get works with streams since V9.0.

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