.jl files: application scenarios and features
The review is dedicated to .jl files. Namely, applications such as:
- creation of custom modules
- using certain scenarios
- debugging the code
- launching server applications
Creating custom modules
Unlike files .ngscript, files with the extension .jl you can use it to create your own modules.
Consider 2 files.
MyModule.jl_MyScriptModule.ngscript
Both files include modules with the same contents.: MyModule and MyScriptModule accordingly.
But for the case of .jl-then we can connect the module using:
include("MyModule.jl")
using .MyModule
area(Rectangle(2,3))
But, using .ngscript, it will no longer be possible to connect the module:
try
include("_MyScriptModule.ngscript")
using .MyScriptModule
area(Rectangle(2,3))
catch e
println(e)
end
Automating the execution of actions
Imagine that there is a table of students and their repositories with homework.
| Last name | And . | O. | Git repository |
|---|---|---|---|
| Ivanov | I. | And. | git@github.com:Ivanov/Julia_course.git |
| Pavlov | P. | P. | |
| Petrov | P. | P. | git@github.com:Petrov/Julia_course.git |
| Sidorov | I. | P. | git@github.com:Sidorov/Julia_course.git |
The teacher teaches a certain subject in several groups at once.
Then, using .jl-Files can be checked for homework
by copying all files automatically to the directory
where the corresponding file is located. .jl- the file.
Please note that there are no folders in the directory right now.
Let's recreate this situation by executing the cell below.
After executing the cell code, folders will appear in your directory:
jl-files
├── Ivanov
,── Seminar_1
,── Petrov
,── Seminar_1
,── Sidorov
,── Seminar_1
`` A
description of what is being performed will be given in the next cell.
run(`bash -c """
julia generate_student_folders.jl students_group_1.csv Семинар_1
"""`)
- Function
runIt can run commands. bash -cmeans "execute command in bash shell".julia-juila программа.jl аргумент_1 аргумент_2 ...generate_student_folders.jl- a file that creates folders and clones student repositories in themARGS[1]- the name of the file containing the data (students_group_1.csv)ARGS[2]- the name of the activity/job that needs to be checked(Семинар_1)
Why not replace the script generate_student_folders.jl a similar function?
The convenience of using a script is that inside it, unlike функции , you can use using and import.
That is, we can call a separate process. connect packages in it perform actions and go back to our main program.
Note that we have used the package
CSVinside the programgenerate_student_folders.jlbut this package is not connected in our script
try
df = CSV.read("students_group_1.csv", DataFrame);
catch e
sprint(showerror,e)
end
Debugging the code
Despite the fact that debugging code is also possible in scripts., .jl files have an advantage over them. And here's why:
.ngscript represents json- the file.
For example, the file _sqrt_16.ngscript represents json-a file that contains
# the part of the file about the first text cell
"id": "ffe36da1",
"cell_type": "markdown",
"source": [
"Value $\\sqrt{16}$"
],
# part of the code about the first code cell
"id": "a4b6a26f",
"cell_type": "code",
"source": [
"sqrt(16)"
],
That is, in fact, debugging such a file is not a trivial task.
Let's make sure of that.
Debugging .ngscript files
Creating a function circle_area in the current .ngscript- the file.
"Площадь круга по радиусу"
function circle_area(r)
π*r^2
end
Using @functionloc you can find out the file name and the string of the method to be called.
@functionloc circle_area(2)
As you can see, the file name has not been returned, and the 2nd row of the cell is indicated as the row (after describing our function "The area is steep in radius").
Debugging .jl-files
However, for .jl- there is no such problem in the files. And the position declared in the file MyModule.jl functions area it is deduced quite definitely:
@functionloc area(Rectangle(2,3))
.jl-files as part of applications
To develop applications using, for example, Genie, it is necessary to create exactly inside the application directory .jl files, not scripts.
So, the main file of the application should be a file with the name app.jl.