def __lshift__(self, other): return _op2(self, other, operator.lshift)
python类lshift()的实例源码
def __rlshift__(self, other): return _op2(self, other, operator.lshift, rev=True)
def test_lshift(self):
self.assertRaises(TypeError, operator.lshift)
self.assertRaises(TypeError, operator.lshift, None, 42)
self.assertTrue(operator.lshift(5, 1) == 10)
self.assertTrue(operator.lshift(5, 0) == 5)
self.assertRaises(ValueError, operator.lshift, 2, -1)
def test_lshift(self):
self.assertRaises(TypeError, operator.lshift)
self.assertRaises(TypeError, operator.lshift, None, 42)
self.assertTrue(operator.lshift(5, 1) == 10)
self.assertTrue(operator.lshift(5, 0) == 5)
self.assertRaises(ValueError, operator.lshift, 2, -1)
def eval_BINARY_LSHIFT(self, instr):
return self.binop(operator.lshift, instr)
def lshift(x, n):
"""For an integer x, calculate x << n. Unlike the plain Python
expression (x << n), n is allowed to be negative, in which case a
right shift with default (floor) rounding is performed."""
if n >= 0: return x << n
else: return x >> (-n)
def __lshift__(self, y):
return NonStandardInteger(operator.lshift(self.val, y))
def __rlshift__(self, y):
return NonStandardInteger(operator.lshift(y, self.val))
def __lshift__(self, other):
return self._o2(other, operator.lshift)
def __rlshift__(self, other):
return self._r_o2(other, operator.lshift)
def lshift(x, n):
"""For an integer x, calculate x << n. Unlike the plain Python
expression (x << n), n is allowed to be negative, in which case a
right shift with default (floor) rounding is performed."""
if n >= 0: return x << n
else: return x >> (-n)
def make_env():
''' An environment with basic procedures. '''
import math
import cmath
import itertools
import operator as op
env = Environment()
env.update(vars(math))
env.update(vars(cmath))
env.update(vars(itertools))
env.update({
'>': op.gt, '<': op.lt,
'>=': op.ge, '<=': op.le,
'=': op.eq,
'>>': op.rshift, '<<': op.lshift,
'+': lambda *x: reduce(op.add, x, 0),
'-': lambda *x: x[0] - sum(x[1:]),
'*': lambda *x: reduce(op.mul, x, 1),
'/': lambda *x: reduce(op.truediv, (x[1:]), x[0]),
'//': lambda *x: reduce(op.floordiv, (x[1:]), x[0]),
'%': op.mod,
'abs': abs,
'append': op.add,
'apply': lambda proc,l: proc(*l),
'begin': lambda *x: x[-1],
'bool?': lambda x: isinstance(x, bool),
'call/cc': callcc,
'car': lambda x: x[0],
'cdr': lambda x: x[1:],
'cons': lambda x,y: [x] + y,
'filter': lambda f,l: list(filter(f, l)),
'length': len,
'list': lambda *x: list(x),
'list?': lambda x: isinstance(x,list),
# Map can be defined in the stdlib, though it will max out python's recursion depth
'map': lambda f,l: list(map(f, l)),
'max': max,
'min': min,
'not': op.not_,
'number?': lambda x: not isinstance(x, bool) and isinstance(x, int) or isinstance(x, float) or isinstance(x, complex),
'or': op.or_,
'proc?': callable,
'range': lambda *x: list(range(x[0], x[1])) if len(x) > 1 else list(range(x[0])),
'round': round,
'str?': lambda x: isinstance(x, str),
'sum': lambda x: sum(x),
})
return env
# Create a global env for `gazeval()` to access