For an experiment I am running it is handy for me to define my own types. Tha problem is that the computation is really heavy so I need to run it on a server and then access the data to plot results.
So the first type has certain fields that I define and then the other types do not have a fixed number of fields. Basically I have this kind of intertwined types:
type TestType1
value::Int
end
type TestType2
testvec::Any
TestType2()=new(Test2[])
end
type TestType3
testtype2::Any
TestType3()=new(TestType2[])
end
type TestType4
testtype3::Any
TestType4()=new(TestType3[])
end
Because the TestType1 never changes but within TestType2 I want to store different number of testtype1 values etc...
So the chain creation work like that :
test1=Test2(2);
test2=TestType2();
test3=TestType3();
test4=TestType4();
push!(test2.testvec,test1)
push!(test3.testtype2,test2)
push!(test4.testtype3,test3)
This works well I can have an easy access to everything I need in my experiment.
although the problem is as I run it on a server I need to be able to store test4
at the end of the run and then open it again on an other server to use the datas and do nice plots.
I tryed to use JLD but I think it is for dictionaries and it does not allow me to retrieve the information.
I would like to be able to call all the fields inside my test4 file stored somewhere the same way I do without changing server.
I am quite new in Julia and in OOP in general I dont have any idea idea how to do what I want to do.
Can anyone help please?
Thank you!
JLD.load
returns a dictionary, inside it is your object. For example:
# test_types.jl
__precompile__()
module TestTypes
using JLD: save
import Base: push!
export TestType1, TestType2, TestType3, TestType4
struct TestType1
value::Int
end
struct TestType2
data::Vector{TestType1}
TestType2() = new(TestType1[])
end
struct TestType3
data::Vector{TestType2}
TestType3() = new(TestType2[])
end
struct TestType4
data::Vector{TestType3}
TestType4() = new(TestType3[])
end
for n in 2:4
@eval function Base.push!(test_type::$(Symbol(:TestType, n)), test_arg::$(Symbol(:TestType, n-1)))
push!(test_type.data, test_arg)
end
end
function main()
test1 = TestType1(2)
test2 = TestType2()
test3 = TestType3()
test4 = TestType4()
push!(test2, test1)
push!(test3, test2)
push!(test4, test3)
file = "test_resutls.jld"
save(file, "test4", test4)
info("Test results saved in: $file")
return @show test4
end
end
An interaction looks like this:
julia> include("test_types.jl")
Main.TestTypes
julia> using Main.TestTypes: main
julia> original_test4 = main();
INFO: Test results saved in: test_resutls.jld
test4 = Main.TestTypes.TestType4(Main.TestTypes.TestType3[Main.TestTypes.TestType3(Main.TestTypes.TestType2[Main.TestTypes.TestType2(
Main.TestTypes.TestType1[Main.TestTypes.TestType1(2)])])])
julia> using JLD: load
julia> results = load("test_resutls.jld")
Dict{String,Any} with 1 entry:
"test4" => Main.TestTypes.TestType4(Main.TestTypes.TestType3[Main.TestTypes.TestType3(Main.TestTypes.TestType2[Main.TestTypes.Test…
julia> copy_test4 = results["test4"] # your data is inside this dict!
Main.TestTypes.TestType4(Main.TestTypes.TestType3[Main.TestTypes.TestType3(Main.TestTypes.TestType2[Main.TestTypes.TestType2(Main.Tes
tTypes.TestType1[Main.TestTypes.TestType1(2)])])])
julia> original_test4.data[1].data[1].data[1].value == copy_test4.data[1].data[1].data[1].value
true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.