-
from operator
import *
-
-
a =
-1
-
b =
5
-
-
print
'a =', a
-
print
'b =', b
-
print
-
-
print
'not_(a) :', not_(a)
-
print
'truth(a) :', truth(a)
-
print
'is_(a, b) :', is_(a, b)
-
print
'is_not(a, b):', is_not(a, b)
-
from operator
import *
-
-
a =
1
-
b =
5.0
-
print
'a =', a
-
print
'b =', b
-
for func
in (lt, le, eq, ne, ge, gt):
-
print
'%s(a, b):' % func.__name__, func(a, b)
-
from operator
import *
-
-
a =
-1
-
b =
5.0
-
c =
2
-
d =
6
-
-
print
'a =', a
-
print
'b =', b
-
print
'c =', c
-
print
'd =', d
-
-
print
'\nPositive/Negative:'
-
print
'abs(a):', abs(a)
-
print
'neg(a):', neg(a)
-
print
'neg(b):', neg(b)
-
print
'pos(a):', pos(a)
-
print
'pos(b):', pos(b)
-
-
print
'\nArithmetic:'
-
print
'add(a, b) :', add(a, b)
-
print
'div(a, b) :', div(a, b)
-
print
'div(d, c) :', div(d, c)
-
print
'floordiv(a, b):', floordiv(a, b)
-
print
'floordiv(d, c):', floordiv(d, c)
-
print
'mod(a, b) :', mod(a, b)
-
print
'mul(a, b) :', mul(a, b)
-
print
'pow(c, d) :', pow(c, d)
-
print
'sub(b, a) :', sub(b, a)
-
print
'truediv(a, b) :', truediv(a, b)
-
print
'truediv(d, c) :', truediv(d, c)
-
-
print
'\nBitwise:'
-
print
'and_(c, d) :', and_(c, d)
-
print
'invert(c) :', invert(c)
-
print
'lshift(c, d):', lshift(c, d)
-
print
'or_(c, d) :', or_(c, d)
-
print
'rshift(d, c):', rshift(d, c)
-
print
'xor(c, d) :', xor(c, d)
-
from operator
import *
-
-
a = [
1,
2,
3 ]
-
b = [
'a',
'b',
'c' ]
-
-
print
'a =', a
-
print
'b =', b
-
-
print
'\nConstructive:'
-
print
' concat(a, b):', concat(a, b)
-
print
' repeat(a, 3):', repeat(a,
3)
-
-
print
'\nSearching:'
-
print
' contains(a, 1) :', contains(a,
1)
-
print
' contains(b, "d"):', contains(b,
"d")
-
print
' countOf(a, 1) :', countOf(a,
1)
-
print
' countOf(b, "d") :', countOf(b,
"d")
-
print
' indexOf(a, 5) :', indexOf(a,
1)
-
-
print
'\nAccess Items:'
-
print
' getitem(b, 1) :', getitem(b,
1)
-
print
' getslice(a, 1, 3) :', getslice(a,
1,
3)
-
print
' setitem(b, 1, "d") :', setitem(b,
1,
"d"),
-
print
', after b =', b
-
print
' setslice(a, 1, 3, [4, 5]):', setslice(a,
1,
3, [
4,
5]),
-
print
', after a =', a
-
-
print
'\nDestructive:'
-
print
' delitem(b, 1) :', delitem(b,
1),
', after b =', b
-
print
' delslice(a, 1, 3):', delslice(a,
1,
3),
', after a=', a
-
from operator
import *
-
-
a =
-1
-
b =
5.0
-
c = [
1,
2,
3]
-
d = [
'a',
'b',
'c' ]
-
print
'a =', a
-
print
'b =', b
-
print
'c =', c
-
print
'd =', d
-
print
-
-
a = iadd(a, b)
-
print
'a = iadd(a, b) =>', a
-
print
-
-
c = iconcat(c, d)
-
print
'c = oconcat(c, d) =>', c
-
from operator
import *
-
-
class MyObj(object):
-
"""example class for attrgetter"""
-
def __init__(self, arg):
-
super(MyObj, self).__init__()
-
self.arg = arg
-
def __repr__(self):
-
return
'MyObj(%s)' % self.arg
-
-
l = [ MyObj(i)
for i
in xrange(
5) ]
-
print
'objects :', l
-
-
# Extract thr 'arg' value from each object
-
g = attrgetter(
'arg')
-
vals = [ g(i)
for i
in l ]
-
print
'arg values:', vals
-
-
# Sort using arg
-
l.reverse()
-
print
'reversed :', l
-
print
'sorted :', sorted(l, key=g)
-
from operator
import *
-
-
l = [ dict(val=
-1 * i)
for i
in xrange(
4) ]
-
print
'Dictionaries:', l
-
g = itemgetter(
'val')
-
vals = [ g(i)
for i
in l ]
-
print
' values:', vals
-
print
' sorted:', sorted(l, key=g)
-
print
-
l = [ (i, i*
-2)
for i
in xrange(
4) ]
-
print
'Tuples :', l
-
g = itemgetter(
1)
-
vals = [ g(i)
for i
in l ]
-
print
' values:', vals
-
print
' sorted:', sorted(l, key=g)
-
from operator
import *
-
-
class MyObj(object):
-
"""example class for attrgetter"""
-
def __init__(self, val):
-
super(MyObj, self).__init__()
-
self.val = val
-
return
-
def __str__(self):
-
return
'MyObj(%s)' % self.val
-
def __lt__(self, other):
-
"""compare for less-than"""
-
print
'Testing %s < %s' % (self, other)
-
return self.val < other.val
-
def __add__(self, other):
-
"""add values"""
-
print
'Adding %s + %s' % (self, other)
-
return MyObj(self.val + other.val)
-
a = MyObj(
1)
-
b = MyObj(
2)
-
-
print
'Comparison:'
-
print lt(a, b)
-
-
print
'\nArithmetic:'
-
print add(a, b)
-
from operator
import *
-
-
class NoType(object):
-
"""Supports none of the type APIs"""
-
-
class MultiType(object):
-
"""Supports multiple type APIs"""
-
def __len__(slef):
-
return
0
-
def __getitem__(slef, name):
-
return
'mapping'
-
def __int__(self):
-
return
0
-
-
o = NoType()
-
t = MultiType()
-
-
for func
in (isMappingType, isNumberType, isSequenceType):
-
print
'%s(o):' % func.__name__, func(o)
-
print
'%s(t):' % func.__name__, func(t)