Engee documentation

Code reuse

When developing Julia programs, it is often necessary to reuse the same code in different projects. In such cases, it is convenient to organize the code in the form of modules or packages.

A module is an easier way to organize your code. It is a file containing functions, variables, and other elements combined into a single namespace (for more information, see Modules). Modules are convenient for local development and code reuse.

A package is a more complex structure that includes not only code, but also metadata such as a project description, dependencies, versions, etc. Packages are maintained by Julia itself through the Pkg package manager, which allows you to manage dependencies and versions.

Unlike packages, modules themselves do not support automatic dependency management via Pkg. This can lead to situations where the module does not work properly if dependencies are not installed or do not match the required versions. Packages, on the other hand, allow Julia to automatically monitor and manage dependencies, making them more reliable and easier to reuse and distribute.

Creating a module

If the goal is to create code and reuse it in the future, then you can limit yourself to your own module. To do this, in the file browser file browser 7 create a file for the future module with the desired name and in the desired directory, for example, the my_module' file.jl on the path `/user/mymodule'.

Use the module command to add the contents of the module, such as the function:

module my_module
    function get_value(x)
        return x
    end
end
In Julia, the module name does not have to match the file name, but when connecting the module using using or import, you need to use exactly the name that is specified inside the module.

Add the module path to the Julia path manually or using the command:

engee.addpath("/user/mymodule")

Use the 'using` operator to load the module.:

using my_module

After loading the module, you can call a function (or any other embedded code) from it:

my_module.get_value(7)
7

Creating a package

To create your own package, first create the basic structure of the project using the 'generate` command. To do this, switch the command prompt to the shell mode (key ;) and go to the directory where you want to create the package. For example:

cd My_libs

# conclusion
/user/My_libs

Generate a new project using the generate command (first switch to the package manager pkg> by clicking ] on the command line):

generate My_lib # a My_lib project will be created with the Project.toml and src/My_lib.jl files.

# conclusion
  Generating  project Test_lib:
    My_lib/Project.toml
    My_lib/src/My_lib.jl

The 'generate` command must be executed once for each new package. This command creates the initial structure of the project, including the files Project.toml and `src/Package Name.jl'. After generation, you can develop the package, add code, configure dependencies, etc.

A Julia project is a structure that includes configuration files and directories necessary for organization, dependency management, and versioning. A package can consist of one or more files and can be uploaded to any Julia project.

The source code of the package will be placed in the src folder. By default, the created package file will contain the code:

module My_lib

greet() = print("Hello World!")

end # module My_lib

Replace the contents of the file with the following code using a different module name:

module My_lib
    function get_value(x)
        return x
    end
end

Next, in Pkg mode, use the develop command to add your project to the current session.:

develop /user/My_libs/My_lib/

# conclusion
   Resolving package versions...
    Updating `~/.project/Project.toml`
  [487ef170] + My_lib v0.1.0 `~/My_libs/My_lib`
    Updating `~/.project/Manifest.toml`
  [487ef170] + My_lib v0.1.0 `~/My_libs/My_lib`

here:

  • The Julia session is the current working environment managed by the Package Manager (Pkg), which defines the available packages and projects for code execution. The session includes the configuration files Project.toml and `Manifest.toml', which capture information about package versions and dependencies, allowing Julia to manage the project and its dependencies.

  • 'Project.toml' — describes the metadata of a package, including its name, version, authors, and dependencies on other packages. This is the main file that helps Julia manage packages and their versions. It is created automatically when using the 'generate` command.

  • Manifest.toml' — captures the exact versions of all dependencies (including their sub-dependencies) of the package. It is created automatically when dependencies are installed or when a package is added via Pkg. It is created automatically when using the 'develop command to connect to the current session.

The 'develop` command must be executed once for each package in the current environment. After that, the package will be added to the environment, and you can use it in your code. If changes are made to the package in the future, they will be automatically available without having to run develop again, as this command creates a symbolic link to the package in the environment.

To use the created package, use the import or using operators:

using My_lib

# or import My_lib

Creating a package using generate provides a structure that facilitates dependency and version management. It also makes it easier to distribute packages.

When developing modules and packages, it may be necessary to restart the kernel in order to restart Julia processes in the current session. To do this, click Feedback kernel reboot 1 in the Engee workspace, and select kernel reboot Reload kernel, or use the command:

engee.clear_all()

To distribute packages, you need to upload package files to a shared repository, which can be used by various hosting companies, for example git.engee.com. Read more about working with a remote repository and uploading local files in our series of articles on Git, the version control system used in Engee. Julia packages can also be registered in the main Julia package registry, the General Registry. To do this, use the tool Registrator.jl.

Packages can be installed using the repository URL, for example for GitHub:

using Pkg
Pkg.add("https://github.com/username/repositoryname")

If the package is successfully registered via Registrar.jl, it can be installed with the standard command:

using Pkg
Pkg.add("LibraryName")