module PoisePython::Utils

Helper methods for Python-related things.

@since 1.0.0

Public Instance Methods

module_to_path(mod, base=nil) click to toggle source

Convert a Python dotted module name to a path.

@param mod [String] Dotted module name. @param base [String] Optional base path to treat the file as relative to. @return [String]

# File lib/poise_python/utils.rb, line 57
def module_to_path(mod, base=nil)
  path = mod.gsub(/\./, ::File::SEPARATOR) + '.py'
  path = ::File.join(base, path) if base
  path
end
path_to_module(path, base=nil) click to toggle source

Convert path to a Python dotted module name.

@param path [String] Path to the file. If base is not given, this must be

a relative path.

@param base [String] Optional base path to treat the file as relative to. @return [String]

# File lib/poise_python/utils.rb, line 42
def path_to_module(path, base=nil)
  if base
    path = ::File.expand_path(path, base)
    raise PoisePython::Error.new("Path #{path} is not inside base path #{base}") unless path.start_with?(base)
    path = path[base.length+1..-1]
  end
  path = path[0..-4] if path.end_with?('.py')
  path.gsub(/#{::File::SEPARATOR}/, '.')
end
to_python(obj) click to toggle source

Convert an object to a Python literal.

@param obj [Object] Ovject to convert. @return [String]

# File lib/poise_python/utils.rb, line 32
def to_python(obj)
  PythonEncoder.new(obj).encode
end