roperator.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """
  2. Reversed Operations not available in the stdlib operator module.
  3. Defining these instead of using lambdas allows us to reference them by name.
  4. """
  5. from __future__ import annotations
  6. import operator
  7. def radd(left, right):
  8. return right + left
  9. def rsub(left, right):
  10. return right - left
  11. def rmul(left, right):
  12. return right * left
  13. def rdiv(left, right):
  14. return right / left
  15. def rtruediv(left, right):
  16. return right / left
  17. def rfloordiv(left, right):
  18. return right // left
  19. def rmod(left, right):
  20. # check if right is a string as % is the string
  21. # formatting operation; this is a TypeError
  22. # otherwise perform the op
  23. if isinstance(right, str):
  24. typ = type(left).__name__
  25. raise TypeError(f"{typ} cannot perform the operation mod")
  26. return right % left
  27. def rdivmod(left, right):
  28. return divmod(right, left)
  29. def rpow(left, right):
  30. return right**left
  31. def rand_(left, right):
  32. return operator.and_(right, left)
  33. def ror_(left, right):
  34. return operator.or_(right, left)
  35. def rxor(left, right):
  36. return operator.xor(right, left)