Puppet Function: wmflib::to_python

Defined in:
modules/wmflib/lib/puppet/functions/wmflib/to_python.rb
Function type:
Ruby 4.x API

Overview

wmflib::to_python(Any $object)Any

TODO: Remove this once fix merged upstream This is a local version of stdlib to_python backporting the following fix until it is merged upstream:

https://github.com/puppetlabs/puppetlabs-stdlib/pull/1205

Parameters:

  • object (Any)

Returns:

  • (Any)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'modules/wmflib/lib/puppet/functions/wmflib/to_python.rb', line 22

Puppet::Functions.create_function(:'wmflib::to_python') do
  dispatch :to_python do
    param 'Any', :object
  end

  # @param object
  #   The object to be converted
  #
  # @return [String]
  #   The String representation of the object
  def to_python(object)
    case object
    when true then 'True'
    when false then 'False'
    when :undef then 'None'
    when nil then 'None'
    when Array then "[#{object.map { |x| to_python(x) }.join(', ')}]"
    when Hash then "{#{object.map { |k, v| "#{to_python(k)}: #{to_python(v)}" }.join(', ')}}"
    else object.inspect
    end
  end
end