Example #1
0
def doubleParityChksum(data):
    """Computes horizontal an vertical parities.

    One horizontal parity bit is computed per octet. 8 vertical parity bits
    are computed. The checksum is the 8 vertical parity bits plus the
    horizontal parity bits, padded by a variable number of 0 bits to align
    on an octet boundary. Even parity is used.

    The checksum is returned as a string where each character codes a byte
    of the checksum.
    """
    bitmask = Numeric.array([128,64,32,16,8,4,2,1])
    bytes = Numeric.fromstring(data,Numeric.UnsignedInt8)
    numBytes = len(bytes)
    bits = Numeric.bitwise_and.outer(bytes,bitmask).flat
    Numeric.putmask(bits,bits,1)
    bits = Numeric.reshape(bits, (numBytes,8))
 
    verParities = Numeric.bitwise_and(Numeric.sum(bits),1)
    bits = Numeric.concatenate((bits,[verParities]))

    horParities = Numeric.bitwise_and(Numeric.sum(bits,1),1)
    if len(horParities)%8:
        horParities = Numeric.concatenate((horParities,
                                           [0]*(8-len(horParities)%8)))

    bitmask = Numeric.array([128,64,32,16,8,4,2,1])
    chksumstring = chr(Numeric.dot(verParities,bitmask))
    for i in range(len(horParities)/8):
        chksumstring += chr(Numeric.dot(horParities[i*8:(i+1)*8],bitmask))
    return chksumstring
Example #2
0
 def testOperators (self):
     "Test the operators +, -, *, /, %, ^, &, |"
     x = Numeric.array([1.,2.,3.,4.,5.,6.])
     y = Numeric.array([-1.,2.,0.,2.,-1, 3.])
     assert_eq(x + y, [0., 4., 3., 6., 4., 9.])
     assert_eq(x - y, [2., 0., 3., 2., 6., 3.])
     assert_eq(x * y, [-1., 4., 0., 8., -5., 18.])
     assert_eq(y / x, [-1, 1., 0., .5, -.2, .5])
     assert_eq(x**2, [1., 4., 9., 16., 25., 36.])
     xc = Numeric.array([1.,2.,3.,4.,5.,6.])
     xc += y
     assert_eq(xc, x + y)
     xc = Numeric.array([1.,2.,3.,4.,5.,6.])
     xc -= y
     assert_eq(xc, x - y)
     xc = Numeric.array([1.,2.,3.,4.,5.,6.])
     xc *= y
     assert_eq(xc, x * y)
     yc = Numeric.array(y)
     yc /= x
     assert_eq( yc, y / x)
     assert_eq(x + y, Numeric.add(x, y))
     assert_eq(x - y, Numeric.subtract(x, y))
     assert_eq(x * y, Numeric.multiply(x, y))
     assert_eq(y / x, Numeric.divide (y, x))
     self.failUnlessRaises(ZeroDivisionError, Numeric.divide, 
                           Numeric.array(1), Numeric.array(0))
     assert_eq(x**2, Numeric.power(x,2))
     x = Numeric.array([1,2])
     y = Numeric.zeros((2,))
     assert_eq(x%x, y)
     assert_eq(Numeric.remainder(x,x), y)
     assert_eq(x <<1, [2,4])
     assert_eq(Numeric.left_shift(x,1), [2,4])
     assert_eq(x >>1, [0,1])
     assert_eq(Numeric.right_shift(x,1), [0,1])
     assert_eq(x & 2, [0,2])
     assert_eq(Numeric.bitwise_and (x, 2), [0,2])
     assert_eq(x | 1, [1,3])
     assert_eq(Numeric.bitwise_or (x, 1), [1,3])
     assert_eq(x ^ 2, [3,0])
     assert_eq(Numeric.bitwise_xor(x,2), [3,0])
     x = divmod(Numeric.array([2,1]), Numeric.array([1,2]))
     assert_eq(x[0], [2,0])
     assert_eq(x[1], [0,1])
     assert (4L*Numeric.arange(3)).typecode() == Numeric.PyObject
     x = Numeric.array([1,2,3,4,5,6],'u')
     y = Numeric.array([1,2,0,2,2,3],'u')
     assert_eq(x + y, [2, 4, 3, 6, 7, 9])
     assert_eq(x - y, [0, 0, 3, 2, 3, 3])
     assert_eq(x * y, [1, 4, 0, 8, 10, 18])
     assert_eq(y / x, [1, 1, 0, 0, 0, 0])
     assert_eq(y // x, [1, 1, 0, 0, 0, 0])
     assert_eq(x**2, [1, 4, 9, 16, 25, 36])