Module: PyCall::PyObjectWrapper

Included in:
PyModuleWrapper, PyTypeObjectWrapper, Set
Defined in:
lib/pycall/pyobject_wrapper.rb

Defined Under Namespace

Classes: SwappedOperationAdapter

Constant Summary collapse

OPERATOR_METHOD_NAMES =
{
  :+  => :__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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pycall/pyobject_wrapper.rb', line 29

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 (readonly)

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



78
79
80
# File 'lib/pycall/pyobject_wrapper.rb', line 78

def [](*key)
  LibPython::Helpers.getitem(__pyptr__, key)
end

#[]=(*key, value) ⇒ Object



82
83
84
# File 'lib/pycall/pyobject_wrapper.rb', line 82

def []=(*key, value)
  LibPython::Helpers.setitem(__pyptr__, key, value)
end

#call(*args) ⇒ Object



86
87
88
# File 'lib/pycall/pyobject_wrapper.rb', line 86

def call(*args)
  LibPython::Helpers.call_object(__pyptr__, *args)
end

#coerce(other) ⇒ Object



142
143
144
# File 'lib/pycall/pyobject_wrapper.rb', line 142

def coerce(other)
  [SwappedOperationAdapter.new(other), self]
end

#dupObject



146
147
148
149
150
151
152
# File 'lib/pycall/pyobject_wrapper.rb', line 146

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

#inspectObject



154
155
156
# File 'lib/pycall/pyobject_wrapper.rb', line 154

def inspect
  PyCall.builtins.repr(__pyptr__)
end

#kind_of?(cls) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
# File 'lib/pycall/pyobject_wrapper.rb', line 54

def kind_of?(cls)
  case cls
  when PyTypeObjectWrapper
    __pyptr__.kind_of?(cls.__pyptr__)
  else
    super
  end
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/pycall/pyobject_wrapper.rb', line 49

def respond_to_missing?(name, include_private)
  return true if LibPython::Helpers.hasattr?(__pyptr__, name)
  super
end

#to_fObject



166
167
168
# File 'lib/pycall/pyobject_wrapper.rb', line 166

def to_f
  LibPython::Helpers.call_object(PyCall::builtins.float.__pyptr__, __pyptr__)
end

#to_iObject



162
163
164
# File 'lib/pycall/pyobject_wrapper.rb', line 162

def to_i
  LibPython::Helpers.call_object(PyCall::builtins.int.__pyptr__, __pyptr__)
end

#to_sObject



158
159
160
# File 'lib/pycall/pyobject_wrapper.rb', line 158

def to_s
  LibPython::Helpers.str(__pyptr__)
end