pycall 1.0.1-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +41 -0
  5. data/CHANGES.md +39 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +91 -0
  9. data/Rakefile +29 -0
  10. data/appveyor.yml +138 -0
  11. data/bin/console +10 -0
  12. data/bin/guard +17 -0
  13. data/bin/rspec +17 -0
  14. data/bin/runner +6 -0
  15. data/bin/setup +8 -0
  16. data/config/Guardfile +30 -0
  17. data/docker/Dockerfile +191 -0
  18. data/docker/Gemfile +12 -0
  19. data/docker/README.md +22 -0
  20. data/examples/classifier_comparison.rb +135 -0
  21. data/examples/datascience_rb_20170519.ipynb +4836 -0
  22. data/examples/hist.rb +32 -0
  23. data/examples/notebooks/classifier_comparison.ipynb +226 -0
  24. data/examples/notebooks/forest_importances.ipynb +238 -0
  25. data/examples/notebooks/iruby_integration.ipynb +183 -0
  26. data/examples/notebooks/lorenz_attractor.ipynb +214 -0
  27. data/examples/notebooks/polar_axes.ipynb +209 -0
  28. data/examples/notebooks/sum_benchmarking.ipynb +374 -0
  29. data/examples/notebooks/xkcd_style.ipynb +149 -0
  30. data/examples/plot_forest_importances_faces.rb +46 -0
  31. data/examples/sum_benchmarking.rb +49 -0
  32. data/ext/pycall/extconf.rb +3 -0
  33. data/ext/pycall/gc.c +74 -0
  34. data/ext/pycall/libpython.c +217 -0
  35. data/ext/pycall/pycall.c +2184 -0
  36. data/ext/pycall/pycall_internal.h +700 -0
  37. data/ext/pycall/range.c +69 -0
  38. data/ext/pycall/ruby_wrapper.c +432 -0
  39. data/lib/2.1/pycall.so +0 -0
  40. data/lib/2.2/pycall.so +0 -0
  41. data/lib/2.3/pycall.so +0 -0
  42. data/lib/2.4/pycall.so +0 -0
  43. data/lib/pycall/conversion.rb +173 -0
  44. data/lib/pycall/dict.rb +48 -0
  45. data/lib/pycall/error.rb +10 -0
  46. data/lib/pycall/gc_guard.rb +84 -0
  47. data/lib/pycall/import.rb +120 -0
  48. data/lib/pycall/init.rb +55 -0
  49. data/lib/pycall/iruby_helper.rb +40 -0
  50. data/lib/pycall/libpython/finder.rb +170 -0
  51. data/lib/pycall/libpython/pyobject_struct.rb +30 -0
  52. data/lib/pycall/libpython/pytypeobject_struct.rb +273 -0
  53. data/lib/pycall/libpython.rb +12 -0
  54. data/lib/pycall/list.rb +45 -0
  55. data/lib/pycall/pretty_print.rb +9 -0
  56. data/lib/pycall/pyerror.rb +30 -0
  57. data/lib/pycall/pyobject_wrapper.rb +212 -0
  58. data/lib/pycall/python/PyCall/__init__.py +1 -0
  59. data/lib/pycall/python/PyCall/six.py +23 -0
  60. data/lib/pycall/python/investigator.py +7 -0
  61. data/lib/pycall/pytypeobject_wrapper.rb +90 -0
  62. data/lib/pycall/set.rb +19 -0
  63. data/lib/pycall/slice.rb +8 -0
  64. data/lib/pycall/tuple.rb +46 -0
  65. data/lib/pycall/version.rb +3 -0
  66. data/lib/pycall/wrapper_object_cache.rb +61 -0
  67. data/lib/pycall.rb +91 -0
  68. data/pycall.gemspec +40 -0
  69. data/tasks/docker.rake +21 -0
  70. data/tasks/pycall.rake +7 -0
  71. metadata +228 -0
data/lib/pycall/set.rb ADDED
@@ -0,0 +1,19 @@
1
+ module PyCall
2
+ class Set
3
+ include PyObjectWrapper
4
+
5
+ def initialize(pyobj)
6
+ super(pyobj)
7
+ end
8
+
9
+ def size
10
+ LibPython.PySet_Size(__pyobj__)
11
+ end
12
+
13
+ alias length size
14
+
15
+ def include?(obj)
16
+ 1 == LibPython.PySet_Contains(__pyobj__, Conversions.from_ruby(obj))
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ module PyCall
2
+ Slice = builtins.slice
3
+ class Slice
4
+ def self.all
5
+ new(nil)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,46 @@
1
+ module PyCall
2
+ class Tuple
3
+ include PyObjectWrapper
4
+
5
+ def self.new(init)
6
+ case init
7
+ when Integer
8
+ super(LibPython.PyTuple_New(init))
9
+ when Array
10
+ tuple = new(init.length)
11
+ init.each_with_index do |obj, index|
12
+ tuple[index] = obj
13
+ end
14
+ tuple
15
+ when LibPython::PyObjectStruct
16
+ super(init)
17
+ end
18
+ end
19
+
20
+ # Make tuple from array
21
+ def self.[](*ary)
22
+ new(ary)
23
+ end
24
+
25
+ def size
26
+ LibPython.PyTuple_Size(__pyobj__)
27
+ end
28
+
29
+ alias length size
30
+
31
+ def [](index)
32
+ LibPython.PyTuple_GetItem(__pyobj__, index).to_ruby
33
+ end
34
+
35
+ def []=(index, value)
36
+ value = Conversions.from_ruby(value)
37
+ LibPython.PyTuple_SetItem(__pyobj__, index, value)
38
+ end
39
+
40
+ def to_a
41
+ Array.new(length) {|i| self[i] }
42
+ end
43
+
44
+ alias to_ary to_a
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module PyCall
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,61 @@
1
+ module PyCall
2
+ class WrapperObjectCache
3
+ def initialize(*restricted_pytypes)
4
+ unless restricted_pytypes.empty?
5
+ restricted_pytypes.each do |pytype|
6
+ next if pytype.kind_of? PyTypePtr
7
+ raise TypeError, "unexpected type of object in the arguments (#{pytype.class} for PyCall::PyTypePtr)"
8
+ end
9
+ end
10
+ @restricted_pytypes = restricted_pytypes
11
+ @wrapper_object_table = {}
12
+ @wrapped_pyptr_table = {}
13
+ @weakref_table = {}
14
+ end
15
+
16
+ def lookup(pyptr)
17
+ # TODO: check pytypeptr type
18
+ unless pyptr.kind_of? PyPtr
19
+ raise TypeError, "unexpected argument type #{pyptr.class} (expected PyCall::PyPtr)"
20
+ end
21
+
22
+ unless @restricted_pytypes.empty?
23
+ unless @restricted_pytypes.any? {|pytype| pyptr.kind_of? pytype }
24
+ raise TypeError, "unexpected argument Python type #{pyptr.__ob_type__.__name__} (expected either of them in [#{@restricted_pytypes.map(&:__tp_name__).join(', ')}])"
25
+ end
26
+ end
27
+
28
+ wrapper_object_id = @wrapper_object_table[pyptr.__address__]
29
+ if wrapper_object_id
30
+ wrapper_object = ObjectSpace._id2ref(wrapper_object_id) rescue nil
31
+ return wrapper_object if wrapper_object
32
+ end
33
+
34
+ wrapper_object = yield(pyptr)
35
+ check_wrapper_object(wrapper_object)
36
+ register_wrapper_object(pyptr, wrapper_object)
37
+
38
+ wrapper_object
39
+ end
40
+
41
+ def check_wrapper_object(wrapper_object)
42
+ unless wrapper_object.kind_of?(PyObjectWrapper)
43
+ raise TypeError, "unexpected wrapper object (expected an object extended by PyObjectWrapper)"
44
+ end
45
+ end
46
+
47
+ def register_wrapper_object(pyptr, wrapper_object)
48
+ @wrapper_object_table[pyptr.__address__] = wrapper_object.__id__
49
+ @wrapped_pyptr_table[wrapper_object.__id__] = pyptr.__address__
50
+ ObjectSpace.define_finalizer(wrapper_object, &method(:unregister_wrapper_object))
51
+ # TODO: weakref
52
+ self
53
+ end
54
+
55
+ def unregister_wrapper_object(wrapper_object_id)
56
+ pyptr_addr = @wrapped_pyptr_table.delete(wrapper_object_id)
57
+ @wrapper_object_table.delete(pyptr_addr) if pyptr_addr
58
+ self
59
+ end
60
+ end
61
+ end
data/lib/pycall.rb ADDED
@@ -0,0 +1,91 @@
1
+ module PyCall
2
+ require 'pycall/version'
3
+ require 'pycall/libpython'
4
+ require 'pycall/pyerror'
5
+ require 'pycall/pyobject_wrapper'
6
+ require 'pycall/pytypeobject_wrapper'
7
+ require 'pycall/init'
8
+
9
+ module_function
10
+
11
+ def builtins
12
+ @builtins ||= wrap_module(LibPython::API.builtins_module_ptr)
13
+ end
14
+
15
+ def callable?(obj)
16
+ case obj
17
+ when PyObjectWrapper
18
+ builtins.callable(obj.__pyptr__)
19
+ when PyPtr
20
+ builtins.callable(obj)
21
+ else
22
+ raise TypeError, "unexpected argument type #{obj.class} (expected PyCall::PyPtr or its wrapper)"
23
+ end
24
+ end
25
+
26
+ def dir(obj)
27
+ case obj
28
+ when PyObjectWrapper
29
+ builtins.dir(obj.__pyptr__)
30
+ when PyPtr
31
+ builtins.dir(obj)
32
+ else
33
+ raise TypeError, "unexpected argument type #{obj.class} (expected PyCall::PyPtr or its wrapper)"
34
+ end
35
+ end
36
+
37
+ def eval(expr, globals: nil, locals: nil)
38
+ globals ||= import_module(:__main__).__dict__
39
+ builtins.eval(expr, globals, locals)
40
+ end
41
+
42
+ def exec(code, globals: nil, locals: nil)
43
+ globals ||= import_module(:__main__).__dict__
44
+ if PYTHON_VERSION >= '3'
45
+ builtins.exec(code, globals, locals)
46
+ else
47
+ import_module('PyCall.six').exec_(code, globals, locals)
48
+ end
49
+ end
50
+
51
+ def import_module(name)
52
+ LibPython::Helpers.import_module(name)
53
+ end
54
+
55
+ def len(obj)
56
+ case obj
57
+ when PyObjectWrapper
58
+ builtins.len(obj.__pyptr__)
59
+ when PyPtr
60
+ builtins.len(obj)
61
+ else
62
+ raise TypeError, "unexpected argument type #{obj.class} (expected PyCall::PyPtr or its wrapper)"
63
+ end
64
+ end
65
+
66
+ def sys
67
+ @sys ||= import_module('sys')
68
+ end
69
+
70
+ def tuple(iterable=nil)
71
+ pyptr = if iterable
72
+ builtins.tuple.(iterable)
73
+ else
74
+ builtins.tuple.()
75
+ end
76
+ Tuple.wrap_pyptr(pyptr)
77
+ end
78
+
79
+ def with(ctx)
80
+ begin
81
+ yield ctx.__enter__
82
+ rescue PyError => err
83
+ raise err unless ctx.__exit__(err.type, err.value, err.traceback)
84
+ rescue Exception => err
85
+ # TODO: support telling what exception has been catched
86
+ raise err unless ctx.__exit__(err.class, err, err.backtrace_locations)
87
+ else
88
+ ctx.__exit__(nil, nil, nil)
89
+ end
90
+ end
91
+ end
data/pycall.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pycall/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pycall"
8
+ spec.version = PyCall::VERSION
9
+ spec.authors = ["Kenta Murata"]
10
+ spec.email = ["mrkn@mrkn.jp"]
11
+
12
+ spec.summary = %q{pycall}
13
+ spec.description = %q{pycall}
14
+ spec.homepage = "https://github.com/mrkn/pycall"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ case f
19
+ when %r{^Guardfile}, # NOTE: Skip symlink for Windows
20
+ %r{^(test|spec|features)/}
21
+ true
22
+ else
23
+ false
24
+ end
25
+ end
26
+
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+ spec.extensions = ["ext/pycall/extconf.rb"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.13"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rake-compiler"
35
+ spec.add_development_dependency "rake-compiler-dock"
36
+ spec.add_development_dependency "rspec", "~> 3.0"
37
+ spec.add_development_dependency "launchy"
38
+ spec.add_development_dependency "pry"
39
+ spec.add_development_dependency "pry-byebug"
40
+ end
data/tasks/docker.rake ADDED
@@ -0,0 +1,21 @@
1
+ namespace :docker do
2
+ task :build do
3
+ Dir.chdir File.expand_path('../..', __FILE__) do
4
+ system "docker build -f docker/Dockerfile -t rubydata/pycall ."
5
+ end
6
+ end
7
+
8
+ desc 'Run docker container [port=8888] [attach_local=$(pwd)]'
9
+ task :run do
10
+ require 'securerandom'
11
+ require 'launchy'
12
+ token = SecureRandom.hex(48)
13
+ port = ENV['port'] || '8888'
14
+ attach_local = File.expand_path(ENV['attach_local'] || Dir.pwd)
15
+ Thread.start do
16
+ sleep 3
17
+ Launchy.open("http://localhost:#{port}/?token=#{token}")
18
+ end
19
+ system "docker run -it -e 'JUPYTER_TOKEN=#{token}' -v #{attach_local}:/notebooks/local -p #{port}:8888 --rm --name pycall rubydata/pycall"
20
+ end
21
+ end
data/tasks/pycall.rake ADDED
@@ -0,0 +1,7 @@
1
+ namespace :pycall do
2
+ desc 'Show PYTHON_DESCRIPTION'
3
+ task :PYTHON_DESCRIPTION do
4
+ require 'pycall'
5
+ puts PyCall::PYTHON_DESCRIPTION
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,228 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pycall
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: x86-mingw32
6
+ authors:
7
+ - Kenta Murata
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake-compiler-dock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: launchy
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: pycall
126
+ email:
127
+ - mrkn@mrkn.jp
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - CHANGES.md
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - appveyor.yml
141
+ - bin/console
142
+ - bin/guard
143
+ - bin/rspec
144
+ - bin/runner
145
+ - bin/setup
146
+ - config/Guardfile
147
+ - docker/Dockerfile
148
+ - docker/Gemfile
149
+ - docker/README.md
150
+ - examples/classifier_comparison.rb
151
+ - examples/datascience_rb_20170519.ipynb
152
+ - examples/hist.rb
153
+ - examples/notebooks/classifier_comparison.ipynb
154
+ - examples/notebooks/forest_importances.ipynb
155
+ - examples/notebooks/iruby_integration.ipynb
156
+ - examples/notebooks/lorenz_attractor.ipynb
157
+ - examples/notebooks/polar_axes.ipynb
158
+ - examples/notebooks/sum_benchmarking.ipynb
159
+ - examples/notebooks/xkcd_style.ipynb
160
+ - examples/plot_forest_importances_faces.rb
161
+ - examples/sum_benchmarking.rb
162
+ - ext/pycall/extconf.rb
163
+ - ext/pycall/gc.c
164
+ - ext/pycall/libpython.c
165
+ - ext/pycall/pycall.c
166
+ - ext/pycall/pycall_internal.h
167
+ - ext/pycall/range.c
168
+ - ext/pycall/ruby_wrapper.c
169
+ - lib/2.1/pycall.so
170
+ - lib/2.2/pycall.so
171
+ - lib/2.3/pycall.so
172
+ - lib/2.4/pycall.so
173
+ - lib/pycall.rb
174
+ - lib/pycall/conversion.rb
175
+ - lib/pycall/dict.rb
176
+ - lib/pycall/error.rb
177
+ - lib/pycall/gc_guard.rb
178
+ - lib/pycall/import.rb
179
+ - lib/pycall/init.rb
180
+ - lib/pycall/iruby_helper.rb
181
+ - lib/pycall/libpython.rb
182
+ - lib/pycall/libpython/finder.rb
183
+ - lib/pycall/libpython/pyobject_struct.rb
184
+ - lib/pycall/libpython/pytypeobject_struct.rb
185
+ - lib/pycall/list.rb
186
+ - lib/pycall/pretty_print.rb
187
+ - lib/pycall/pyerror.rb
188
+ - lib/pycall/pyobject_wrapper.rb
189
+ - lib/pycall/python/PyCall/__init__.py
190
+ - lib/pycall/python/PyCall/six.py
191
+ - lib/pycall/python/investigator.py
192
+ - lib/pycall/pytypeobject_wrapper.rb
193
+ - lib/pycall/set.rb
194
+ - lib/pycall/slice.rb
195
+ - lib/pycall/tuple.rb
196
+ - lib/pycall/version.rb
197
+ - lib/pycall/wrapper_object_cache.rb
198
+ - pycall.gemspec
199
+ - tasks/docker.rake
200
+ - tasks/pycall.rake
201
+ homepage: https://github.com/mrkn/pycall
202
+ licenses:
203
+ - MIT
204
+ metadata: {}
205
+ post_install_message:
206
+ rdoc_options: []
207
+ require_paths:
208
+ - lib
209
+ required_ruby_version: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - ">="
212
+ - !ruby/object:Gem::Version
213
+ version: '2.1'
214
+ - - "<"
215
+ - !ruby/object:Gem::Version
216
+ version: '2.5'
217
+ required_rubygems_version: !ruby/object:Gem::Requirement
218
+ requirements:
219
+ - - ">="
220
+ - !ruby/object:Gem::Version
221
+ version: '0'
222
+ requirements: []
223
+ rubyforge_project:
224
+ rubygems_version: 2.6.12
225
+ signing_key:
226
+ specification_version: 4
227
+ summary: pycall
228
+ test_files: []