How to organize code for a module + scripts

I am generating plots and tables for a paper. I want to organize the relevant code

  1. in a git repo,
  2. containing a module, eg Figures, in a single file,
  3. a bunch of scripts that I can run that call functionality from the module to make the actual plots.

(Why the module? Because Revise.jl makes this so convenient.)

Let’s say that I am working in DIR. Can I somehow have a flat structure like

DIR/Figures/Project.toml
DIR/Figures/Figures.jl
DIR/Figures/scrips.jl

or do I need DIR/Figures/src/Figures.jl? I tried the former but the loader does not find the module unless it is in a src/.

I personally have the following structure:

PaperScript.jl
Manifest.toml
Project.toml
lib
- MyModule.jl (important bit: this needs to be named as [MODULONAME].jl)
- MyModule_file1.jl
- MyModule_file2.jl
- ...

In PaperScript.jl I then have:

cd(@__DIR__)
using Pkg
Pkg.activate(".")
using Revise
# ### Load the model structure (function "luc_model")
push!(LOAD_PATH,joinpath(@__DIR__,"lib"))
using MyModule
...

In MyModule.jl I have:

module MyModule
using Revise

include("MyModule_file1.jl")
include("MyModule_file1.jl")
# [...]
end

In this way I can apply changes in the MyModule files and get them applied (thanks to Revise) to the function calls that I use in PaperScript.jl.

I was using includet earlier, but I come into an issue with nested includet, so I use this approach now…

2 Likes

You can if you fiddle with LOAD_PATH but I would just organize it as a normal package.

Unless you really need to change the current directory you can do

Pkg.activate(@__DIR__)
2 Likes