Message Boards Message Boards

Packages and Notebooks as a Databin

Posted 9 years ago

Can I use Databins to share packages and notebooks?

If yes. How?

bin = CreateDatabin[]
DatabinAdd[bin, <|"myPack" -> "???"|>]

How can I load a Package stored in a Databin?

Same question in SE: here

POSTED BY: Rodrigo Murta
4 Replies

Hello Rodrigo,

Create a bin

mybin = CreateDatabin[]

a simple package read from disk with escapes

packstring = 
 "BeginPackage[\"droptest`\"]
 Unprotect[myFunc];
 myFunc::usage=\"a function from the data drop\"
    Begin[\"`Private`\"]
      myFunc[x_]:= x + 1
    End[]
 EndPackage[]"

Drop it in a bin

DatabinAdd[mybin, <| "myPack" -> packstring |>]

Ask for the values in your bin

Values[mybin]

if there is a lot of stuff in your bin you need to be more specific

x = mybin["Data"]["myPack"]["Values"][[3]]

The "Values" parameter is used because the datadrops representation is in "EventSeries" and I don't want the "Times" part of the coordinates. The part [[3]] is just the third selection.

You package tekst is now back from the data drop. Next challenge is the Get function to load it. My solution is to first export it to disk.

f = Export["e:\\folder\\tst2.wl", x, "Text"]

If you write to tst2.txt it works fine. For convention I write to .wl in plain text.

Get[f]

the context droptest is on the context path and myFunc[2] results in 3. Nice

So yes it is possible but why do you need such a solution? Wouldn't is be much easier if your package was loaded from your own web-server somewhere.

Tks Pieter.

I thought that it would make more elegant to exchange Wolfram code in this way. So users could interact directly with the repository, without need to use DropBox or it own server, and all could be done directly in Mathematica.

If I could just do something like:

DatabinAdd[mybin,"FilePath/myPackName.wl"]

and then, to load it, do:

Get@mybin["myPackName`"]

or

Needs@mybin["myPackName`"]

Would be cool!

But I believe that it's not the main pourpose of Databin (now). And that neither Get or Needs were overload to do this.

Pity. It would be nice for quick code exchange in forums and in the community.

POSTED BY: Rodrigo Murta
Posted 9 years ago

Here is an overloading sugestion:

Unprotect@File;
File/:DatabinAdd[mybin_Databin, File[packPath_]]:=Module[{},
    DatabinAdd[mybin, <|FileBaseName@packPath<>"`"->Import[packPath,"Text"]|>]
]
Protect@File;

So, to add you package into a Databin, just do:

DatabinAdd[Databin["3GUtgBVI"], File["/Desktop/dropTest.wl"]]

Now, to load it, let's work with Get:

Unprotect@Get;
Get[pack_String,Databin[id_String]]:=Module[{databin=Databin[id]},
    Get[pack,databin]
]

Get[pack_String,databin_Databin]:=Module[{packContent,path},
    packContent=Lookup[databin["Values"],pack,Return[$Failed]][[1]];
    path=FileNameJoin@{$TemporaryDirectory,StringReplace[pack,"`"-> ""]<>".wl"};
    Export[path, packContent, "Text"];
    Get[Evaluate@path]
]
Protect@Get;

Now, to load you package, just do:

Get["dropTest`", Databin["3GUtgBVI"]]

now:

myFunc[1]
2
POSTED BY: Updating Name

Some problem with my post. I can't edit it, and my name is missing.

This code part is useless:

Get[pack_String,Databin[id_String]]:=Module[{databin=Databin[id]},
    Get[pack,databin]
]

Can be removed

Murta

POSTED BY: Rodrigo Murta
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract