Module: PyBind::AttrAccessor

Included in:
PyObjectWrapper
Defined in:
lib/pybind/wrapper/attr_accessor.rb

Instance Method Summary collapse

Instance Method Details

#[](*indices) ⇒ Object



35
36
37
38
39
40
# File 'lib/pybind/wrapper/attr_accessor.rb', line 35

def [](*indices)
  key = TypeCast.to_python_arguments(indices)
  value = LibPython.PyObject_GetItem(@pystruct, key)
  raise PyError.fetch if value.null?
  value.to_ruby
end

#[]=(*indices_and_value) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/pybind/wrapper/attr_accessor.rb', line 42

def []=(*indices_and_value)
  value = indices_and_value.pop
  indices = indices_and_value
  key = TypeCast.to_python_arguments(indices)
  value = value.to_python
  ret = LibPython.PyObject_SetItem(@pystruct, pykey, value)
  raise PyError.fetch if ret == -1
  self
end

#get_attribute(name, default = nil) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/pybind/wrapper/attr_accessor.rb', line 3

def get_attribute(name, default = nil)
  value = LibPython.PyObject_GetAttrString(@pystruct, name.to_s)
  if value.null?
    raise PyError.fetch unless default
    return default
  end
  value.to_ruby
end

#has_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/pybind/wrapper/attr_accessor.rb', line 31

def has_attribute?(name)
  LibPython.PyObject_HasAttrString(@pystruct, name.to_s) == 1
end

#remove_attribute(name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pybind/wrapper/attr_accessor.rb', line 19

def remove_attribute(name)
  value = LibPython.PyObject_GetAttrString(@pystruct, name.to_s)
  raise PyError.fetch if value.null?
  ret = if LibPython.respond_to? :PyObject_DelAttrString
      LibPython.PyObject_DelAttrString(@pystruct, name.to_s)
    else
      LibPython.PyObject_SetAttrString(@pystruct, name.to_s, PyBind.None)
    end
  raise PyError.fetch if ret == -1
  value.to_ruby
end

#set_attribute(name, value) ⇒ Object



12
13
14
15
16
17
# File 'lib/pybind/wrapper/attr_accessor.rb', line 12

def set_attribute(name, value)
  value = value.to_python
  ret = LibPython.PyObject_SetAttrString(@pystruct, name.to_s, value)
  raise PyError.fetch if ret == -1
  self
end