Class: PyBind::PyDict

Inherits:
Object show all
Includes:
Enumerable, PyObjectWrapper
Defined in:
lib/pybind/types/dict.rb

Constant Summary

Constants included from Operator

Operator::BINARY_OPERATION_OPFUNCS, Operator::UNARY_OPERATION_OPFUNCS

Constants included from RichComparer

RichComparer::Py_EQ, RichComparer::Py_GE, RichComparer::Py_GT, RichComparer::Py_LE, RichComparer::Py_LT, RichComparer::Py_NE, RichComparer::RICH_COMPARISON_OPCODES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PyObjectWrapper

#autocall_method_missing, #call, included, #initialize, #inspect, #methods, #python_type, #to_python_struct, #to_s

Methods included from Operator

#!, #**, #<=>, #===, #__binary_operate__, #__unary_operate__

Methods included from RichComparer

#__rich_compare__

Methods included from AttrAccessor

#get_attribute, #has_attribute?, #remove_attribute, #set_attribute

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class PyBind::PyObjectWrapper

Class Method Details

.new(init = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pybind/types/dict.rb', line 7

def self.new(init = nil)
  case init
  when PyObjectStruct
    super
  when nil
    new(LibPython.PyDict_New())
  when Hash
    new.tap do |dict|
      init.each do |key, value|
        dict[key] = value
      end
    end
  else
    raise TypeError, "the argument must be a PyObjectStruct or a Hash"
  end
end

Instance Method Details

#[](key) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/pybind/types/dict.rb', line 24

def [](key)
  case key
  when String, Symbol
    LibPython.PyDict_GetItemString(@pystruct, key.to_s).to_ruby
  else
    key = key.to_python
    LibPython.PyDict_GetItem(@pystruct, key).to_ruby
  end
end

#[]=(key, value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pybind/types/dict.rb', line 34

def []=(key, value)
  value = value.to_python
  case key
  when String, Symbol
    LibPython.PyDict_SetItemString(@pystruct, key.to_s, value)
  else
    key = key.to_python
    LibPython.PyDict_SetItem(@pystruct, key, value)
  end
  value
end

#delete(key) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pybind/types/dict.rb', line 46

def delete(key)
  case key
  when String, Symbol
    value = LibPython.PyDict_GetItemString(@pystruct, key).to_ruby
    LibPython.PyDict_DelItemString(@pystruct, key.to_s)
  else
    key = key.to_python
    value = LibPython.PyDict_GetItem(@pystruct, key).to_ruby
    LibPython.PyDict_DelItem(@pystruct, key)
  end
  value
end

#eachObject



86
87
88
89
90
91
92
# File 'lib/pybind/types/dict.rb', line 86

def each
  return enum_for unless block_given?
  keys.each do |key|
    yield key, self[key]
  end
  self
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/pybind/types/dict.rb', line 71

def has_key?(key)
  key = key.to_python
  value = LibPython.PyDict_Contains(@pystruct, key)
  raise PyError.fetch if value == -1
  value == 1
end

#keysObject



63
64
65
# File 'lib/pybind/types/dict.rb', line 63

def keys
  LibPython.PyDict_Keys(@pystruct).to_ruby
end

#sizeObject



59
60
61
# File 'lib/pybind/types/dict.rb', line 59

def size
  LibPython.PyDict_Size(@pystruct)
end

#to_aObject



78
79
80
# File 'lib/pybind/types/dict.rb', line 78

def to_a
  LibPython.PyDict_Items(@pystruct).to_ruby
end

#to_hashObject



82
83
84
# File 'lib/pybind/types/dict.rb', line 82

def to_hash
  Hash[to_a]
end

#valuesObject



67
68
69
# File 'lib/pybind/types/dict.rb', line 67

def values
  LibPython.PyDict_Values(@pystruct).to_ruby
end