Module: RbbtPython

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

Class Method Summary collapse

Class Method Details

.add_path(path) ⇒ Object



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

def self.add_path(path)
  self.run 'sys' do
    sys.path.append path
  end
end

.add_paths(paths) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/rbbt/util/python.rb', line 20

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

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



71
72
73
74
75
76
77
78
# File 'lib/rbbt/util/python.rb', line 71

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



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

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

.init_rbbtObject



28
29
30
31
32
33
34
35
# File 'lib/rbbt/util/python.rb', line 28

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

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rbbt/util/python.rb', line 41

def self.iterate(iterator, options = {})
  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
      yield iterator.__next__
      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

.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



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rbbt/util/python.rb', line 80

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

  module_eval(&block)
end

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rbbt/util/python.rb', line 94

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rbbt/util/python.rb', line 112

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



8
9
10
11
12
# File 'lib/rbbt/util/python.rb', line 8

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