zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Reversed Operations not available in the stdlib operator module.
Defining these instead of using lambdas allows us to reference them by name.
"""
from __future__ import annotations
 
import operator
 
 
def radd(left, right):
    return right + left
 
 
def rsub(left, right):
    return right - left
 
 
def rmul(left, right):
    return right * left
 
 
def rdiv(left, right):
    return right / left
 
 
def rtruediv(left, right):
    return right / left
 
 
def rfloordiv(left, right):
    return right // left
 
 
def rmod(left, right):
    # check if right is a string as % is the string
    # formatting operation; this is a TypeError
    # otherwise perform the op
    if isinstance(right, str):
        typ = type(left).__name__
        raise TypeError(f"{typ} cannot perform the operation mod")
 
    return right % left
 
 
def rdivmod(left, right):
    return divmod(right, left)
 
 
def rpow(left, right):
    return right**left
 
 
def rand_(left, right):
    return operator.and_(right, left)
 
 
def ror_(left, right):
    return operator.or_(right, left)
 
 
def rxor(left, right):
    return operator.xor(right, left)