bind operator to this class
(cls, op)
| 1419 | |
| 1420 | @classmethod |
| 1421 | def _bind_operator(cls, op): |
| 1422 | """bind operator to this class""" |
| 1423 | name = op.__name__ |
| 1424 | |
| 1425 | if name.endswith("_"): |
| 1426 | # for and_ and or_ |
| 1427 | name = name[:-1] |
| 1428 | elif name == "inv": |
| 1429 | name = "invert" |
| 1430 | |
| 1431 | meth = f"__{name}__" |
| 1432 | |
| 1433 | if name in ("abs", "invert", "neg", "pos"): |
| 1434 | setattr(cls, meth, cls._get_unary_operator(op)) |
| 1435 | else: |
| 1436 | setattr(cls, meth, cls._get_binary_operator(op)) |
| 1437 | |
| 1438 | if name in ("eq", "gt", "ge", "lt", "le", "ne", "getitem"): |
| 1439 | return |
| 1440 | |
| 1441 | rmeth = f"__r{name}__" |
| 1442 | setattr(cls, rmeth, cls._get_binary_operator(op, inv=True)) |
| 1443 | |
| 1444 | @classmethod |
| 1445 | def _get_unary_operator(cls, op): |
no test coverage detected