-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Override display format #69
Labels
Milestone
Comments
ohno
added
enhancement
New feature or request
good first issue
Good for newcomers
labels
Jul 2, 2024
Here is an example for the harmonic oscillator. julia> Base.show(io::IO, model::HarmonicOscillator) = print(io, "HarmonicOscillator(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(HarmonicOscillator)], ", ") * ")")
julia> HO = HarmonicOscillator(k=1.0, m=1.0, ℏ=1.0)
HarmonicOscillator(k=1.0, m=1.0, ℏ=1.0) We should add 2 lines to src/HarmonicOscillator.jl. export HarmonicOscillator, V, E, ψ
+ # formatter
+ Base.show(io::IO, model::HarmonicOscillator) = print(io, "HarmonicOscillator(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(HarmonicOscillator)], ", ") * ")")
# parameters
@kwdef struct HarmonicOscillator
k = 1.0
m = 1.0
ℏ = 1.0
end |
We may be able to avoid rewriting each file through metaprogramming. Add the following code to src/Antique.jl: for model in Antique.models
code = """
Base.show(io::IO, model::$(model)) = print(io, \"$(model)(\" * join([ \"\$(symbol)=\$(getproperty(model,symbol))\" for symbol in fieldnames($(model))], \", \") * \")\")
"""
code |> Meta.parse |> eval
print(code)
end Base.show(io::IO, model::InfinitePotentialWell) = print(io, "InfinitePotentialWell(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(InfinitePotentialWell)], ", ") * ")")
Base.show(io::IO, model::HarmonicOscillator) = print(io, "HarmonicOscillator(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(HarmonicOscillator)], ", ") * ")")
Base.show(io::IO, model::MorsePotential) = print(io, "MorsePotential(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(MorsePotential)], ", ") * ")")
Base.show(io::IO, model::HydrogenAtom) = print(io, "HydrogenAtom(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(HydrogenAtom)], ", ") * ")")
Base.show(io::IO, model::DeltaPotential) = print(io, "DeltaPotential(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(DeltaPotential)], ", ") * ")")
Base.show(io::IO, model::PoschlTeller) = print(io, "PoschlTeller(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(PoschlTeller)], ", ") * ")")
Base.show(io::IO, model::SphericalOscillator) = print(io, "SphericalOscillator(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(SphericalOscillator)], ", ") * ")")
Base.show(io::IO, model::RigidRotor) = print(io, "RigidRotor(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(RigidRotor)], ", ") * ")")
Base.show(io::IO, model::InfinitePotentialWell3D) = print(io, "InfinitePotentialWell3D(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(InfinitePotentialWell3D)], ", ") * ")")
Base.show(io::IO, model::CoulombTwoBody) = print(io, "CoulombTwoBody(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(CoulombTwoBody)], ", ") * ")") These codes are working: for model in Antique.models
code = "$(model)()"
println("julia> ", code)
code |> Meta.parse |> eval |> println
println()
end julia> InfinitePotentialWell()
InfinitePotentialWell(L=1.0, m=1.0, ℏ=1.0)
julia> HarmonicOscillator()
HarmonicOscillator(k=1.0, m=1.0, ℏ=1.0)
julia> MorsePotential()
MorsePotential(rₑ=1.997193319969992, Dₑ=0.10263461910653993, k=0.1027265041900817, μ=918.076336715, ℏ=1.0)
julia> HydrogenAtom()
HydrogenAtom(Z=1, mₑ=1.0, a₀=1.0, Eₕ=1.0, ℏ=1.0)
julia> DeltaPotential()
DeltaPotential(α=1.0, m=1.0, ℏ=1.0)
julia> PoschlTeller()
PoschlTeller(λ=1, m=1.0, ℏ=1.0, x₀=1.0)
julia> SphericalOscillator()
SphericalOscillator(k=1.0, μ=1.0, ℏ=1.0)
julia> RigidRotor()
RigidRotor(m₁=1.0, m₂=1.0, R=1.0, ℏ=1.0)
julia> InfinitePotentialWell3D()
InfinitePotentialWell3D(L=[1.0, 1.0, 1.0], m=1.0, ℏ=1.0)
julia> CoulombTwoBody()
CoulombTwoBody(z₁=-1, z₂=1, m₁=1.0, m₂=1.0, mₑ=1.0, a₀=1.0, Eₕ=1.0, ℏ=1.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to display variable names when displaying struct. Please refer to the discourse.
Current:
Goal:
Overwriting
dump()
toBase.show()
is enough if we ignore executability:In any case, this struct contains information about the variable names and their values.
The text was updated successfully, but these errors were encountered: