Module: PyBind::Import

Defined in:
lib/pybind/import.rb

Instance Method Summary collapse

Instance Method Details

#pyfrom(mod_name, import: nil) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pybind/import.rb', line 14

def pyfrom(mod_name, import: nil)
  raise ArgumentError, "missing identifiers to be imported" unless import

  mod = PyBind.import(mod_name)
  raise PyError.fetch unless mod

  case import
  when Hash
    import.each do |attr, as|
      val = mod.get_attribute(attr)
      define_singleton_method(as) { val }
    end
  when Array
    import.each do |attr|
      val = mod.get_attribute(attr)
      define_singleton_method(attr) { val }
    end
  when Symbol, String
    val = mod.get_attribute(import)
    define_singleton_method(import) { val }
  end
end

#pyimport(mod_name, as: mod_name) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/pybind/import.rb', line 3

def pyimport(mod_name, as: mod_name)
  if as.to_s.include?('.')
    raise ArgumentError, "#{as.inspect} is not a valid module variable name, use pyimport #{mod_name.inspect}, as: <name>"
  end

  mod = PyBind.import(mod_name)
  raise PyError.fetch unless mod

  define_singleton_method(as) { mod }
end