Hide keyboard shortcuts

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 

6 

7 

8def format_value(v): 

9 """ 

10 Formats a value to be included in a string. 

11 

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)) 

17 

18 

19def format_parameters(pdict): 

20 """ 

21 Formats a list of parameters. 

22 

23 @param pdict dictionary 

24 @return string 

25 

26 .. runpython:: 

27 :showcode: 

28 

29 from mlinsights.helpers.parameters import format_parameters 

30 

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) 

38 

39 

40def format_function_call(name, pdict): 

41 """ 

42 Formats a function call with named parameters. 

43 

44 @param pdict dictionary 

45 @return string 

46 

47 .. runpython:: 

48 :showcode: 

49 

50 from mlinsights.helpers.parameters import format_function_call 

51 

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=' '))