How can I do Get on InputStream?

One of the annoying things about encoded packages is that they must be in a separate file. If we want to distribute a simple stand-alone application (encoded), we need to provide two files: the "interface" of the application and the application package.

If I put the entire contents of an encoded file inside a string and convert that string to an InputStream, I halfway through the contents of this package look like a file.

But Get, which, as far as I know, the only operation (also used by Needs) that has a decoding function, does not work in streams. It only works with real files.

Can someone figure out a way to get the stream?

+4
source share
3 answers

Waiting for Mathematica to appear on my iPhone, so it can't verify anything, but why don't you write the line to a temporary file and get this?

Refresh

Here's how to do it:

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

It is important to copy the contents of a line of code from an ASCII file containing encoded code using the ASCII editor and paste it between existing empty quotation marks (""). Mathematica then automatically escapes backslashes and quotes that may be in code. This file was made earlier using Encode . You cannot do this here in the sample code, since SO Markdown will interfere with the string.

 Get[encoded] (* get encrypted code and decode *) DeleteFile[encoded] (* Remove temp file *) 

Final answer

Get not required for decoding. ImportString also works:

 ImportString["code string", "NB"] 

As above, paste the encoded text from the ASCII editor right between the "" and let MMA do the escaping.

enter image description here

+6
source

I don’t know how to get Stream, but you can store the encoded data in one package, write it to a temporary file, and then read the temporary file using Get.

+3
source

Just to update the information:

Get works with streams with V9.0.

0
source

Source: https://habr.com/ru/post/1369359/


All Articles