rubypython - 用 ruby 愛上 python
DESCRIPTION
How to run python code from ruby? Perhaps this slide can help you :DTRANSCRIPT
資料分析⽤用python
Honda @ Mosut2014/05/31
Honda Dai (aka Honda)
• 菸酒⽣生 @ 臺⼤大⼯工科所intern @ KKBOX
Honda Dai (aka Honda)
• 菸酒⽣生 @ 臺⼤大⼯工科所intern @ KKBOX
• 喜歡 ruby <3
Honda Dai (aka Honda)
• 菸酒⽣生 @ 臺⼤大⼯工科所intern @ KKBOX
• 喜歡 ruby <3
謝謝⼩小鈞⼤大⼤大
謝謝⼩小鈞⼤大⼤大每個禮拜
謝謝⼩小鈞⼤大⼤大每個禮拜
都問我要不要來MOSUT!!!
⽽而且⼀一拉就拉上台了 XD
所以我今天是來踢館的 (誤)
資料分析⽤用python
Honda @ Mosut2014/05/31
資料分析⽤用python
Honda @ Mosut2014/05/31
⽤用 Ruby 愛上 Python
⼤大家可能都知道
⼤大家可能都知道
Python是⼀一個很適合資料處理的語⾔言
⼤大家可能都知道
Python是⼀一個很適合資料處理的語⾔言
因為有⾮非常多好⽤用的⼯工具!
3rd libraries
3rd libraries
python:
3rd libraries
python:numpy
3rd libraries
python:numpyscipy
3rd libraries
python:numpyscipymatplotlib
3rd libraries
python:numpyscipymatplotlibetc…
3rd libraries
python:numpyscipymatplotlib
3rd libraries
ruby:
3rd libraries
ruby:…
3rd libraries
ruby:……
3rd libraries
ruby:……sciruby
3rd libraries
ruby:……sciruby
Ruby has no equivalent to the beautifully constructed numpy, scipy, and matplotlib libraries for Python
可是...
可是...
前⾯面的code都是 ruby 寫的...
可是...
前⾯面的code都是 ruby 寫的...我不想砍掉重練啊!!
可是...
前⾯面的code都是 ruby 寫的...我不想砍掉重練啊!!⽽而且我就是想⽤用 ruby XD
Honored to Introduce…
Honored to Introduce…
RubyPython
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.
- 官⽅方網站
簡單來說就是
• 在 Ruby 裏⾯面在插⼀一個 Python 的 interpreters
• 也就是說你必須要有 Python 的環境
• 會⾃自動將 Ruby 的 object 與 Python 的互相轉換
怎麼開始⽤用呢?• gem install rubypython
• 確定你有 python
Live Demo在 Ruby 內 call Python 的 method
踩雷時間
Q1: 我要 import 在 python 內⾃自⼰己安裝的 library,為什麼會找不到 ?
• 觀察 Ruby 和 Python 底下的 sys.path,發現竟然不⼀一樣多!
• Ruby
!
!
• Python
解決⽅方法• 總之就是把 python 裡⾯面的 load_path 加進去就可以了
!
• 結果:
ok!
Q2: 我想 call Python 的 method 並且指定參數,為什麼失敗了?
Python:
Ruby:
Q2: 我想 call Python 的 method 並且指定參數,為什麼失敗了?
Python:
Ruby:
我們的意思不是想把 a 變成這個.. XD
原因• 這是 feature 不是 bug XD
• ⽽而且可能還會改
解決⽅方法• 記得在那個 method 後⾯面加上 ! ,變成 method!
ok!
Q3: python 傳回的 tuple / array 在 ruby 內,怎麼不能 multiple assign?
Python:
Ruby:
Q3: python 傳回的 tuple / array 在 ruby 內,怎麼不能 multiple assign?
Python:
Ruby:
怎麼全都在 a 咧?
解決⽅方法• ⽤用 to_a 把它變成 ruby 內的 array 物件就可以了
ok!
意猶未盡嗎?
意猶未盡嗎?
那再讓我講⼀一點 XD
Honored to Introduce…
MatPlotLib for Rubyhttps://github.com/HondaDai/matplotlib-for-ruby
• 研究⽣生做模擬後總是需要跟⼀一堆數據打架
• 研究⽣生做模擬後總是需要跟⼀一堆數據打架
• Ruby 中實在沒有好⼯工具畫出美美的統計圖
• 研究⽣生做模擬後總是需要跟⼀一堆數據打架
• Ruby 中實在沒有好⼯工具畫出美美的統計圖
• 只好⾃自⼰己包⼀一個 :3
Live DemoMatPlotLib for Ruby
結語
• 雖然⽅方便,但並不是⼀一個有效率的⽅方法
• 如果要⽤用在⼤大型專案上,可能要好好考慮
謝謝各位 Q&A