Skip to content

Commit

Permalink
Merge pull request #136 from giordano/load-macro
Browse files Browse the repository at this point in the history
Simplify methods of load macro by reducing code duplication
  • Loading branch information
ablaom committed May 21, 2019
2 parents d636720 + 9f2449b commit ec94538
Showing 1 changed file with 22 additions and 48 deletions.
70 changes: 22 additions & 48 deletions src/loading.jl
Expand Up @@ -153,23 +153,7 @@ function try_to_get_package(model::String)
return (pop!(pkgs), true)
end

macro load(model_ex)
model_ = string(model_ex)

# get rid brackets, as in "(MLJModels.Clustering).KMedoids":
model = filter(model_) do c !(c in ['(',')']) end

if model in string.(MLJBase.finaltypes(Model))
@info "A model named \"$model\" is already loaded.\n"*
"Nothing new loaded. "
return
end

pkg, success = try_to_get_package(model)
if !success # pkg is then a message
error(pkg*"Use @load $model pkg=\"PackageName\". ")
end

function _load(model, pkg, mdl)
# get load path:
info = METADATA[pkg][model]
path = info[:load_path]
Expand All @@ -179,59 +163,49 @@ macro load(model_ex)
# is already loaded into MLJ's namespace):
if path_components[1] == "MLJModels"
print("import MLJModels ")
__module__.eval(:(import MLJModels))
mdl.eval(:(import MLJModels))
println('\u2714')
end

# load the package (triggering lazy-load of implementation code if
# this is in MLJModels):
pkg_ex = Symbol(pkg)
print("import $pkg_ex ")
__module__.eval(:(import $pkg_ex))
mdl.eval(:(import $pkg_ex))
println('\u2714')

# load the model:
load_ex = Meta.parse("import $path")
print(string(load_ex, " "))
__module__.eval(load_ex)
mdl.eval(load_ex)
println('\u2714')

end

macro load(model_ex)
model_ = string(model_ex)

# TODO: elminate duplicate code (above and below are same after "# get
# load path:"

macro load(model_ex, pkg_ex)
model = string(model_ex)
pkg = string(pkg_ex.args[2])
# get rid brackets, as in "(MLJModels.Clustering).KMedoids":
model = filter(model_) do c !(c in ['(',')']) end

# get load path:
info = METADATA[pkg][model]
path = info[:load_path]
path_components = split(path, '.')
if model in string.(MLJBase.finaltypes(Model))
@info "A model named \"$model\" is already loaded.\n"*
"Nothing new loaded. "
return
end

# if needed, put MLJModels in the calling module's namespace (it
# is already loaded into MLJ's namespace):
if path_components[1] == "MLJModels"
print("import MLJModels ")
__module__.eval(:(import MLJModels))
println('\u2714')
pkg, success = try_to_get_package(model)
if !success # pkg is then a message
error(pkg*"Use @load $model pkg=\"PackageName\". ")
end

# load the package (triggering lazy-load of implementation code if
# this is in MLJModels):
pkg_ex = Symbol(pkg)
print("import $pkg_ex ")
__module__.eval(:(import $pkg_ex))
println('\u2714')
_load(model, pkg, __module__)
end

# load the model:
load_ex = Meta.parse("import $path")
print(string(load_ex, " "))
__module__.eval(load_ex)
println('\u2714')
macro load(model_ex, pkg_ex)
model = string(model_ex)
pkg = string(pkg_ex.args[2])

_load(model, pkg, __module__)
end


Expand Down

0 comments on commit ec94538

Please sign in to comment.