.jl files: application scenarios and features¶
This review is about .jl
files. Specifically, such applications as:
- creating custom modules
- using certain scripts
- code debugging
- running server applications
Creating custom modules¶
Unlike .ngscript
files , files with the extension .jl
can be used to create custom modules.
Let's consider 2 files.
MyModule.jl
_MyScriptModule.ngscript
Both files include modules with the same content: MyModule
and MyScriptModule
respectively.
But for the case of .jl
-file we can connect the module using:
include("MyModule.jl")
using .MyModule
area(Rectangle(2,3))
But using .ngscript
, it is no longer 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¶
Let's imagine that there is a table of students and their repositories with homework assignments.
| Surname | I. | O. | Git repository | | ------- | --- | --- | --------------------------------------- | | Ivanov | I. | I. | git@github.com:Ivanov/Julia_course.git | git@github.com:Ivanov/Julia_course.git | | Pavlov | P. | P. | | | | Petrov | P. | P. | P. | git@github.com:Petrov/Julia_course.git | | Sidorov | I. | P. | git@github.com:Sidorov/Julia_course.git | git@github.com:Sidorov/Julia_course.git |
A teacher teaches a subject to several groups at the same time.
Then, using .jl
files, you can check homework assignments,
by copying all the files automatically to the same directory
where the corresponding .jl
file is located.
Note that there are no folders in the directory now.
Let's recreate this situation by executing the cell below. After executing the cell code, the folders will appear in your directory:
jl-files
├─── Ivanov
│ └─── Seminar_1
├─── Petrov
│ └─── Seminar_1
└──── Sidorov
└─── Seminar_1
A description to what is being executed will be given in the next cell.
run(`bash -c """
julia generate_student_folders.jl students_group_1.csv Семинар_1
"""`)
- The function
run
can run commands. bash -c
means "execute command in the bash shell".julia
-juila программа.jl аргумент_1 аргумент_2 ...
generate_student_folders.jl
- file that creates folders and clones student repositories into themARGS[1]
- name of the file containing the data (students_group_1.csv
)ARGS[2]
- name of the class/work to check(Семинар_1
)
Why not replace the generate_student_folders.jl
script with a similar function?¶
The convenience of using the script is that within it, unlike the function, you can use using
and import
.
That is, we can call a separate process $\rightarrow$, connect packages $\rightarrow$ in it, execute actions $\rightarrow$ and return back to our main programme.
Note that we used the package
CSV
inside the programmegenerate_student_folders.jl
, but this package is not connected in our script.
try
df = CSV.read("students_group_1.csv", DataFrame);
catch e
sprint(showerror,e)
end
Code debugging¶
Although code debugging is also possible in scripts, .jl
files have an advantage over them. And here's why:
.ngscript
represents a json
-file.
For example, a _sqrt_16.ngscript
file is a json
-file, inside of which there is
# 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)"
],
So, debugging such a file is actually a non-trivial task. Let's make sure of that
Debugging .ngscript
files¶
Let's create the function circle_area
in the current .ngscript
-file.
"Площадь круга по радиусу"
function circle_area(r)
π*r^2
end
Using @functionloc
we can find out the name of the file and the string of the method to be called.
@functionloc circle_area(2)
As we can see, the file name is not returned, and the string is the 2nd line of the cell (after the description of our function "Circle area by radius").
Debugging .jl
-files¶
However, there is no such problem for .jl
-files. And the position of the function area
declared in the file MyModule.jl
is printed quite clearly:
@functionloc area(Rectangle(2,3))