Is there a function or macro that accepts an object and returns the Julia code that defines the object? For instance, is there a function f() with the following behaviour?
julia> X=Dict([("A", 1), ("B", 2)])
Dict{String, Int64} with 2 entries:
"B" => 2
"A" => 1
julia> f(X)
Dict([("A", 1), ("B", 2)])
julia>eval(f(X))
Dict{String, Int64} with 2 entries:
"B" => 2
"A" => 1
My use case is software regression testing, so any alternative for recovering a definition from a value is welcome.
If you mean I want an already assembled call to the default constructor, then show(X) gives you Dict("B" => 2,"A" => 1) in your example.
If you mean I want to recover the exact way this object was constructed or the line this happened, no. I do not believe this is easy to achieve and, in fact, it may be impossible.
Option 1 is sufficient. I just need a way to reproduce the result, not recover the history. show() works for dictionaries, but not for other objects like DataFrames. For these, it behaves more like display, which gives a human-readable (but not Julia-readable) version.
There is none. As a workaround, I could type out the definition myself. I am curious if there is some way to generate this automatically. This would be helpful for much larger structures.
The function I write will generate the object. However, I am software regression testing, so I want to make sure that the output stays the same between versions. So, Iβd like to save the output from a previous version. Test cases might involve very large data frames, so it isnβt feasible to type out the definition of the intended output. One workaround might be to compare the test display of the DataFramse using show(), but Iβd like to directly compare DataFrames for equality.
just save the output to a string, re-direct stdout, and compare? this is a very common test in Julia
how else are you gonna test across versions? if you use some auto generation, such as repr, supposedly it will always equal to the other end of the equality right? namely, the current repr you want to test.
so you always have to hard-code previous, known good output β isnβt that the whole point?
Thanks, this should be sufficient. Still, Iβd prefer to hard code two DataFrame definitions rather than hard code the text output from the display. Since Julia is homoiconic, Iβd be interested in seeing a version of repr() or show() that handles a DataFrame the way a Dict is handled. Iβll accept your answer if I canβt find something like that. Many thanks!