Class: Python::Pickle::PyObject

Inherits:
Object
  • Object
show all
Defined in:
lib/python/pickle/py_object.rb

Overview

Represents a Python object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(py_class, *args, **kwargs) ⇒ PyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the Python object.

Parameters:

  • py_class (PyClass)

    The Python class of the Python object.

  • args (Array)

    Additional arguments used to initialize the Python object.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments used to initialize the Python object.



44
45
46
47
48
49
50
# File 'lib/python/pickle/py_object.rb', line 44

def initialize(py_class,*args,**kwargs)
  @py_class    = py_class
  @init_args   = args
  @init_kwargs = kwargs

  @attributes = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object (protected)

Allows for direct access to attributes.

Examples:

obj.x = 2
obj.x
# => 2


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/python/pickle/py_object.rb', line 119

def method_missing(method_name,*arguments,&block)
  if method_name.end_with?('=')
    attr = method_name[0..-2]

    if arguments.length == 1 && !block
      return @attributes[attr] = arguments[0]
    else
      super(method_name,*arguments,&block)
    end
  else
    attr = method_name.to_s

    if @attributes.has_key?(attr) && arguments.empty? && !block
      return @attributes[attr]
    else
      super(method_name,*arguments,&block)
    end
  end
end

Instance Attribute Details

#attributesHash{String => Object} (readonly)

The populated attributes of the Python object.

Returns:

  • (Hash{String => Object})


28
29
30
# File 'lib/python/pickle/py_object.rb', line 28

def attributes
  @attributes
end

#init_argsArray (readonly)

The arguments used to initialize the Python object.

Returns:

  • (Array)


18
19
20
# File 'lib/python/pickle/py_object.rb', line 18

def init_args
  @init_args
end

#init_kwargsArray (readonly)

The keyword arguments used to initialize the Python object.

Returns:

  • (Array)


23
24
25
# File 'lib/python/pickle/py_object.rb', line 23

def init_kwargs
  @init_kwargs
end

#py_classPyClass (readonly)

The Python class of the Python object.

Returns:



13
14
15
# File 'lib/python/pickle/py_object.rb', line 13

def py_class
  @py_class
end

Instance Method Details

#__setstate__(new_attributes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets the state of the Python object.



96
97
98
# File 'lib/python/pickle/py_object.rb', line 96

def __setstate__(new_attributes)
  @attributes = new_attributes
end

#getattr(attribute) ⇒ Object

Fetches the attribute of the Python object.

Examples:

obj.getattr('x')
# => 2

Parameters:

  • attribute (String)

    The attribute name.

Returns:

  • (Object)

    The attributes value.

Raises:

  • (ArgumentError)

    The Python object does not have an attribute of the given name.



68
69
70
71
72
# File 'lib/python/pickle/py_object.rb', line 68

def getattr(attribute)
  @attributes.fetch(attribute) do
    raise(ArgumentError,"Python object has no attribute #{attribute.inspect}: #{self.inspect}")
  end
end

#setattr(name, value) ⇒ Object

Sets an attribute in the Python object.

Examples:

obj.setattr('x',2)
# => 2

Parameters:

  • name (String)

    The attribute name.

  • value (Object)

    The new value for the attribute.



87
88
89
# File 'lib/python/pickle/py_object.rb', line 87

def setattr(name,value)
  @attributes[name] = value
end

#to_hHash

Converts the Python object to a Hash.

Returns:

  • (Hash)


105
106
107
# File 'lib/python/pickle/py_object.rb', line 105

def to_h
  @attributes
end