Skip to content

Commit 0e005cf

Browse files
authored
Merge pull request #6 from mrkn/rewrite
Rewrite for pycall rewriting
2 parents c71c849 + 3f9ed50 commit 0e005cf

File tree

15 files changed

+87
-124
lines changed

15 files changed

+87
-124
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ source 'https://rubygems.org'
33
# Specify your gem's dependencies in matplotlib.gemspec
44
gemspec
55

6-
gem 'pycall', github: 'mrkn/pycall'
6+
gem 'pycall', github: 'mrkn/pycall', branch: 'rewrite'

lib/matplotlib.rb

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
1-
require "matplotlib/version"
2-
require 'pycall/import'
1+
require 'matplotlib/version'
2+
require 'pycall'
33

4-
module Matplotlib
5-
@matplotlib = PyCall.import_module('matplotlib')
6-
PyCall.dir(@matplotlib).each do |name|
7-
obj = PyCall.getattr(@matplotlib, name)
8-
next unless obj.kind_of?(PyCall::PyObject) || obj.kind_of?(PyCall::PyObjectWrapper)
9-
next unless PyCall.callable?(obj)
10-
11-
define_singleton_method(name) do |*args, **kwargs|
12-
obj.(*args, **kwargs)
13-
end
14-
end
4+
Matplotlib = PyCall.import_module('matplotlib')
155

16-
class << self
17-
def __pyobj__
18-
@matplotlib
19-
end
20-
21-
def method_missing(name, *args, **kwargs)
22-
return super unless PyCall.hasattr?(@matplotlib, name)
23-
PyCall.getattr(@matplotlib, name)
24-
end
25-
end
6+
module Matplotlib
7+
VERSION = MATPLOTLIB_VERSION
8+
Object.class_eval { remove_const :MATPLOTLIB_VERSION }
269

2710
# FIXME: MacOSX backend is unavailable via pycall.
2811
# I don't know why it is.
29-
if Matplotlib.get_backend() == 'MacOSX'
30-
Matplotlib.use('TkAgg')
12+
if get_backend == 'MacOSX'
13+
use('TkAgg')
3114
end
3215

3316
class Error < StandardError
@@ -40,4 +23,4 @@ class Error < StandardError
4023
require 'matplotlib/figure'
4124
require 'matplotlib/spines'
4225

43-
PyCall.append_sys_path(File.expand_path('../matplotlib/python', __FILE__))
26+
PyCall.sys.path.insert(0, File.expand_path('../matplotlib/python', __FILE__))

lib/matplotlib/axes.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
module Matplotlib
2-
class Axes
3-
include PyCall::PyObjectWrapper
4-
wrap_class PyCall.import_module('matplotlib.axes').Axes
5-
end
2+
Axes = PyCall.import_module('matplotlib.axes').Axes
3+
Axes.__send__ :register_python_type_mapping
64
end

lib/matplotlib/axes_3d.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
module Matplotlib
2-
class Axes3D
3-
include PyCall::PyObjectWrapper
4-
wrap_class PyCall.import_module('mpl_toolkits.mplot3d').Axes3D
5-
end
2+
Axes3D = PyCall.import_module('mpl_toolkits.mplot3d').Axes3D
3+
Axes3D.__send__ :register_python_type_mapping
64
end

lib/matplotlib/axis.rb

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
module Matplotlib
2-
class XTick
3-
include PyCall::PyObjectWrapper
4-
wrap_class PyCall.import_module('matplotlib.axis').XTick
5-
end
2+
Axis = PyCall.import_module('matplotlib.axis')
3+
module Axis
4+
XTick = self.XTick
5+
XTick.__send__ :register_python_type_mapping
66

7-
class YTick
8-
include PyCall::PyObjectWrapper
9-
wrap_class PyCall.import_module('matplotlib.axis').XTick
10-
end
7+
YTick = self.YTick
8+
YTick.__send__ :register_python_type_mapping
119

12-
class XAxis
13-
include PyCall::PyObjectWrapper
14-
wrap_class PyCall.import_module('matplotlib.axis').XAxis
15-
end
10+
XAxis = self.XAxis
11+
XAxis.__send__ :register_python_type_mapping
1612

17-
class YAxis
18-
include PyCall::PyObjectWrapper
19-
wrap_class PyCall.import_module('matplotlib.axis').XAxis
13+
YAxis = self.YAxis
14+
YAxis.__send__ :register_python_type_mapping
2015
end
2116
end

lib/matplotlib/figure.rb

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,4 @@
11
module Matplotlib
2-
class Figure
3-
include PyCall::PyObjectWrapper
4-
5-
@__pyobj__ = PyCall.import_module('matplotlib.figure').Figure
6-
7-
PyCall.dir(@__pyobj__).each do |name|
8-
obj = PyCall.getattr(@__pyobj__, name)
9-
next unless obj.kind_of?(PyCall::PyObject) || obj.kind_of?(PyCall::PyObjectWrapper)
10-
next unless PyCall.callable?(obj)
11-
12-
define_method(name) do |*args, **kwargs|
13-
PyCall.getattr(__pyobj__, name).(*args, **kwargs)
14-
end
15-
end
16-
17-
class << self
18-
attr_reader :__pyobj__
19-
20-
def method_missing(name, *args, **kwargs)
21-
return super unless PyCall.hasattr?(__pyobj__, name)
22-
PyCall.getattr(__pyobj__, name)
23-
end
24-
end
25-
26-
PyCall::Conversions.python_type_mapping(__pyobj__, self)
27-
end
2+
Figure = PyCall.import_module('matplotlib.figure').Figure
3+
Figure.__send__ :register_python_type_mapping
284
end

lib/matplotlib/iruby.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'pycall'
1+
require 'matplotlib'
22

33
module Matplotlib
44
module IRuby
@@ -85,17 +85,18 @@ def execute_request(msg)
8585
}.freeze
8686

8787
module Helper
88+
BytesIO = PyCall.import_module('io').BytesIO
89+
8890
def register_formats
89-
bytes_io = PyCall.import_module('io').BytesIO
9091
type { Figure }
9192
AGG_FORMATS.each do |mime, format|
9293
format mime do |fig|
93-
unless fig.canvas.get_supported_filetypes.().has_key?(format)
94+
unless fig.canvas.get_supported_filetypes.has_key?(format)
9495
raise Error, "Unable to display a figure in #{format} format"
9596
end
96-
io = bytes_io.()
97-
fig.canvas.print_figure.(io, format: format, bbox_inches: 'tight')
98-
io.getvalue.()
97+
io = BytesIO.new
98+
fig.canvas.print_figure(io, format: format, bbox_inches: 'tight')
99+
io.getvalue
99100
end
100101
end
101102
end
@@ -237,17 +238,17 @@ def flush_figures
237238
# @param [true, false] close If true, a `plt.close('all')` call is automatically issued after sending all the figures.
238239
def show_figures(close=false)
239240
_pylab_helpers = PyCall.import_module('matplotlib._pylab_helpers')
240-
gcf = PyCall.getattr(_pylab_helpers, :Gcf)
241+
gcf = _pylab_helpers.Gcf
241242
kernel = ::IRuby::Kernel.instance
242-
gcf.get_all_fig_managers.().each do |fig_manager|
243+
gcf.get_all_fig_managers.each do |fig_manager|
243244
data = ::IRuby::Display.display(fig_manager.canvas.figure)
244245
kernel.session.send(:publish, :execute_result,
245246
data: data,
246247
metadata: {},
247248
execution_count: kernel.instance_variable_get(:@execution_count))
248249
end
249250
ensure
250-
unless gcf.get_all_fig_managers.().none?
251+
unless gcf.get_all_fig_managers.nil?
251252
Matplotlib::Pyplot.close('all')
252253
end
253254
end

lib/matplotlib/polar_axes.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
module Matplotlib
2-
class PolarAxes
3-
include PyCall::PyObjectWrapper
4-
wrap_class PyCall.import_module('matplotlib.projections.polar').PolarAxes
5-
end
2+
PolarAxes = PyCall.import_module('matplotlib.projections.polar').PolarAxes
3+
PolarAxes.__send__ :register_python_type_mapping
64
end

lib/matplotlib/pyplot.rb

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
require 'matplotlib'
22

33
module Matplotlib
4+
Pyplot = PyCall.import_module('matplotlib.pyplot')
45
module Pyplot
5-
@pyplot = PyCall.import_module('matplotlib.pyplot')
6-
PyCall.dir(@pyplot).each do |name|
7-
obj = PyCall.getattr(@pyplot, name)
8-
next unless obj.kind_of?(PyCall::PyObject) || obj.kind_of?(PyCall::PyObjectWrapper)
9-
next unless PyCall.callable?(obj)
10-
11-
define_singleton_method(name) do |*args, **kwargs|
12-
obj.(*args, **kwargs)
13-
end
14-
end
15-
16-
class << self
17-
def __pyobj__
18-
@pyplot
19-
end
20-
21-
def xkcd(scale: 1, length: 100, randomness: 2, &block)
22-
PyCall.with(super.(scale: scale, length: length, randomness: randomness), &block)
23-
end
24-
25-
def method_missing(name, *args, **kwargs)
26-
return super unless PyCall.hasattr?(@pyplot, name)
27-
PyCall.getattr(@pyplot, name)
28-
end
6+
def self.xkcd(scale: 1, length: 100, randomness: 2, &block)
7+
ctx = super(scale: scale, length: length, randomness: randomness)
8+
PyCall.with(ctx, &block)
299
end
3010
end
3111
end

lib/matplotlib/spines.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
module Matplotlib
2-
class Spine
3-
include PyCall::PyObjectWrapper
4-
wrap_class PyCall.import_module('matplotlib.spines').Spine
5-
end
2+
Spine = PyCall.import_module('matplotlib.spines').Spine
3+
Spine.__send__ :register_python_type_mapping
64
end

0 commit comments

Comments
 (0)