Class: Python

Inherits:
Object
  • Object
show all
Defined in:
lib/raka/lang/python/impl.rb

Overview

Binding for python language, allow specifying imports and paths

Instance Method Summary collapse

Constructor Details

#initialize(libs: [], paths: [], runner: 'python') ⇒ Python

Returns a new instance of Python.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/raka/lang/python/impl.rb', line 8

def initialize(libs: [], paths: [], runner: 'python')
  common_aliases = {
    pandas: :pd,
    numpy: :np
  }.freeze

  libs = libs.map(&:to_s) # convert all to strings
  @imports = libs.map { |lib| "import #{lib}" }
  common_aliases.each do |name, short|
    @imports.push("import #{name} as #{short}") if libs.include? name.to_s
  end
  @paths = ['import sys'] + paths.map { |path| "sys.path.append('#{path}')" }
  @runner = runner
end

Instance Method Details

#build(code, _task) ⇒ Object



23
24
25
# File 'lib/raka/lang/python/impl.rb', line 23

def build(code, _task)
  (@paths + @imports + [code]).join "\n"
end

#run_script(env, fname, _task) ⇒ Object



27
28
29
# File 'lib/raka/lang/python/impl.rb', line 27

def run_script(env, fname, _task)
  run_cmd(env, "#{@runner} #{fname}")
end