Module: PyCall::PyObjectWrapper
Defined Under Namespace
Classes: SwappedOperationAdapter
Constant Summary
collapse
- OPERATOR_METHOD_NAMES =
{
:+@ => :__pos__,
:-@ => :__neg__,
:~ => :__invert__,
:+ => :__add__,
:- => :__sub__,
:* => :__mul__,
:/ => :__truediv__,
:% => :__mod__,
:** => :__pow__,
:<< => :__lshift__,
:>> => :__rshift__,
:& => :__and__,
:^ => :__xor__,
:| => :__or__
}.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/pycall/pyobject_wrapper.rb', line 35
def method_missing(name, *args)
name_str = name.to_s if name.kind_of?(Symbol)
name_str.chop! if name_str.end_with?('=')
case name
when *OPERATOR_METHOD_NAMES.keys
op_name = OPERATOR_METHOD_NAMES[name]
if LibPython::Helpers.hasattr?(__pyptr__, op_name)
LibPython::Helpers.define_wrapper_method(self, op_name)
singleton_class.__send__(:alias_method, name, op_name)
return self.__send__(name, *args)
end
else
if LibPython::Helpers.hasattr?(__pyptr__, name_str)
LibPython::Helpers.define_wrapper_method(self, name)
return self.__send__(name, *args)
end
end
super
end
|
Instance Attribute Details
#__pyptr__ ⇒ Object
Returns the value of attribute __pyptr__.
5
6
7
|
# File 'lib/pycall/pyobject_wrapper.rb', line 5
def __pyptr__
@__pyptr__
end
|
Class Method Details
.extend_object(obj) ⇒ Object
7
8
9
10
11
12
13
|
# File 'lib/pycall/pyobject_wrapper.rb', line 7
def self.extend_object(obj)
pyptr = obj.instance_variable_get(:@__pyptr__)
unless pyptr.kind_of? PyPtr
raise TypeError, "@__pyptr__ should have PyCall::PyPtr object"
end
super
end
|
Instance Method Details
#[](*key) ⇒ Object
84
85
86
|
# File 'lib/pycall/pyobject_wrapper.rb', line 84
def [](*key)
LibPython::Helpers.getitem(__pyptr__, key)
end
|
#[]=(*key, value) ⇒ Object
88
89
90
|
# File 'lib/pycall/pyobject_wrapper.rb', line 88
def []=(*key, value)
LibPython::Helpers.setitem(__pyptr__, key, value)
end
|
#dup ⇒ Object
152
153
154
155
156
157
158
|
# File 'lib/pycall/pyobject_wrapper.rb', line 152
def dup
super.tap do |duped|
copied = PyCall.import_module('copy').copy(__pyptr__)
copied = copied.__pyptr__ if copied.kind_of? PyObjectWrapper
duped.instance_variable_set(:@__pyptr__, copied)
end
end
|
#inspect ⇒ Object
160
161
162
|
# File 'lib/pycall/pyobject_wrapper.rb', line 160
def inspect
PyCall.builtins.repr(__pyptr__)
end
|
#kind_of?(cls) ⇒ Boolean
60
61
62
63
64
65
66
67
|
# File 'lib/pycall/pyobject_wrapper.rb', line 60
def kind_of?(cls)
case cls
when PyTypeObjectWrapper
__pyptr__.kind_of?(cls.__pyptr__)
else
super
end
end
|
#respond_to_missing?(name, include_private) ⇒ Boolean
55
56
57
58
|
# File 'lib/pycall/pyobject_wrapper.rb', line 55
def respond_to_missing?(name, include_private)
return true if LibPython::Helpers.hasattr?(__pyptr__, name)
super
end
|
#to_s ⇒ Object
164
165
166
|
# File 'lib/pycall/pyobject_wrapper.rb', line 164
def to_s
LibPython::Helpers.str(__pyptr__)
end
|