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

2Templates for invalid operations. 

3""" 

4import operator 

5 

6import numpy as np 

7 

8 

9def invalid_comparison(left, right, op): 

10 """ 

11 If a comparison has mismatched types and is not necessarily meaningful, 

12 follow python3 conventions by: 

13 

14 - returning all-False for equality 

15 - returning all-True for inequality 

16 - raising TypeError otherwise 

17 

18 Parameters 

19 ---------- 

20 left : array-like 

21 right : scalar, array-like 

22 op : operator.{eq, ne, lt, le, gt} 

23 

24 Raises 

25 ------ 

26 TypeError : on inequality comparisons 

27 """ 

28 if op is operator.eq: 

29 res_values = np.zeros(left.shape, dtype=bool) 

30 elif op is operator.ne: 

31 res_values = np.ones(left.shape, dtype=bool) 

32 else: 

33 typ = type(right).__name__ 

34 raise TypeError(f"Invalid comparison between dtype={left.dtype} and {typ}") 

35 return res_values 

36 

37 

38def make_invalid_op(name: str): 

39 """ 

40 Return a binary method that always raises a TypeError. 

41 

42 Parameters 

43 ---------- 

44 name : str 

45 

46 Returns 

47 ------- 

48 invalid_op : function 

49 """ 

50 

51 def invalid_op(self, other=None): 

52 typ = type(self).__name__ 

53 raise TypeError(f"cannot perform {name} with this index type: {typ}") 

54 

55 invalid_op.__name__ = name 

56 return invalid_op