(dc_operator, operator_python, dtype_precedence_dict, flag)
| 490 | |
| 491 | |
| 492 | def overload_python_operator(dc_operator, operator_python, dtype_precedence_dict, flag): |
| 493 | s = "" |
| 494 | if flag == "logical" or flag == "binary": |
| 495 | s = ''' |
| 496 | def __<operand>__(self, other): |
| 497 | return <operator>(self, other) |
| 498 | |
| 499 | def __r<operand>__(self, other): |
| 500 | return <operator>(other, self) |
| 501 | |
| 502 | def __i<operand>__(self, other): |
| 503 | """ |
| 504 | making sure left hand operand is immutable |
| 505 | """ |
| 506 | dtype_precedence_dict = ''' |
| 507 | s += str(dtype_precedence_dict) + ''' |
| 508 | left_operand_dtype = right_operand_dtype = "" |
| 509 | if "Tensor" in str(type(self)): |
| 510 | left_operand_dtype = str(type(self)).split(".")[-1].split("Tensor")[0] |
| 511 | else: |
| 512 | left_operand_dtype = str(type(self)).split("'")[1] |
| 513 | if "Tensor" in str(type(other)): |
| 514 | right_operand_dtype = str(type(other)).split(".")[-1].split("Tensor")[0] |
| 515 | else: |
| 516 | right_operand_dtype = str(type(other)).split("'")[1] |
| 517 | if (dtype_precedence_dict[left_operand_dtype] < dtype_precedence_dict[right_operand_dtype]): |
| 518 | errorMsg = "cannot modify left hand operand datatype." |
| 519 | raise TypeError(errorMsg) |
| 520 | return <operator>(self, other) |
| 521 | ''' |
| 522 | elif flag == "comparison": |
| 523 | s = ''' |
| 524 | def __<operand>__(self, other): |
| 525 | return <operator>(self, other) |
| 526 | ''' |
| 527 | s = s.replace("<operator>",dc_operator).replace("<operand>",operator_python) |
| 528 | return s |
no outgoing calls
no test coverage detected