rubypython - 用 ruby 愛上 python

57
資料分析python Honda @ Mosut 2014/05/31

Upload: honda-dai

Post on 27-Aug-2014

693 views

Category:

Software


7 download

DESCRIPTION

How to run python code from ruby? Perhaps this slide can help you :D

TRANSCRIPT

Page 1: Rubypython - 用 ruby 愛上 python

資料分析⽤用python

Honda @ Mosut2014/05/31

Page 2: Rubypython - 用 ruby 愛上 python

Honda Dai (aka Honda)

• 菸酒⽣生 @ 臺⼤大⼯工科所intern @ KKBOX

Page 3: Rubypython - 用 ruby 愛上 python

Honda Dai (aka Honda)

• 菸酒⽣生 @ 臺⼤大⼯工科所intern @ KKBOX

• 喜歡 ruby <3

Page 4: Rubypython - 用 ruby 愛上 python

Honda Dai (aka Honda)

• 菸酒⽣生 @ 臺⼤大⼯工科所intern @ KKBOX

• 喜歡 ruby <3

Page 5: Rubypython - 用 ruby 愛上 python

謝謝⼩小鈞⼤大⼤大

Page 6: Rubypython - 用 ruby 愛上 python

謝謝⼩小鈞⼤大⼤大每個禮拜

Page 7: Rubypython - 用 ruby 愛上 python

謝謝⼩小鈞⼤大⼤大每個禮拜

都問我要不要來MOSUT!!!

Page 8: Rubypython - 用 ruby 愛上 python

⽽而且⼀一拉就拉上台了 XD

Page 9: Rubypython - 用 ruby 愛上 python

所以我今天是來踢館的 (誤)

Page 10: Rubypython - 用 ruby 愛上 python

資料分析⽤用python

Honda @ Mosut2014/05/31

Page 11: Rubypython - 用 ruby 愛上 python

資料分析⽤用python

Honda @ Mosut2014/05/31

⽤用 Ruby 愛上 Python

Page 12: Rubypython - 用 ruby 愛上 python

⼤大家可能都知道

Page 13: Rubypython - 用 ruby 愛上 python

⼤大家可能都知道

Python是⼀一個很適合資料處理的語⾔言

Page 14: Rubypython - 用 ruby 愛上 python

⼤大家可能都知道

Python是⼀一個很適合資料處理的語⾔言

因為有⾮非常多好⽤用的⼯工具!

Page 15: Rubypython - 用 ruby 愛上 python

3rd libraries

Page 16: Rubypython - 用 ruby 愛上 python

3rd libraries

python:

Page 17: Rubypython - 用 ruby 愛上 python

3rd libraries

python:numpy

Page 18: Rubypython - 用 ruby 愛上 python

3rd libraries

python:numpyscipy

Page 19: Rubypython - 用 ruby 愛上 python

3rd libraries

python:numpyscipymatplotlib

Page 20: Rubypython - 用 ruby 愛上 python

3rd libraries

python:numpyscipymatplotlibetc…

Page 21: Rubypython - 用 ruby 愛上 python

3rd libraries

python:numpyscipymatplotlib

Page 22: Rubypython - 用 ruby 愛上 python

3rd libraries

ruby:

Page 23: Rubypython - 用 ruby 愛上 python

3rd libraries

ruby:…

Page 24: Rubypython - 用 ruby 愛上 python

3rd libraries

ruby:……

Page 25: Rubypython - 用 ruby 愛上 python

3rd libraries

ruby:……sciruby

Page 26: Rubypython - 用 ruby 愛上 python

3rd libraries

ruby:……sciruby

Page 27: Rubypython - 用 ruby 愛上 python

Ruby has no equivalent to the beautifully constructed numpy, scipy, and matplotlib libraries for Python

Page 28: Rubypython - 用 ruby 愛上 python

可是...

Page 29: Rubypython - 用 ruby 愛上 python

可是...

前⾯面的code都是 ruby 寫的...

Page 30: Rubypython - 用 ruby 愛上 python

可是...

前⾯面的code都是 ruby 寫的...我不想砍掉重練啊!!

Page 31: Rubypython - 用 ruby 愛上 python

可是...

前⾯面的code都是 ruby 寫的...我不想砍掉重練啊!!⽽而且我就是想⽤用 ruby XD

Page 32: Rubypython - 用 ruby 愛上 python

Honored to Introduce…

Page 33: Rubypython - 用 ruby 愛上 python

Honored to Introduce…

RubyPython

Page 34: Rubypython - 用 ruby 愛上 python

RubyPython

• RubyPython is a bridge between the Ruby and Python interpreters.

• It embeds a running Python interpreter in the Ruby application's process using FFI and provides a means for wrapping, converting, and calling Python objects and methods.

- 官⽅方網站

Page 35: Rubypython - 用 ruby 愛上 python

簡單來說就是

• 在 Ruby 裏⾯面在插⼀一個 Python 的 interpreters

• 也就是說你必須要有 Python 的環境

• 會⾃自動將 Ruby 的 object 與 Python 的互相轉換

Page 36: Rubypython - 用 ruby 愛上 python

怎麼開始⽤用呢?• gem install rubypython

• 確定你有 python

Page 37: Rubypython - 用 ruby 愛上 python

Live Demo在 Ruby 內 call Python 的 method

Page 38: Rubypython - 用 ruby 愛上 python

踩雷時間

Page 39: Rubypython - 用 ruby 愛上 python

Q1: 我要 import 在 python 內⾃自⼰己安裝的 library,為什麼會找不到 ?

Page 40: Rubypython - 用 ruby 愛上 python

• 觀察 Ruby 和 Python 底下的 sys.path,發現竟然不⼀一樣多!

• Ruby

!

!

• Python

Page 41: Rubypython - 用 ruby 愛上 python

解決⽅方法• 總之就是把 python 裡⾯面的 load_path 加進去就可以了

!

• 結果:

ok!

Page 42: Rubypython - 用 ruby 愛上 python

Q2: 我想 call Python 的 method 並且指定參數,為什麼失敗了?

Python:

Ruby:

Page 43: Rubypython - 用 ruby 愛上 python

Q2: 我想 call Python 的 method 並且指定參數,為什麼失敗了?

Python:

Ruby:

我們的意思不是想把 a 變成這個.. XD

Page 44: Rubypython - 用 ruby 愛上 python

原因• 這是 feature 不是 bug XD

• ⽽而且可能還會改

Page 45: Rubypython - 用 ruby 愛上 python

解決⽅方法• 記得在那個 method 後⾯面加上 ! ,變成 method!

ok!

Page 46: Rubypython - 用 ruby 愛上 python

Q3: python 傳回的 tuple / array 在 ruby 內,怎麼不能 multiple assign?

Python:

Ruby:

Page 47: Rubypython - 用 ruby 愛上 python

Q3: python 傳回的 tuple / array 在 ruby 內,怎麼不能 multiple assign?

Python:

Ruby:

怎麼全都在 a 咧?

Page 48: Rubypython - 用 ruby 愛上 python

解決⽅方法• ⽤用 to_a 把它變成 ruby 內的 array 物件就可以了

ok!

Page 49: Rubypython - 用 ruby 愛上 python

意猶未盡嗎?

Page 50: Rubypython - 用 ruby 愛上 python

意猶未盡嗎?

那再讓我講⼀一點 XD

Page 51: Rubypython - 用 ruby 愛上 python

Honored to Introduce…

MatPlotLib for Rubyhttps://github.com/HondaDai/matplotlib-for-ruby

Page 52: Rubypython - 用 ruby 愛上 python

• 研究⽣生做模擬後總是需要跟⼀一堆數據打架

Page 53: Rubypython - 用 ruby 愛上 python

• 研究⽣生做模擬後總是需要跟⼀一堆數據打架

• Ruby 中實在沒有好⼯工具畫出美美的統計圖

Page 54: Rubypython - 用 ruby 愛上 python

• 研究⽣生做模擬後總是需要跟⼀一堆數據打架

• Ruby 中實在沒有好⼯工具畫出美美的統計圖

• 只好⾃自⼰己包⼀一個 :3

Page 55: Rubypython - 用 ruby 愛上 python

Live DemoMatPlotLib for Ruby

Page 56: Rubypython - 用 ruby 愛上 python

結語

• 雖然⽅方便,但並不是⼀一個有效率的⽅方法

• 如果要⽤用在⼤大型專案上,可能要好好考慮

Page 57: Rubypython - 用 ruby 愛上 python

謝謝各位 Q&A