Using Packages
It is entirely likely that in the course of writing a package providing a custom data transformer, one would come across packages that may be needed.
Every possibly desired package could be shoved into the list of dependences, but this is a somewhat crude approach. A more granular approach is enabled with two macros, @addpkg
and @import
.
Letting DataToolkitBase know about extra packages
DataToolkitBase.@addpkg
— Macro@addpkg name::Symbol uuid::String
Register the package identified by name
with UUID uuid
. This package may now be used with @import $name
.
All @addpkg statements should lie within a module's __init__
function.
Example
@addpkg CSV "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Using extra packages
DataToolkitBase.@import
— Macro@import pkg1, pkg2...
@import pkg1 as name1, pkg2 as name2...
@import pkg: foo, bar...
@import pkg: foo as bar, bar as baz...
Fetch modules previously registered with @addpkg
, and import them into the current namespace. This macro tries to largely mirror the syntax of using
.
If a required package had to be loaded for the @import
statement, a PkgRequiredRerunNeeded
singleton will be returned.
Example
@import pkg
pkg.dothing(...)
# Alternative form
@import pkg: dothing
dothing(...)
Example
module DataToolkitExample
using DataToolkitBase
using DataFrame
function __init__()
@addpkg CSV "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
@addpkg DelimitedFiles "8bb1440f-4735-579b-a4ab-409b98df4dab"
end
function load(::DataLoader{:csv}, from::IOStream, ::Type{DataFrame})
@import CSV
result = CSV.read(from, DataFrame)
close(from)
result
end
function load(::DataLoader{:delimcsv}, from::IOStream, ::Type{DataFrame})
@import DelimitedFiles
result = DelimitedFiles.readdlm(from, ',', DataFrame)
close(from)
result
end
end