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
# File 'lib/pycall/pyobject_wrapper.rb', line 35

def method_missing(name, *args)
  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
    name_without_last_equal = name.to_s.chomp('=')
    if LibPython::Helpers.hasattr?(__pyptr__, name_without_last_equal)
      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



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

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

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



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

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

#call(*args) ⇒ Object



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

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

#coerce(other) ⇒ Object



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

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

#dup ⇒ Object



151
152
153
154
155
156
157
# File 'lib/pycall/pyobject_wrapper.rb', line 151

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



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

def inspect
  PyCall.builtins.repr(__pyptr__)
end

#kind_of?(cls) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
# File 'lib/pycall/pyobject_wrapper.rb', line 59

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)


54
55
56
57
# File 'lib/pycall/pyobject_wrapper.rb', line 54

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

#to_f ⇒ Object



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

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

#to_i ⇒ Object



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

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

#to_s ⇒ Object



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

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