I'm still touching the Mac Book Pro Retina 13 inch with a doy face in the morning. I wondered if I would do julia today as well. Earlier, I wrote in a Qiita article that Python could also be used.
[Personal memo] Try julia Web Application Framework "Morsel"
I thought I'd give it a try, so I'll give it a try. You can run Python libraries using PyCall.jl.
https://github.com/stevengj/PyCall.jl
Let's try it out.
Execute the following command from Julia's REPL
Pkg.add("PyCall")
This is OK
In the example written in README.md of PyCall.jl I am using the math library.
https://github.com/stevengj/PyCall.jl/blob/ef23e044d4870d3440a76c79d09c85a36c510d06/README.md
using PyCall
@pyimport math
math.sin(math.pi / 4) - sin(pi / 4) # returns 0.0
Try out.
using PyCall
@pyimport re
match_pattern = re.compile("Welcome to (.+\.)")
groups_of_match = pycall(match_pattern["search"], PyObject, "Welcome to underground.")["group"]
result = convert(String, pycall(groups_of_match, PyObject, 0))
match_of_group = convert(String, pycall(groups_of_match, PyObject, 1))
println("All match is [$result]")
println("Match of first regex group is [$match_of_group]")
The execution result is as follows.
This code is equivalent to the following in Python:
import re
match_pattern = re.compile("Welcome to (.+\.)")
groups_of_match = match_pattern.search("Welcome to underground.")
result = groups_of_match.group(0)
match_of_group = groups_of_match.group(1)
print("All match is [{result}]".format(result=result))
print("Match of first regex group is [{match_of_group}]".format(
match_of_group=match_of_group)
)
Execution result
According to Reference
pycall(function::PyObject, returntype::Type, args...)
Since it is written in the format, I did the following for the trial of the regular expression library.
pycall(match_pattern["search"], PyObject, "Welcome to underground.")["group"]
PyObject returns numeric, bool, and functional types. It looks like a convenient object. (The explanation says that PyObject of C API is used)
By calling pycall as above, You can use the group that matches the regular expression and the entire string that matches.
match_pattern["search"]
Is
Get the attributes of the object as described in Description of PyObject. In this case, you are calling the ** search ** method of the regular expression match object.
convert
Convert description is written in the description of PyObject.
convert(T, o::PyObject)
In short, cast. I wanted to make it a string type of julia, so I cast the result of the regular expression (PyObject) to Julia's String type.
convert(String, pycall(groups_of_match, PyObject, 0))
I could easily cast the PyObject type to a String type.
Having PyCall seems to be one of julia's complaints. Since julia's own library number is not so large in the built-in one, I thought it was good to be able to use Python library assets ^-^
Recommended Posts