Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Functions about parameters.
4"""
5import textwrap
8def format_value(v):
9 """
10 Formats a value to be included in a string.
12 @param v a string
13 @return a string
14 """
15 return ("'{0}'".format(v.replace("'", "\\'"))
16 if isinstance(v, str) else "{0}".format(v))
19def format_parameters(pdict):
20 """
21 Formats a list of parameters.
23 @param pdict dictionary
24 @return string
26 .. runpython::
27 :showcode:
29 from mlinsights.helpers.parameters import format_parameters
31 d = dict(i=2, x=6.7, s="r")
32 print(format_parameters(d))
33 """
34 res = []
35 for k, v in sorted(pdict.items()):
36 res.append('{0}={1}'.format(k, format_value(v)))
37 return ", ".join(res)
40def format_function_call(name, pdict):
41 """
42 Formats a function call with named parameters.
44 @param pdict dictionary
45 @return string
47 .. runpython::
48 :showcode:
50 from mlinsights.helpers.parameters import format_function_call
52 d = dict(i=2, x=6.7, s="r")
53 print(format_function_call("fct", d))
54 """
55 res = '{0}({1})'.format(name, format_parameters(pdict))
56 return "\n".join(textwrap.wrap(res, width=70, subsequent_indent=' '))