pybind 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +25 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +132 -0
- data/Rakefile +5 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/autocall_functions.rb +15 -0
- data/examples/hello_world.py +1 -0
- data/examples/hello_world.rb +14 -0
- data/examples/mnist.softmax.rb +53 -0
- data/examples/numpy.rb +9 -0
- data/lib/pybind.rb +14 -0
- data/lib/pybind/autocall.rb +14 -0
- data/lib/pybind/core_ext/basic.rb +66 -0
- data/lib/pybind/error.rb +38 -0
- data/lib/pybind/import.rb +51 -0
- data/lib/pybind/init.rb +23 -0
- data/lib/pybind/libpython.rb +371 -0
- data/lib/pybind/python/investigator.py +10 -0
- data/lib/pybind/struct.rb +36 -0
- data/lib/pybind/typecast.rb +28 -0
- data/lib/pybind/types.rb +21 -0
- data/lib/pybind/types/basic.rb +70 -0
- data/lib/pybind/types/dict.rb +94 -0
- data/lib/pybind/types/function.rb +26 -0
- data/lib/pybind/types/list.rb +39 -0
- data/lib/pybind/types/object.rb +9 -0
- data/lib/pybind/types/sequence.rb +26 -0
- data/lib/pybind/types/set.rb +19 -0
- data/lib/pybind/types/slice.rb +19 -0
- data/lib/pybind/types/tuple.rb +45 -0
- data/lib/pybind/utils.rb +81 -0
- data/lib/pybind/version.rb +3 -0
- data/lib/pybind/wrapper.rb +120 -0
- data/lib/pybind/wrapper/attr_accessor.rb +53 -0
- data/lib/pybind/wrapper/operator.rb +82 -0
- data/lib/pybind/wrapper/rich_comparer.rb +39 -0
- data/pybind.gemspec +40 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4e68107d3828d200f4635f63e539d19cc3ad5892
|
4
|
+
data.tar.gz: 49849daa7fcdb468a477edf86aaa5b31a6ed8473
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '09c51a324b3cc900578604835c0fbcde6fcb060aa53faf0818e92f1bce08378c680976acb72b0a5de762d1d87efab8d8277abd335188405083bb809aebc45af6'
|
7
|
+
data.tar.gz: 938f32fabe4c4f8a81416ea5773f930f36b106bca3679d8852722f1682e73545a4a08a71b89d958447474fc7ec66b1522936c705f6aa201103e2fde546c01053
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
|
4
|
+
rvm:
|
5
|
+
- ruby-head
|
6
|
+
- 2.4.0
|
7
|
+
- 2.3.1
|
8
|
+
- 2.2.5
|
9
|
+
- 2.1.10
|
10
|
+
|
11
|
+
env:
|
12
|
+
- PYTHON=python
|
13
|
+
- PYTHON=python3 LIBPYTHON=wrong_value
|
14
|
+
- LIBPYTHON=/usr/lib/libpython3.2mu.so.1
|
15
|
+
|
16
|
+
addons:
|
17
|
+
apt:
|
18
|
+
packages:
|
19
|
+
- python3
|
20
|
+
- python3-dev
|
21
|
+
- python3-all
|
22
|
+
|
23
|
+
before_install:
|
24
|
+
- gem update --system
|
25
|
+
- gem update bundler
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at bbtfrr@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Theo Li
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# PyBind
|
2
|
+
|
3
|
+
**PyBind.rb** is a lightweight Ruby - Python binding using [`ffi`](https://github.com/ffi/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`.
|
4
|
+
|
5
|
+
More use-cases can be found in `examples` folder.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'pybind'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install pybind
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Hello world with PyBind.rb
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# This program prints Hello, world!
|
29
|
+
require 'pybind'
|
30
|
+
|
31
|
+
# You can eval a string in Python with `PyBind.eval`,
|
32
|
+
# this is the easiest way to use PyBind.rb
|
33
|
+
# and this is equivalent to Python built-in `eval` function
|
34
|
+
PyBind.eval('print("Hello, world!")')
|
35
|
+
|
36
|
+
# Or exec a Python file
|
37
|
+
PyBind.execfile('examples/hello_world.py')
|
38
|
+
|
39
|
+
# You can find all Python built-in functions at `PyBind.builtin`
|
40
|
+
# Note that `PyBind.builtin.print` is a Python function object,
|
41
|
+
# like a `proc` in Ruby, you need to call it by adding a `.` or `.call`
|
42
|
+
# if you don't like it, see `pybind/autocall` secion below
|
43
|
+
PyBind.builtin.print.('hello, world!')
|
44
|
+
```
|
45
|
+
|
46
|
+
Import Python modules
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
require 'pybind'
|
50
|
+
|
51
|
+
os = PyBind.import('os')
|
52
|
+
puts os.name
|
53
|
+
```
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
# or more python-like
|
57
|
+
require 'pybind'
|
58
|
+
include PyBind::Import
|
59
|
+
|
60
|
+
pyimport 'os'
|
61
|
+
puts os.name
|
62
|
+
```
|
63
|
+
|
64
|
+
Customize convertor between Ruby & Python object
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
require 'pybind'
|
68
|
+
|
69
|
+
Fraction = PyBind.import('fractions').Fraction
|
70
|
+
|
71
|
+
class PyFraction
|
72
|
+
include PyBind::PyObjectWrapper
|
73
|
+
pybind_type Fraction
|
74
|
+
end
|
75
|
+
|
76
|
+
f = Fraction.(1, 2)
|
77
|
+
f.kind_of? PyFraction # => true
|
78
|
+
f.numerator # => 1
|
79
|
+
f.denominator # => 2
|
80
|
+
```
|
81
|
+
|
82
|
+
Or you can map Python object to exsisting Ruby class
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
require 'pybind'
|
86
|
+
|
87
|
+
class Rational
|
88
|
+
include PyBind::PyObjectWrapper
|
89
|
+
|
90
|
+
Fraction = PyBind.import('fractions').Fraction
|
91
|
+
|
92
|
+
pybind_type Fraction do |pystruct|
|
93
|
+
# pystruct is a PyObjectStruct, which is a FFI::Struct
|
94
|
+
# This block defines how Python object converts to Ruby object
|
95
|
+
# By default, it's `new(pystruct)`
|
96
|
+
|
97
|
+
# For easily access the attributes, let's convert it to PyObject
|
98
|
+
pyobj = pystruct.to_ruby_object
|
99
|
+
new(pyobj.numerator, pyobj.denominator)
|
100
|
+
end
|
101
|
+
|
102
|
+
def to_python
|
103
|
+
# This block defines how Ruby object converts back to Python object
|
104
|
+
Fraction.(self.numerator, self.denominator)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
If you don't like the dot everywhere before the function call (just like me), you can just `require 'pybind/autocall'`.
|
110
|
+
Note that this will heavily change the behavior of your code, but the life will be easier.
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
require 'pybind'
|
114
|
+
require 'pybind/autocall'
|
115
|
+
|
116
|
+
# No dot anymore, if you need the function object, you need to call
|
117
|
+
# `PyBind.builtin.get_attribute(:print)`
|
118
|
+
PyBind.builtin.print('Hello, world!')
|
119
|
+
```
|
120
|
+
|
121
|
+
## Development
|
122
|
+
|
123
|
+
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.
|
124
|
+
|
125
|
+
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](https://rubygems.org).
|
126
|
+
|
127
|
+
## Contributing
|
128
|
+
|
129
|
+
**PyBind.rb** originally forked from [`pycall`](https://github.com/mrkn/pycall), special thanks goes to Kenta Murata ([`mrkn`](https://github.com/mrkn)) for his brilliant idea.
|
130
|
+
|
131
|
+
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](http://contributor-covenant.org) code of conduct.
|
132
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'pybind'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'pybind'
|
2
|
+
require 'pybind/autocall'
|
3
|
+
|
4
|
+
include PyBind::Import
|
5
|
+
|
6
|
+
# builtin function
|
7
|
+
pyimport 'numpy', as: :np
|
8
|
+
puts np.array([0])
|
9
|
+
|
10
|
+
# type
|
11
|
+
puts np.str('str')
|
12
|
+
|
13
|
+
# function
|
14
|
+
pyimport 'numpy.matlib', as: :ml
|
15
|
+
puts np.zeros([1])
|
@@ -0,0 +1 @@
|
|
1
|
+
print("Hello, world!")
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'pybind'
|
2
|
+
|
3
|
+
# You can eval a string in Python with `PyBind.eval`,
|
4
|
+
# this is the easiest way to use PyBind.rb
|
5
|
+
# and this is equivalent to Python built-in `eval` function
|
6
|
+
PyBind.eval('print("Hello, world!")')
|
7
|
+
|
8
|
+
# Or exec a Python file
|
9
|
+
PyBind.execfile('examples/hello_world.py')
|
10
|
+
|
11
|
+
# You can find all Python built-in functions at PyBind.builtin
|
12
|
+
# Note that `PyBind.builtin.print` is a Python function object,
|
13
|
+
# like a `proc` in Ruby, you need to call it by adding a `.` or `.call`
|
14
|
+
PyBind.builtin.print.('hello, world!')
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'pybind'
|
2
|
+
require 'pybind/autocall'
|
3
|
+
include PyBind::Import
|
4
|
+
|
5
|
+
pyfrom 'tensorflow.examples.tutorials.mnist', import: :input_data
|
6
|
+
pyimport 'tensorflow', as: :tf
|
7
|
+
|
8
|
+
DATA_DIR = '/tmp/tensorflow/mnist/input_data'
|
9
|
+
|
10
|
+
# Import data
|
11
|
+
mnist = input_data.read_data_sets(DATA_DIR, one_hot: true)
|
12
|
+
|
13
|
+
# Create the model
|
14
|
+
x = tf.placeholder(tf.float32, [nil, 784])
|
15
|
+
W = tf.Variable(tf.zeros([784, 10]))
|
16
|
+
b = tf.Variable(tf.zeros([10]))
|
17
|
+
y = tf.matmul(x, W) + b
|
18
|
+
|
19
|
+
# Define loss and optimizer
|
20
|
+
y_ = tf.placeholder(tf.float32, [nil, 10])
|
21
|
+
|
22
|
+
# The raw formulation of cross-entropy,
|
23
|
+
#
|
24
|
+
# tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),
|
25
|
+
# reduction_indices: [1]))
|
26
|
+
#
|
27
|
+
# can be numerically unstable.
|
28
|
+
#
|
29
|
+
# So here we use tf.nn.softmax_cross_entropy_with_logits on the raw
|
30
|
+
# outputs of 'y', and then average across the batch.
|
31
|
+
cross_entropy = tf.reduce_mean(
|
32
|
+
tf.nn.softmax_cross_entropy_with_logits(labels: y_, logits: y))
|
33
|
+
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
|
34
|
+
|
35
|
+
sess = tf.InteractiveSession()
|
36
|
+
tf.global_variables_initializer().run()
|
37
|
+
|
38
|
+
# Train
|
39
|
+
1000.times do
|
40
|
+
batch_xs, batch_ys = mnist.train.next_batch(100)
|
41
|
+
sess.run(train_step, feed_dict: {
|
42
|
+
x => batch_xs, # but what I want is just x => batch_xs,
|
43
|
+
y_ => batch_ys
|
44
|
+
})
|
45
|
+
end
|
46
|
+
|
47
|
+
# Test trained model
|
48
|
+
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
|
49
|
+
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
|
50
|
+
puts(sess.run(accuracy, feed_dict: {
|
51
|
+
x => mnist.test.images,
|
52
|
+
y_ => mnist.test.labels
|
53
|
+
}))
|
data/examples/numpy.rb
ADDED
data/lib/pybind.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'pybind/version'
|
2
|
+
require 'pybind/struct'
|
3
|
+
require 'pybind/libpython'
|
4
|
+
require 'pybind/import'
|
5
|
+
require 'pybind/error'
|
6
|
+
require 'pybind/typecast'
|
7
|
+
require 'pybind/wrapper'
|
8
|
+
require 'pybind/types'
|
9
|
+
require 'pybind/utils'
|
10
|
+
require 'pybind/init'
|
11
|
+
|
12
|
+
module PyBind
|
13
|
+
# Your code goes here...
|
14
|
+
end
|