Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/scipy/io/matlab/_byteordercodes.py: 37%

19 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-03 06:39 +0000

1''' Byteorder utilities for system - numpy byteorder encoding 

2 

3Converts a variety of string codes for little endian, big endian, 

4native byte order and swapped byte order to explicit NumPy endian 

5codes - one of '<' (little endian) or '>' (big endian) 

6 

7''' 

8import sys 

9 

10__all__ = [ 

11 'aliases', 'native_code', 'swapped_code', 

12 'sys_is_le', 'to_numpy_code' 

13] 

14 

15sys_is_le = sys.byteorder == 'little' 

16native_code = sys_is_le and '<' or '>' 

17swapped_code = sys_is_le and '>' or '<' 

18 

19aliases = {'little': ('little', '<', 'l', 'le'), 

20 'big': ('big', '>', 'b', 'be'), 

21 'native': ('native', '='), 

22 'swapped': ('swapped', 'S')} 

23 

24 

25def to_numpy_code(code): 

26 """ 

27 Convert various order codings to NumPy format. 

28 

29 Parameters 

30 ---------- 

31 code : str 

32 The code to convert. It is converted to lower case before parsing. 

33 Legal values are: 

34 'little', 'big', 'l', 'b', 'le', 'be', '<', '>', 'native', '=', 

35 'swapped', 's'. 

36 

37 Returns 

38 ------- 

39 out_code : {'<', '>'} 

40 Here '<' is the numpy dtype code for little endian, 

41 and '>' is the code for big endian. 

42 

43 Examples 

44 -------- 

45 >>> import sys 

46 >>> from scipy.io.matlab._byteordercodes import to_numpy_code 

47 >>> sys_is_le = (sys.byteorder == 'little') 

48 >>> sys_is_le 

49 True 

50 >>> to_numpy_code('big') 

51 '>' 

52 >>> to_numpy_code('little') 

53 '<' 

54 >>> nc = to_numpy_code('native') 

55 >>> nc == '<' if sys_is_le else nc == '>' 

56 True 

57 >>> sc = to_numpy_code('swapped') 

58 >>> sc == '>' if sys_is_le else sc == '<' 

59 True 

60 

61 """ 

62 code = code.lower() 

63 if code is None: 

64 return native_code 

65 if code in aliases['little']: 

66 return '<' 

67 elif code in aliases['big']: 

68 return '>' 

69 elif code in aliases['native']: 

70 return native_code 

71 elif code in aliases['swapped']: 

72 return swapped_code 

73 else: 

74 raise ValueError( 

75 'We cannot handle byte order %s' % code)