FileIO.jl icon indicating copy to clipboard operation
FileIO.jl copied to clipboard

Save expressions to .jl?

Open ChrisRackauckas opened this issue 5 years ago • 7 comments

I would like to just do save(filename, ::Expr) with ModelingToolkit.jl generated code. Would it be right to add this as a standard FileIO.jl thing?

ChrisRackauckas avatar Feb 06 '20 05:02 ChrisRackauckas

This may already work if filename ends in .jld or .jld2 (I haven't tested recently). Or are you thinking of a dedicated serialization format?

EDIT: Oh, from the title you mean as a loadable .jl file? That's easy if you just wrap in an eval but I'd guess you want it to have a readable & editable result? That would be interesting. I assume you know about https://docs.julialang.org/en/latest/devdocs/ast/ and would only be interested in the surface syntax?

timholy avatar Feb 06 '20 08:02 timholy

Yeah, I am thinking it would be a nice helper to allow for just saving expressions to files, just doing write("function.jl", string(func)). It's nothing big but something that felt missing.

ChrisRackauckas avatar Feb 06 '20 13:02 ChrisRackauckas

It would be excellent to make that easy. I've wanted something like that for a while.

If you've not done this before, I just submitted a PR myself (#254) so there are "fresh" models available. I guess this is a case, though, where you'd want save but not load support. We also couldn't have magic bytes, it will have to be detected entirely by the ".jl" file extension. So really it's not very similar to my recent PRs.

timholy avatar Feb 06 '20 16:02 timholy

load could take a Julia file and make an array of Expr from it. 99.99% of the time you'd want include though.

ChrisRackauckas avatar Feb 07 '20 03:02 ChrisRackauckas

Very dim memories clamoring for attention: https://github.com/timholy/MacroExpandJL.jl. Steal anything you find useful.

timholy avatar Feb 07 '20 07:02 timholy

@ChrisRackauckas Do you still want this?

ViralBShah avatar Feb 27 '24 16:02 ViralBShah