Building a Julia module to get information about spaceships
Posted on Tue 06 October 2020 in julia
using PkgTemplates
t = Template(;
user="scrambldchannel",
dir="/home/alex/git/",
plugins=[
Git(; manifest=true, ssh=true),
Codecov(),
Develop(),
],
); # semi colons suppress output, it's a matlab thing apparently
Build package scaffold from template¶
This uses the template to create a new package based on the the details supplied.
t("SWAPI")
Using Revise¶
In theory this should let me hack on my module and reflect changes as I go but it's easier to enabled it by default by adding a couple of config files in ~/.julia/config. Note, I got stuck here for a while before realising I wasn't on >= 1.5 - see here for details
using Pkg
# It was at about this point that I started giving up managing the environment in a notebook and moved to the REPl
Pkg.add("Revise")
using Revise
You really want to run it automatically if developing¶
Do it before using the package you want to develop if doing manually.
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.4.1
_/ |\__'_|_|_|\__'_| | Ubuntu ⛬ julia/1.4.1+dfsg-1
|__/ |
~/.julia/config/startup.jl¶
atreplinit() do repl
try
@eval using Revise
@async Revise.wait_steal_repl_backend()
catch e
@warn(e.msg)
end
end
~/.julia/config/startup_ijulia.jl¶
try
@eval using Revise
catch e
@warn(e.msg)
end
Simple module overview¶
SWAPI/src/SWAPI.jl¶
module SWAPI
export request_data
include("request.jl")
end
SWAPI/src/request.jl¶
using HTTP
using JSON
function request_data(url)
response = HTTP.get(url)
return JSON.print(JSON.parse(response), 4)
end
Local packages can be loaded like this¶
Pkg.add(PackageSpec(path="/home/alex/git/SWAPI"))
Developing modules workflow¶
This seemed a bit counter intuitive for me, it clones the repo somewhere else. I'm not sure whether this is actually necessary. Surely I can just edit it in its existing repository?
Pkg.develop("SWAPI")
Note path to SWAPI has changed¶
Pkg.status()
# some warnings here, I need to update the deps for the package
using SWAPI
Now we can get Han Solo baby¶
response = SWAPI.request_data("https://swapi.dev/api/people/14/")
JSON.print(JSON.parse(String(response.body)), 4)
Functions namespace seems to be implicit¶
This call gives the same result as the previous one. Presumably there are precedence rules in cases of namespace clashes. Anyway, let's look at old Ben's stats.
response = request_data("https://swapi.dev/api/people/10/")
JSON.print(JSON.parse(String(response.body)), 4)
But what about the spaceships?!?¶
Sorry, I kinda teased you with that x-wing clickbait. I've managed to mess up this notebook. Working with julia's built in package manager isn't really working for me in there and jumping between too many windows was making my head spin...