Skip to content

Commit 6cb256e

Browse files
committed
Rewrite for the latest pycall
1 parent 6eb1ddc commit 6cb256e

File tree

13 files changed

+56
-123
lines changed

13 files changed

+56
-123
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/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').YTick
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').YAxis
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/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

lib/matplotlib/version.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
module Matplotlib
2-
VERSION = "0.1.0.alpha.20170426"
3-
end
1+
MATPLOTLIB_VERSION = "1.0.0.alpha.20170905"

matplotlib.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'matplotlib/version'
55

66
Gem::Specification.new do |spec|
77
spec.name = "matplotlib"
8-
spec.version = Matplotlib::VERSION
8+
spec.version = MATPLOTLIB_VERSION
99
spec.authors = ["Kenta Murata"]
1010
spec.email = ["[email protected]"]
1111

0 commit comments

Comments
 (0)