Giter VIP home page Giter VIP logo

pybind.rb's Introduction

PyBind

PyBind.rb is a lightweight Ruby - Python binding using ffi, it aims to create a way to call exsisting Python functions in Ruby. With the power of PyBind.rb, you can use all data-science packages in Python, e.g.: numpy, pandas, matplotlib, and even tensorflow.

More use-cases can be found in examples folder.

Installation

Add this line to your application's Gemfile:

gem 'pybind'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pybind

Usage

Hello world with PyBind.rb

# This program prints Hello, world!
require 'pybind'

# You can eval a string in Python with `PyBind.eval`,
# this is the easiest way to use PyBind.rb
# and this is equivalent to Python built-in `eval` function
PyBind.eval('print("Hello, world!")')

# Or exec a Python file
PyBind.execfile('examples/hello_world.py')

# You can find all Python built-in functions at `PyBind.builtin`
# Note that `PyBind.builtin.print` is a Python function object,
# like a `proc` in Ruby, you need to call it by adding a `.` or `.call`
# if you don't like it, see `pybind/autocall` secion below
PyBind.builtin.print.('hello, world!')

Import Python modules

require 'pybind'

os = PyBind.import('os')
puts os.name
# or more python-like
require 'pybind'
include PyBind::Import

pyimport 'os'
puts os.name

Customize convertor between Ruby & Python object

require 'pybind'

Fraction = PyBind.import('fractions').Fraction

class PyFraction
  include PyBind::PyObjectWrapper
  pybind_type Fraction
end

f = Fraction.(1, 2)
f.kind_of? PyFraction # => true
f.numerator # => 1
f.denominator # => 2

Or you can map Python object to exsisting Ruby class

require 'pybind'

class Rational
  include PyBind::PyObjectWrapper

  Fraction = PyBind.import('fractions').Fraction

  pybind_type Fraction do |pystruct|
    # pystruct is a PyObjectStruct, which is a FFI::Struct
    # This block defines how Python object converts to Ruby object
    # By default, it's `new(pystruct)`

    # For easily access the attributes, let's convert it to PyObject
    pyobj = pystruct.to_ruby_object
    new(pyobj.numerator, pyobj.denominator)
  end

  def to_python
    # This block defines how Ruby object converts back to Python object
    Fraction.(self.numerator, self.denominator)
  end
end

If you don't like the dot everywhere before the function call (just like me), you can just require 'pybind/autocall'. Note that this will heavily change the behavior of your code, but the life will be easier.

require 'pybind'
require 'pybind/autocall'

# No dot anymore, if you need the function object, you need to call
# `PyBind.builtin.get_attribute(:print)`
PyBind.builtin.print('Hello, world!')

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

PyBind.rb originally forked from pycall, special thanks goes to Kenta Murata (mrkn) for his brilliant idea.

Bug reports and pull requests are welcome on GitHub at https://github.com/bbtfr/pybind.rb This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

pybind.rb's People

Contributors

bbtfr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.