Coverage for mlprodict/npy/onnx_version.py: 100%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

26 statements  

1""" 

2@file 

3@brief Identifies a version of a function. 

4 

5.. versionadded:: 0.6 

6""" 

7from collections import namedtuple 

8 

9 

10class FctVersion(namedtuple("_version_", ['args', 'kwargs'])): 

11 """ 

12 Identifies a version of a function based on its 

13 arguments and its parameters. 

14 """ 

15 __slots__ = () 

16 

17 def _check_(self): 

18 if self.args is not None and not isinstance(self.args, tuple): 

19 raise TypeError("args must be None or a tuple.") 

20 if self.kwargs is not None and not isinstance(self.kwargs, tuple): 

21 raise TypeError("kwargs must None or be a tuple.") 

22 

23 def __repr__(self): 

24 "usual" 

25 def cl(s): 

26 return str(s).replace("<class '", "").replace("'>", "") 

27 if self.args is None: 

28 sa = "None" 

29 else: 

30 sa = ",".join(map(cl, self.args)) 

31 sa = ("(%s)" % sa) if len(self.args) > 1 else ("(%s,)" % sa) 

32 

33 return "%s(%s, %s)" % ( 

34 self.__class__.__name__, sa, self.kwargs) 

35 

36 def __len__(self): 

37 "Returns the sum of lengths." 

38 return ((0 if self.args is None else len(self.args)) + 

39 (0 if self.kwargs is None else len(self.kwargs))) 

40 

41 def as_tuple(self): 

42 "Returns a single tuple for the version." 

43 return ((tuple() if self.args is None else self.args) + 

44 (tuple() if self.kwargs is None else self.kwargs)) 

45 

46 def as_tuple_with_sep(self, sep): 

47 "Returns a single tuple for the version." 

48 return ((tuple() if self.args is None else self.args) + 

49 (sep, ) + 

50 (tuple() if self.kwargs is None else self.kwargs)) 

51 

52 def as_string(self): 

53 "Returns a single string identifier." 

54 val = "_".join(map(str, self.as_tuple_with_sep("_"))) 

55 val = val.replace("<class 'numpy.", "").replace( 

56 '.', "_").replace("'>", "").replace(" ", "") 

57 return val.lower()