예제 #1
0
import sys
import random
import numpy as np
import PS1bin
import PS13mycode

if __name__ == "__main__":
    for maxInput, tableSize in [(2,5), (3,10)]:
        print '\n\ntesting with maxInput=%d, tableSize=%d'%(maxInput,tableSize)
        iA = range(maxInput)
        cA = range(tableSize)
        for i in xrange(10):
            print '..'
            msg = [random.choice(iA) for j in xrange(20)]
            cmsg = PS13mycode.compress(msg,maxInput,tableSize)
            assert type(cmsg)==type([]), "encoded message not a list"
            for x in cmsg:
                assert x in cA, "coded message out of range"
            dmsg = PS13mycode.uncompress(cmsg,maxInput,tableSize)
            assert type(dmsg)==type([]), "decoded message not a list"
            for x in dmsg:
                assert x in iA, "decoded message out of range"
            print msg, '->', cmsg
            assert PS1bin.chk3(msg,cmsg,maxInput,tableSize), "incorrect encoding"
            print dmsg
            assert len(msg)==len(dmsg), "incorrect length after decoding"
            for i in xrange(len(msg)):
                assert msg[i]==dmsg[i], "incorrect decoding"
                
    print '\n\nGREAT! NO ERRORS FOUND'  
예제 #2
0
import sys
import PS13mycode

if __name__ == "__main__":
    nargin = len(sys.argv)
    if nargin == 1:
        print """
usage:
python PS1lzw.py filename
python PS1lzw.py u filename
              """
    else:
        filename = sys.argv[1]
        input_file = open(filename,'rb')
        msg = [ord(x) for x in input_file.read()]
        input_file.close()
        if nargin == 2:
            cmsg = PS13mycode.compress(msg,256,65536)
            newmsg = ''.join(["%s%s"%(chr(x/256),chr(x%256)) for x in cmsg])
            newfile = filename+'.zl'
        else:
            cmsg = [256*msg[i]+msg[i+1] for i in xrange(0,len(msg),2)]
            umsg = PS13mycode.uncompress(cmsg,256,65536)
            newmsg = ''.join([chr(x) for x in umsg])
            newfile = filename+'.u'
        output_file = open(newfile,'wb')
        output_file.write(newmsg)
        output_file.close()