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.
Class Method Summary collapse
-
.module_to_path(mod, base = nil) ⇒ String
Convert a Python dotted module name to a path.
-
.path_to_module(path, base = nil) ⇒ String
Convert path to a Python dotted module name.
-
.to_python(obj) ⇒ String
Convert an object to a Python literal.
Instance Method Summary collapse
-
#module_to_path(mod, base = nil) ⇒ String
Convert a Python dotted module name to a path.
-
#path_to_module(path, base = nil) ⇒ String
Convert path to a Python dotted module name.
-
#to_python(obj) ⇒ String
Convert an object to a Python literal.
Class Method Details
.module_to_path(mod, base = nil) ⇒ String
Convert a Python dotted module name to a path.
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.
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.(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.
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.
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.
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.(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.
32 33 34 |
# File 'lib/poise_python/utils.rb', line 32 def to_python(obj) PythonEncoder.new(obj).encode end |