[Personal memo] julia --Using Python library with julia using PyCall

Use Python library

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.

Install PyCall.jl

Execute the following command from Julia's REPL

Pkg.add("PyCall")

This is OK

Try out the Python library

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

Can I use a regular expression library?

Try it for the time being

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.

スクリーンショット 2014-05-27 9.37.12.png

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

スクリーンショット 2014-05-27 9.45.26.png

Functions used, etc.

pycall function

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.

Touch it lightly

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

[Personal memo] julia --Using Python library with julia using PyCall
I tried using the Python library from Ruby with PyCall
The pain of gRPC using Python. November 2019. (Personal memo)
[S3] CRUD with S3 using Python [Python]
Twitter graphing memo with Python
Using Quaternion with Python ~ numpy-quaternion ~
[Python] Using OpenCV with Python (Basic)
Python memo using perl --join
Using OpenCV with Python @Mac
Send using Python with Gmail
Using jupyter notobook with VS Code (mac OS) (personal memo)
Try Juniper JUNOS PyEz (python library) Memo 3 ~ Change settings with PyEz ~
Try Juniper JUNOS PyEz (python library) Memo 2 ~ Get information with PyEz ~
Complement python with emacs using company-jedi
Harmonic mean with Python Harmonic mean (using SciPy)
[Python] Using OpenCV with Python (Image Filtering)
Using Rstan from Python with PypeR
[Python] Using OpenCV with Python (Image transformation)
[Personal memo] Python virtual environment command memo
[Python] Using OpenCV with Python (Edge Detection)
python memo --Specify options with getopt
Input / output with Python (Python learning memo ⑤)
[Personal memo] Python sequence type / mapping type
Notes on using rstrip with python.
[Hyperledger Iroha] Query with Python library
"Scraping & machine learning with Python" Learning memo
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
[Memo] Tweet on twitter with python
[Python] Error and solution memo when using venv with pyenv + anaconda
[AWS] Using ini files with Lambda [Python]
Convert memo at once with Python 2to3
WEB scraping with Python (for personal notes)
Try mathematical formulas using Σ with python
Python memo
Memo to ask for KPI with python
Behind the flyer: Using Docker with Python
python memo
Using Python and MeCab with Azure Databricks
"System trade starting with Python3" reading memo
Socket communication using socketserver with python now
Python memo using perl --format is convenient.
Install python library on Lambda using [/ tmp]
Python memo
A memo with Python2.7 and Python3 on CentOS
Try using Python with Google Cloud Functions
Use cryptography library cryptography with Docker Python image
Check stock prices with slackbot using python
Working with OpenStack using the Python SDK
[Personal note] Web page scraping with python3
Tips for using python + caffe with TSUBAME
python memo
Python memo
Try HTML scraping with a Python library
Python standard library: second half (Python learning memo ⑨)
Publish your own Python library with Homebrew
Automatic PowerPoint generation with python-pptx (personal memo)
I'm using tox and Python 3.3 with Travis-CI
Python memo
Python standard library: First half (Python learning memo ⑧)
Python memo