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 =
{
  # Unary operators
  :+@ => :__pos__,
  :-@ => :__neg__,
  :~  => :__invert__,

  # Binary operators
  :+  => :__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 (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



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

#call(*args) ⇒ Object



92
93
94
# File 'lib/pycall/pyobject_wrapper.rb', line 92

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

#coerce(other) ⇒ Object



148
149
150
# File 'lib/pycall/pyobject_wrapper.rb', line 148

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

#dupObject



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

#inspectObject



160
161
162
# File 'lib/pycall/pyobject_wrapper.rb', line 160

def inspect
  PyCall.builtins.repr(__pyptr__)
end

#kind_of?(cls) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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_fObject



172
173
174
# File 'lib/pycall/pyobject_wrapper.rb', line 172

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

#to_iObject



168
169
170
# File 'lib/pycall/pyobject_wrapper.rb', line 168

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

#to_sObject



164
165
166
# File 'lib/pycall/pyobject_wrapper.rb', line 164

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