Skip to content

Commit 994c19b

Browse files
committed
exposed update and cached
1 parent 00fe89a commit 994c19b

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DynamicObjects"
22
uuid = "23d02862-63fe-4c6e-8fdb-1d52cbbd39d5"
33
authors = ["Nikolas Siccha <nikolassiccha@gmail.com> and contributors"]
4-
version = "0.1.4"
4+
version = "0.1.5"
55

66
[deps]
77
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

examples/_freeze/index/execute-results/html.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"hash": "a030b219fc18d58b27547d2267b43c0f",
2+
"hash": "61154ed732487d9179d206dfd250cb56",
33
"result": {
4-
"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",
4+
"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",
55
"supporting": [
66
"index_files"
77
],
Binary file not shown.

examples/index.qmd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@ println(Rectangle(10).description)
3131
println(Circle(20).description)
3232
Circle(20)
3333
34-
DynamicObjects.cached(Circle(20), :area)
34+
Circle(20).cached_area
35+
36+
Circle(20) |> update(:area) |> cached(:description)
37+
Circle(20) |> update(:area) |> cached(:description)
38+
3539
```

src/DynamicObjects.jl

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module DynamicObjects
2-
export AbstractDynamicObject, DynamicObject, @dynamic_object, @dynamic_type#, update, cached,
2+
export AbstractDynamicObject, DynamicObject, @dynamic_object, @dynamic_type, update, cached
33
import Serialization
44

55

@@ -89,6 +89,8 @@ macro dynamic_type(name)
8989
elseif isdefined(Main, name)
9090
# Should this actually be done?
9191
getproperty(Main, name)(what)
92+
elseif startswith(String(name), "cached_")
93+
DynamicObjects.cached(what, Symbol(String(name)[8:end]))
9294
else
9395
# Should this be a different error?
9496
throw(DomainError(name, "Can't resolve attribute $(name). Looked in $($__module__) and Main."))
@@ -99,13 +101,14 @@ macro dynamic_type(name)
99101
$ename{T}(;kwargs...) where T = $ename{T}((;kwargs...))
100102
Base.show(io::IO, what::$ename{T}) where T = print(io, T, what.nt)
101103
Base.merge(what::$ename, args...) = typeof(what)(merge(what.nt, args...))
102-
DynamicObjects.update(what::$ename; kwargs...) = merge(what, (;kwargs...))
103-
DynamicObjects.update(what::$ename, args...) = merge(what, (;zip(args, getproperty.([what], args))...))
104+
# DynamicObjects.update(what::$ename; kwargs...) = merge(what, (;kwargs...))
105+
DynamicObjects.update(what::$ename, args::Symbol...; kwargs...) = merge(what, (;kwargs...), (;zip(args, getproperty.([what], args))...))
104106
Base.hash(what::$ename{T}, h::UInt=UInt(0)) where T = persistent_hash((what.nt, T), h)
105107
end
106108
end
107109

108-
update(what::AbstractDynamicObject) = what
110+
# update(what::AbstractDynamicObject) = what
111+
update(args::Symbol...; kwargs...) = what->update(what, args...; kwargs...)
109112

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

143146

144-
function cached(what, key)
147+
get_cache_path() = get(ENV, "DYNAMIC_CACHE", "cache")
148+
set_cache_path!(path::AbstractString) = (ENV["DYNAMIC_CACHE"] = path)
149+
get_cache_path(key::Symbol, what) = joinpath(get_cache_path(), "$(key)_$(typeof(what))_$(what.hash)")
150+
# get_cache_verbosity() = get(ENV, "DYNAMIC_CACHE_VERBOSITY", 0)
151+
# set_cache_verbosity!(path::AbstractString) = (ENV["DYNAMIC_CACHE_VERBOSITY"] = path)
152+
153+
function cached(what, key::Symbol)
145154
if hasproperty(what, key)
146155
# println("LOADING FROM DynamicObject")
147156
getproperty(what, key)
148157
else
149-
if !isdir("cache")
150-
mkdir("cache")
151-
end
152-
file_name = "cache/$(key)_$(typeof(what))_$(what.hash)"
158+
mkpath(get_cache_path())
159+
file_name = get_cache_path(key, what)
153160
if isfile(file_name)
154161
# println("LOADING FROM FILE!")
155162
Serialization.deserialize(file_name)
156163
else
157-
println("Writing result to $(file_name)!")
164+
# println("Writing result to $(file_name)!")
158165
rv = getproperty(what, key)
159166
Serialization.serialize(file_name, rv)
160167
rv
161168
end
162169
end
163170
end
171+
cached(key::Symbol) = x->cached(x, key)
164172

165173
# DynamicObject{T}() where T = DynamicObject{T}(NamedTuple())
166174
# default(what, name) = missing

0 commit comments

Comments
 (0)