Skip to content

Commit a5bef5c

Browse files
committed
add module operator
1 parent 64e270e commit a5bef5c

9 files changed

Lines changed: 199 additions & 0 deletions

‎module_operator/__init__.py‎

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import operator
2+
3+
4+
class MyObj:
5+
"""example class for attrgetter"""
6+
7+
def __init__(self, arg):
8+
super(MyObj, self).__init__()
9+
self.arg = arg
10+
11+
def __repr__(self):
12+
return 'MyObj({})'.format(self.arg)
13+
14+
15+
l = [MyObj(i) for i in range(5)]
16+
print('objects:', l)
17+
g = operator.attrgetter('arg')
18+
vals = [g(i) for i in l]
19+
print('arg values:', vals)
20+
21+
# Sort using arg
22+
l.reverse()
23+
print('reversed:', l)
24+
print('sorted: ', sorted(l, key=g))
25+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import operator
2+
3+
4+
a = -1
5+
b = 5
6+
7+
print('a =', a)
8+
print('b =', b)
9+
print()
10+
11+
print('not_(a): ', operator.not_(a))
12+
print('truth(a): ', operator.truth(a))
13+
print('is_(a, b): ', operator.is_(a, b))
14+
print('is_not(a, b): ', operator.is_not(a, b))
15+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import operator
2+
3+
4+
class MyObj:
5+
"""Example for operator overloading"""
6+
7+
def __init__(self, val):
8+
super(MyObj, self).__init__()
9+
self.val = val
10+
11+
def __str__(self):
12+
return 'MyObj({})'.format(self.val)
13+
14+
def __lt__(self, other):
15+
"""compare for less-than"""
16+
print('Testing {} < {}'.format(self, other))
17+
return self.val < other.val
18+
19+
def __add__(self, other):
20+
"""add values"""
21+
print('Adding {} + {}'.format(self, other))
22+
return MyObj(self.val + other.val)
23+
24+
25+
a = MyObj(1)
26+
b = MyObj(2)
27+
28+
print('Comparison:')
29+
print(operator.lt(a, b))
30+
31+
print('\nArithmetic:')
32+
print(operator.add(a, b))
33+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import operator
2+
3+
a = 1
4+
b = 5.0
5+
6+
print('a =', a)
7+
print('b =', b)
8+
9+
for func in (operator.lt, operator.le, operator.eq, operator.ge, operator.gt):
10+
print('{}(a, b): {}'.format(func.__name__, func(a, b)))
11+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import operator
2+
3+
a = -1
4+
b = 5.0
5+
c = [1, 2, 3]
6+
d = ['a', 'b', 'c']
7+
8+
print('a =', a)
9+
print('b =', b)
10+
print('c =', c)
11+
print('d =', d)
12+
13+
a = operator.iadd(a, b)
14+
print('a = iadd(a, b) =>', a)
15+
print()
16+
17+
operator.iconcat(c, d)
18+
print('c = iconcat(c, d) =>', c)
19+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import operator
2+
3+
4+
l = [dict(val=-1 * i) for i in range(4)]
5+
print('Dictionaries:')
6+
print(' original:', l)
7+
g = operator.itemgetter('val')
8+
vals = [g(i) for i in l]
9+
print(' values:', vals)
10+
print(' sorted:', sorted(l, key=g))
11+
12+
print()
13+
l = [(i, i * -2) for i in range(4)]
14+
print('\nTuples:')
15+
print(' original:', l)
16+
g = operator.itemgetter(1)
17+
vals = [g(i) for i in l]
18+
print(' values:', vals)
19+
print(' sorted:', sorted(l, key=g))
20+

‎module_operator/operator_math.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import operator
2+
3+
4+
a = -1
5+
b = 5.0
6+
c = 2
7+
d = 6
8+
9+
10+
print('a =', a)
11+
print('b =', b)
12+
print('c =', c)
13+
print('d =', d)
14+
15+
print('\nPositive/Negative:')
16+
print('abs(a): ', operator.abs(a))
17+
print('neg(a): ', operator.neg(a))
18+
print('neg(b): ', operator.neg(b))
19+
print('pos(a): ', operator.pos(a))
20+
print('pos(b): ', operator.pos(b))
21+
22+
print('\nArithmetic:')
23+
print('add(a, b): ', operator.add(a, b))
24+
print('floordiv(a, b): ', operator.floordiv(a, b))
25+
print('floordiv(d, c): ', operator.floordiv(d, c))
26+
print('mod(a, b): ', operator.mod(a, b))
27+
print('mul(a, b): ', operator.mul(a, b))
28+
print('pow(c, d): ', operator.pow(c, d))
29+
print('sub(b, a): ', operator.sub(b, a))
30+
print('truediv(a, b): ', operator.truediv(a, b))
31+
print('truediv(d, c): ', operator.truediv(d, c))
32+
33+
print('\nBitwise:')
34+
print('and_(c, d): ', operator.and_(c, d))
35+
print('invert(c): ', operator.invert(c))
36+
print('lshift(c, d): ', operator.lshift(c, d))
37+
print('or_(c, d): ', operator.or_(c, d))
38+
print('rshift(d, c): ', operator.rshift(d, c))
39+
print('xor(c, d): ', operator.xor(c, d))
40+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import operator
2+
3+
4+
a = [1, 2, 3]
5+
b = ['a', 'b', 'c']
6+
7+
print('a =', a)
8+
print('b =', b)
9+
10+
print('\nConstructive:')
11+
print(' concat(a, b):', operator.concat(a, b))
12+
13+
print('\nSearching:')
14+
print(' contains(a, 1):', operator.contains(a, 1))
15+
print(' contains(b, "d"):', operator.contains(b, "d"))
16+
print(' countOf(a, 1):', operator.countOf(a, 1))
17+
print(' countOf(b, "d"):', operator.countOf(b, "d"))
18+
print(' indexOf(a, 1):', operator.indexOf(a, 1))
19+
20+
print('\nAccess items:')
21+
print(' getitem(b, 1):', operator.getitem(b, 1))
22+
print(' getitem(b, slice(1, 3))', operator.getitem(b, slice(1, 3)))
23+
print(' setitem(a, slice(1, 3), [4, 5])', end=' ')
24+
operator.setitem(a, slice(1, 3), [4, 5])
25+
print(a)
26+
27+
print('\nDestructive:')
28+
print(' delitem(b, 1): ', end=' ')
29+
operator.delitem(b, 1)
30+
print(b)
31+
32+
print(' delitem(a, slice(1, 3)):', end=' ')
33+
operator.delitem(a, slice(1, 3))
34+
print(a)
35+
36+

0 commit comments

Comments
 (0)