Module: PoisePython::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/poise_python/utils.rb,
lib/poise_python/utils/python_encoder.rb

Overview

Helper methods for Python-related things.

Since:

  • 1.0.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.module_to_path(mod, base = nil) ⇒ String

Convert a Python dotted module name to a path.

Parameters:

  • mod (String)

    Dotted module name.

  • base (String) (defaults to: nil)

    Optional base path to treat the file as relative to.

Returns:

  • (String)

Since:

  • 1.0.0



57
58
59
60
61
# 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) ⇒ String

Convert path to a Python dotted module name.

Parameters:

  • path (String)

    Path to the file. If base is not given, this must be a relative path.

  • base (String) (defaults to: nil)

    Optional base path to treat the file as relative to.

Returns:

  • (String)

Since:

  • 1.0.0



42
43
44
45
46
47
48
49
50
# 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) ⇒ String

Convert an object to a Python literal.

Parameters:

  • obj (Object)

    Ovject to convert.

Returns:

  • (String)

Since:

  • 1.0.0



32
33
34
# File 'lib/poise_python/utils.rb', line 32

def to_python(obj)
  PythonEncoder.new(obj).encode
end

Instance Method Details

#module_to_path(mod, base = nil) ⇒ String

Convert a Python dotted module name to a path.

Parameters:

  • mod (String)

    Dotted module name.

  • base (String) (defaults to: nil)

    Optional base path to treat the file as relative to.

Returns:

  • (String)

Since:

  • 1.0.0



57
58
59
60
61
# 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) ⇒ String

Convert path to a Python dotted module name.

Parameters:

  • path (String)

    Path to the file. If base is not given, this must be a relative path.

  • base (String) (defaults to: nil)

    Optional base path to treat the file as relative to.

Returns:

  • (String)

Since:

  • 1.0.0



42
43
44
45
46
47
48
49
50
# 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) ⇒ String

Convert an object to a Python literal.

Parameters:

  • obj (Object)

    Ovject to convert.

Returns:

  • (String)

Since:

  • 1.0.0



32
33
34
# File 'lib/poise_python/utils.rb', line 32

def to_python(obj)
  PythonEncoder.new(obj).encode
end