Skip to content

Commit

Permalink
exposed update and cached
Browse files Browse the repository at this point in the history
  • Loading branch information
nsiccha committed May 4, 2023
1 parent 00fe89a commit 994c19b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DynamicObjects"
uuid = "23d02862-63fe-4c6e-8fdb-1d52cbbd39d5"
authors = ["Nikolas Siccha <[email protected]> and contributors"]
version = "0.1.4"
version = "0.1.5"

[deps]
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Expand Down
4 changes: 2 additions & 2 deletions examples/_freeze/index/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"hash": "a030b219fc18d58b27547d2267b43c0f",
"hash": "61154ed732487d9179d206dfd250cb56",
"result": {
"markdown": "---\ntitle: Geometry example\nexecute:\n daemon: 999999\n---\n\n::: {.cell execution_count=1}\n``` {.julia .cell-code}\nusing DynamicObjects\n\n\"\"\"\nA common base type for shapes.\n\"\"\"\n@dynamic_type DynamicShape\n\ndescription(what::DynamicShape) = \"A $(what) has an area of $(what.area).\"\n\n\"\"\"\nA fancy rectangle.\n\"\"\"\n@dynamic_object Rectangle <: DynamicShape height::Number width=1 \narea(what::Rectangle) = what.height * what.width\n\n\"\"\"\nA boring circle.\n\"\"\"\n@dynamic_object Circle <: DynamicShape radius::Number\narea(what::Circle) = what.radius^2 * pi \n\nprintln(Rectangle(10).description)\nprintln(Circle(20).description)\nCircle(20) \n\nDynamicObjects.cached(Circle(20), :area)\n```\n\n::: {.cell-output .cell-output-stderr}\n```\n┌ Info: Precompiling DynamicObjects [23d02862-63fe-4c6e-8fdb-1d52cbbd39d5]\n└ @ Base loading.jl:1664\n```\n:::\n\n::: {.cell-output .cell-output-stdout}\n```\nA Rectangle(height = 10, width = 1) has an area of 10.\nA Circle(radius = 20,) has an area of 1256.6370614359173.\n```\n:::\n\n::: {.cell-output .cell-output-display execution_count=2}\n```\n1256.6370614359173\n```\n:::\n:::\n\n\n",
"markdown": "---\ntitle: Geometry example\nexecute:\n daemon: 999999\n---\n\n::: {.cell execution_count=1}\n``` {.julia .cell-code}\nusing DynamicObjects\n\n\"\"\"\nA common base type for shapes.\n\"\"\"\n@dynamic_type DynamicShape\n\ndescription(what::DynamicShape) = \"A $(what) has an area of $(what.area).\"\n\n\"\"\"\nA fancy rectangle.\n\"\"\"\n@dynamic_object Rectangle <: DynamicShape height::Number width=1 \narea(what::Rectangle) = what.height * what.width\n\n\"\"\"\nA boring circle.\n\"\"\"\n@dynamic_object Circle <: DynamicShape radius::Number\narea(what::Circle) = what.radius^2 * pi \n\nprintln(Rectangle(10).description)\nprintln(Circle(20).description)\nCircle(20) \n\nCircle(20).cached_area\n\nCircle(20) |> update(:area) |> cached(:description) \nCircle(20) |> update(:area) |> cached(:description) \n```\n\n::: {.cell-output .cell-output-stdout}\n```\nA Rectangle(height = 10, width = 1) has an area of 10.\nA Circle(radius = 20,) has an area of 1256.6370614359173.\n```\n:::\n\n::: {.cell-output .cell-output-display execution_count=12}\n```\n\"A Circle(radius = 20, area = 1256.6370614359173) has an area of 1256.6370614359173.\"\n```\n:::\n:::\n\n\n",
"supporting": [
"index_files"
],
Expand Down
Binary file not shown.
6 changes: 5 additions & 1 deletion examples/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@ println(Rectangle(10).description)
println(Circle(20).description)
Circle(20)
DynamicObjects.cached(Circle(20), :area)
Circle(20).cached_area
Circle(20) |> update(:area) |> cached(:description)
Circle(20) |> update(:area) |> cached(:description)
```
28 changes: 18 additions & 10 deletions src/DynamicObjects.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module DynamicObjects
export AbstractDynamicObject, DynamicObject, @dynamic_object, @dynamic_type#, update, cached,
export AbstractDynamicObject, DynamicObject, @dynamic_object, @dynamic_type, update, cached
import Serialization


Expand Down Expand Up @@ -89,6 +89,8 @@ macro dynamic_type(name)
elseif isdefined(Main, name)
# Should this actually be done?
getproperty(Main, name)(what)
elseif startswith(String(name), "cached_")
DynamicObjects.cached(what, Symbol(String(name)[8:end]))
else
# Should this be a different error?
throw(DomainError(name, "Can't resolve attribute $(name). Looked in $($__module__) and Main."))
Expand All @@ -99,13 +101,14 @@ macro dynamic_type(name)
$ename{T}(;kwargs...) where T = $ename{T}((;kwargs...))
Base.show(io::IO, what::$ename{T}) where T = print(io, T, what.nt)
Base.merge(what::$ename, args...) = typeof(what)(merge(what.nt, args...))
DynamicObjects.update(what::$ename; kwargs...) = merge(what, (;kwargs...))
DynamicObjects.update(what::$ename, args...) = merge(what, (;zip(args, getproperty.([what], args))...))
# DynamicObjects.update(what::$ename; kwargs...) = merge(what, (;kwargs...))
DynamicObjects.update(what::$ename, args::Symbol...; kwargs...) = merge(what, (;kwargs...), (;zip(args, getproperty.([what], args))...))
Base.hash(what::$ename{T}, h::UInt=UInt(0)) where T = persistent_hash((what.nt, T), h)
end
end

update(what::AbstractDynamicObject) = what
# update(what::AbstractDynamicObject) = what
update(args::Symbol...; kwargs...) = what->update(what, args...; kwargs...)

"""
DynamicObject{T}
Expand Down Expand Up @@ -141,26 +144,31 @@ The type is inefficient computationally, but can enable more efficient prototypi
# Base.hash(what::DynamicObject{T}, h::Int=0) where T = Base.hash((what.nt, T, h))


function cached(what, key)
get_cache_path() = get(ENV, "DYNAMIC_CACHE", "cache")
set_cache_path!(path::AbstractString) = (ENV["DYNAMIC_CACHE"] = path)
get_cache_path(key::Symbol, what) = joinpath(get_cache_path(), "$(key)_$(typeof(what))_$(what.hash)")
# get_cache_verbosity() = get(ENV, "DYNAMIC_CACHE_VERBOSITY", 0)
# set_cache_verbosity!(path::AbstractString) = (ENV["DYNAMIC_CACHE_VERBOSITY"] = path)

function cached(what, key::Symbol)
if hasproperty(what, key)
# println("LOADING FROM DynamicObject")
getproperty(what, key)
else
if !isdir("cache")
mkdir("cache")
end
file_name = "cache/$(key)_$(typeof(what))_$(what.hash)"
mkpath(get_cache_path())
file_name = get_cache_path(key, what)
if isfile(file_name)
# println("LOADING FROM FILE!")
Serialization.deserialize(file_name)
else
println("Writing result to $(file_name)!")
# println("Writing result to $(file_name)!")
rv = getproperty(what, key)
Serialization.serialize(file_name, rv)
rv
end
end
end
cached(key::Symbol) = x->cached(x, key)

# DynamicObject{T}() where T = DynamicObject{T}(NamedTuple())
# default(what, name) = missing
Expand Down

2 comments on commit 994c19b

@nsiccha
Copy link
Owner Author

@nsiccha nsiccha commented on 994c19b May 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/82880

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.5 -m "<description of version>" 994c19bf4919f87f2e8b045b45e6cea872a66f60
git push origin v0.1.5

Please sign in to comment.