module RbbtPython
Constants
- MUTEX
Attributes
Public Class Methods
Source
# File lib/rbbt/util/python/paths.rb, line 9 def self.add_path(path) self.paths << path end
Source
# File lib/rbbt/util/python/paths.rb, line 13 def self.add_paths(paths) self.paths.concat paths end
Source
# File lib/rbbt/util/python.rb, line 146 def self.binding_run(binding = nil, *args, &block) binding = new_binding binding.instance_exec *args, &block end
Source
# File lib/rbbt/util/python.rb, line 39 def self.call_method(module_name, method_name, *args) RbbtPython.import_method(module_name, method_name).call(*args) end
Source
# File lib/rbbt/util/python.rb, line 55 def self.class_new_obj(module_name, class_name, args={}) RbbtPython.get_class(module_name, class_name).new(**args) end
Source
# File lib/rbbt/util/python.rb, line 133 def self.collect(iterator, options = {}, &block) acc = [] self.iterate(iterator, options) do |elem| res = block.call elem acc << res end acc end
Source
# 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.to_s tsv.fields = py2ruby_a(tuple.columns.values) keys = py2ruby_a(tuple.index.values) PyCall.len(tuple.index).times do |i| k = keys[i] tsv[k] = py2ruby_a(tuple.values[i]) end tsv end
Source
# File lib/rbbt/util/python.rb, line 59 def self.exec(script) PyCall.exec(script) end
Source
# File lib/rbbt/util/python.rb, line 50 def self.get_class(module_name, class_name) mod = get_module(module_name) mod.send(class_name) end
Source
# File lib/rbbt/util/python.rb, line 43 def self.get_module(module_name) init_rbbt save_module_name = module_name.to_s.gsub(".", "_") RbbtPython.pyimport(module_name, as: save_module_name) RbbtPython.send(save_module_name) end
Source
# File lib/rbbt/util/python.rb, line 33 def self.import_method(module_name, method_name, as = nil) init_rbbt RbbtPython.pyfrom module_name, import: method_name RbbtPython.method(method_name) end
Source
# File lib/rbbt/util/python.rb, line 22 def self.init_rbbt if ! defined?(@@__init_rbbt_python) || ! @@__init_rbbt_python RbbtPython.process_paths res = RbbtPython.run do Log.debug "Loading python 'rbbt' module into pycall RbbtPython module" pyimport("rbbt") end @@__init_rbbt_python = true end end
Source
# File lib/rbbt/util/python/run.rb, line 17 def self.init_thread if defined?(self.thread) && (self.thread && ! self.thread.alive?) Log.warn "Reloading RbbtPython thread" self.thread.join self.thread = nil end self.thread ||= Thread.new do require 'pycall' RbbtPython.process_paths begin while block = QUEUE_IN.pop break if block == :stop res = begin module_eval(&block) rescue Exception Log.exception $! raise $! end QUEUE_OUT.push res end rescue Exception Log.exception $! raise $! ensure PyCall.finalize if PyCall.respond_to?(:finalize) end end end
Source
# File lib/rbbt/util/python.rb, line 94 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
Source
# File lib/rbbt/util/python.rb, line 63 def self.iterate_index(elem, options = {}) 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
Source
# 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
Source
# File lib/rbbt/util/python/script.rb, line 84 def self.load_json(file) JSON.load_file(file) end
Source
# File lib/rbbt/util/python/script.rb, line 62 def self.load_pickle(file) require 'python/pickle' Log.debug ("Loading pickle #{file}") Python::Pickle.load_file(file) end
Also aliased as: load_result
Source
# File lib/rbbt/util/python/script.rb, line 30 def self.load_script_variables(variables = {}) code = "# Variables\nimport rbbt\n" tmp_files = [] variables.each do |name,value| case value when TSV tmp_file = TmpFile.tmp_file tmp_files << tmp_file Open.write(tmp_file, value.to_s) code << "#{name} = rbbt.tsv('#{tmp_file}')" << "\n" else code << "#{name} = #{RbbtPython.ruby2python(value)}" << "\n" end end [code, tmp_files] end
Source
# File lib/rbbt/util/python/util.rb, line 41 def self.numpy2ruby(numpy) list2ruby(numpy.tolist) end
Source
# 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
Source
# File lib/rbbt/util/python/paths.rb, line 17 def self.process_paths RbbtPython.run_direct 'sys' do RbbtPython.paths.each do |path| sys.path.append path end nil end end
Source
# File lib/rbbt/util/python/util.rb, line 2 def self.py2ruby_a(array) PyCall::List.(array).to_a end
Also aliased as: to_a
Source
# File lib/rbbt/util/python/script.rb, line 2 def self.ruby2python(object) case object when Float::INFINITY "inf" when nil "None" when ":NA" "None" when Symbol "#{ object }" when String object = object.dup if Path === object object[0] == ":" ? object[1..-1] : "'#{ object }'" when Numeric object when TrueClass "True" when FalseClass "False" when Array "[#{object.collect{|e| ruby2python(e) } * ", "}]" when Hash "{" << object.collect{|k,v| [ruby2python(k.to_s), ruby2python(v)] * ":"} * ", " << "}" else raise "Type of object not known: #{ object.inspect }" end end
Source
# File lib/rbbt/util/python/run.rb, line 64 def self.run_direct(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
Source
# File lib/rbbt/util/python/run.rb, line 49 def self.run_in_thread(&block) self.synchronize do init_thread QUEUE_IN.push block QUEUE_OUT.pop end end
Source
# File lib/rbbt/util/python/run.rb, line 103 def self.run_log(mod = nil, imports = nil, severity = 0, severity_err = nil, &block) Log.trap_std("Python STDOUT", "Python STDERR", severity, severity_err) do run(mod, imports, &block) end end
Source
# File lib/rbbt/util/python/run.rb, line 109 def self.run_log_stderr(mod = nil, imports = nil, severity = 0, &block) Log.trap_stderr("Python STDERR", severity) do run(mod, imports, &block) end end
Source
# File lib/rbbt/util/python/run.rb, line 92 def self.run_simple(mod = nil, imports = nil, &block) self.synchronize do RbbtPython.process_paths run_direct(mod, imports, &block) end end
Also aliased as: run
Source
# File lib/rbbt/util/python/run.rb, line 78 def self.run_threaded(mod = nil, imports = nil, &block) run_in_thread do if Hash === imports pyimport mod, **imports elsif imports.nil? pyimport mod else pyfrom mod, :import => imports end end if mod run_in_thread(&block) end
Source
# File lib/rbbt/util/python/script.rb, line 68 def self.save_script_result_json(file) <<-EOF # Save try: result except NameError: result = None if result is not None: import json file = open('#{file}', 'w', encoding='utf-8') # dump information to that file file.write(json.dumps(result)) file.flush file.close EOF end
Source
# File lib/rbbt/util/python/script.rb, line 48 def self.save_script_result_pickle(file) <<-EOF # Save try: result except NameError: result = None if result is not None: import pickle file = open('#{file}', 'wb') # dump information to that file pickle.dump(result, file) EOF end
Also aliased as: save_script_result
Source
# File lib/rbbt/util/python/script.rb, line 93 def self.script(text, variables = {}) if variables.any? variable_definitions, tmp_files = load_script_variables(variables) text = variable_definitions + "\n# Script\n" + text end TmpFile.with_file do |tmp_file| text += save_script_result(tmp_file) Log.debug "Running python script:\n#{text.dup}" path_env = RbbtPython.paths * ":" CMD.cmd_log("env PYTHONPATH=#{path_env} python", {in: text}) tmp_files.each{|file| Open.rm_rf file } if tmp_files if Open.exists?(tmp_file) load_result(tmp_file) end end end
Source
# File lib/rbbt/util/python/run.rb, line 57 def self.stop_thread self.synchronize do QUEUE_IN.push :stop end if self.thread && self.thread.alive? self.thread.join if self.thread end
Source
# File lib/rbbt/util/python/run.rb, line 13 def self.synchronize(&block) MUTEX.synchronize &block end
Source
# File lib/rbbt/util/python/util.rb, line 11 def self.tsv2df(tsv) df = nil RbbtPython.run_direct 'pandas' do df = pandas.DataFrame.new(tsv.values, columns: tsv.fields, index: tsv.keys) df.columns.name = tsv.key_field end df end