Module: RbbtPython

Extended by:
PyCall::Import
Defined in:
lib/rbbt/util/python.rb,
lib/rbbt/util/python/util.rb

Defined Under Namespace

Classes: Binding, RbbtPythonException

Class Method Summary collapse

Class Method Details

.add_path(path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/rbbt/util/python.rb', line 24

def self.add_path(path)
  begin
    self.run 'sys' do
      sys.path.append path
    end
  rescue
    raise RbbtPythonException, 
      "Could not add path #{Misc.fingerprint path} to python sys: " + $!.message
  end
end

.add_paths(paths) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/rbbt/util/python.rb', line 35

def self.add_paths(paths)
  self.run 'sys' do
    paths.each do |path|
      sys.path.append path
    end
  end
end

.binding_run(binding = nil, *args, &block) ⇒ Object



207
208
209
210
# File 'lib/rbbt/util/python.rb', line 207

def self.binding_run(binding = nil, *args, &block)
  binding = new_binding
  binding.instance_exec *args, &block
end

.call_method(module_name, method_name, *args) ⇒ Object



57
58
59
# File 'lib/rbbt/util/python.rb', line 57

def self.call_method(module_name, method_name, *args)
  RbbtPython.import_method(module_name, method_name).call(*args)
end

.class_new_obj(module_name, class_name, args = {}) ⇒ Object



67
68
69
# File 'lib/rbbt/util/python.rb', line 67

def self.class_new_obj(module_name, class_name, args={})
  RbbtPython.get_class(module_name, class_name).new(**args)
end

.collect(iterator, options = {}, &block) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/rbbt/util/python.rb', line 146

def self.collect(iterator, options = {}, &block)
  acc = []
  self.iterate(iterator, options) do |elem|
    res = block.call elem
    acc << res
  end
  acc
end

.df2tsv(tuple, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rbbt/util/python/util.rb', line 20

def self.df2tsv(tuple, options = {})
  options = Misc.add_defaults options, :type => :list
  IndiferentHash.setup options
  tsv = TSV.setup({}, options)
  tsv.key_field = options[:key_field] || tuple.columns.name
  tsv.fields = py2ruby_a(tuple.columns.values)
  keys = tuple.index.values
  PyCall.len(tuple.index).times do |i|
    k = keys[i]
    tsv[k] = py2ruby_a(tuple.values[i])
  end
  tsv
end

.exec(script) ⇒ Object



71
72
73
# File 'lib/rbbt/util/python.rb', line 71

def self.exec(script)
  PyCall.exec(script)
end

.get_class(module_name, class_name) ⇒ Object



61
62
63
64
65
# File 'lib/rbbt/util/python.rb', line 61

def self.get_class(module_name, class_name)
  save_module_name = module_name.to_s.gsub(".", "_")
  RbbtPython.pyimport(module_name, as: save_module_name)
  RbbtPython.send(save_module_name).send(class_name)
end

.import_method(module_name, method_name, as = nil) ⇒ Object



52
53
54
55
# File 'lib/rbbt/util/python.rb', line 52

def self.import_method(module_name, method_name, as = nil)
  RbbtPython.pyfrom module_name, import: method_name
  RbbtPython.method(method_name)
end

.init_rbbtObject



43
44
45
46
47
48
49
50
# File 'lib/rbbt/util/python.rb', line 43

def self.init_rbbt
  if ! defined?(@@__init_rbbt_python) || ! @@__init_rbbt_python
    Log.debug "Loading python 'rbbt' module into pycall RbbtPython module"
    RbbtPython.add_paths(Rbbt.python.find_all)
    RbbtPython.pyimport("rbbt")
    @@__init_rbbt_python = true
  end
end

.iterate(iterator, options = {}, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rbbt/util/python.rb', line 107

def self.iterate(iterator, options = {}, &block)
  if ! iterator.respond_to?(:__next__)
    if iterator.respond_to?(:__iter__)
      iterator = iterator.__iter__
    else
      return iterate_index(iterator, options, &block)
    end
  end

  bar = options[:bar]

  case bar
  when TrueClass
    bar = Log::ProgressBar.new nil, :desc => "RbbtPython iterate"
  when String
    bar = Log::ProgressBar.new nil, :desc => bar
  end

  while true
    begin
      elem = iterator.__next__
      yield elem
      bar.tick if bar
    rescue PyCall::PyError
      if $!.type.to_s == "<class 'StopIteration'>"
        break
      else
        raise $!
      end
    rescue
      bar.error if bar
      raise $!
    end
  end

  Log::ProgressBar.remove_bar bar if bar
  nil
end

.iterate_index(elem, options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rbbt/util/python.rb', line 75

def self.iterate_index(elem, options = {})
  iii :interate_index
  bar = options[:bar]

  len = PyCall.len(elem)
  case bar
  when TrueClass
    bar = Log::ProgressBar.new nil, :desc => "RbbtPython iterate"
  when String
    bar = Log::ProgressBar.new nil, :desc => bar
  end

  len.times do |i|
    begin
      yield elem[i]
      bar.tick if bar
    rescue PyCall::PyError
      if $!.type.to_s == "<class 'StopIteration'>"
        break
      else
        raise $!
      end
    rescue
      bar.error if bar
      raise $!
    end
  end

  Log::ProgressBar.remove_bar bar if bar
  nil
end

.list2ruby(list) ⇒ Object



34
35
36
37
38
39
# File 'lib/rbbt/util/python/util.rb', line 34

def self.list2ruby(list)
  return list unless PyCall::List === list 
  list.collect do |e|
    list2ruby(e)
  end
end

.new_bindingObject



203
204
205
# File 'lib/rbbt/util/python.rb', line 203

def self.new_binding
  Binding.new
end

.numpy2ruby(numpy) ⇒ Object



41
42
43
# File 'lib/rbbt/util/python/util.rb', line 41

def self.numpy2ruby(numpy)
  list2ruby(numpy.tolist)
end

.obj2hash(obj) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/rbbt/util/python/util.rb', line 45

def self.obj2hash(obj)
  hash = {}
  RbbtPython.iterate obj.keys do |k|
    hash[k] = obj[k]
  end
  hash
end

.py2ruby_a(array) ⇒ Object Also known as: to_a



2
3
4
# File 'lib/rbbt/util/python/util.rb', line 2

def self.py2ruby_a(array)
  PyCall::List.(array).to_a
end

.run(mod = nil, imports = nil, &block) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rbbt/util/python.rb', line 155

def self.run(mod = nil, imports = nil, &block)
  if mod
    if Hash === imports
      pyimport mod, **imports
    elsif imports.nil?
      pyimport mod 
    else
      pyfrom mod, :import => imports
    end
  end

  module_eval(&block)
end

.run_log(mod = nil, imports = nil, severity = 0, severity_err = nil, &block) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rbbt/util/python.rb', line 169

def self.run_log(mod = nil, imports = nil, severity = 0, severity_err = nil, &block)
  if mod
    if imports == "*" || imports == ["*"]
      pyfrom mod
    elsif Array === imports
      pyfrom mod, :import => imports
    elsif Hash === imports
      pyimport mod, imports
    else
      pyimport mod 
    end
  end

  Log.trap_std("Python STDOUT", "Python STDERR", severity, severity_err) do
    module_eval(&block)
  end
end

.run_log_stderr(mod = nil, imports = nil, severity = 0, &block) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rbbt/util/python.rb', line 187

def self.run_log_stderr(mod = nil, imports = nil, severity = 0, &block)
  if mod
    if Array === imports
      pyfrom mod, :import => imports
    elsif Hash === imports
      pyimport mod, imports
    else
      pyimport mod 
    end
  end

  Log.trap_stderr("Python STDERR", severity) do
    module_eval(&block)
  end
end

.script(text, options = {}) ⇒ Object



18
19
20
21
22
# File 'lib/rbbt/util/python.rb', line 18

def self.script(text, options = {})
  Log.debug "Running python script:\n#{text.dup}"
  text = StringIO.new text unless IO === text
  CMD.cmd_log(:python, options.merge(:in => text))
end

.tsv2df(tsv) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/rbbt/util/python/util.rb', line 11

def self.tsv2df(tsv)
  df = nil
  RbbtPython.run 'pandas' do
    df = pandas.DataFrame.new(tsv.values, columns: tsv.fields, index: tsv.keys)
    df.columns.name = tsv.key_field
  end
  df
end