Coverage for mlinsights/helpers/parameters.py: 100%
11 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-28 08:46 +0100
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-28 08:46 +0100
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 ("%r" % v.replace("'", "\\'")
16 if isinstance(v, str) else f"{v!r}")
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(f'{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 = f'{name}({format_parameters(pdict)})'
56 return "\n".join(textwrap.wrap(res, width=70, subsequent_indent=' '))