prasad rao wrote:
hello
 I removed the bugs.But still getting error report.


import mcript

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    import mcript
  File "C:\Python26\mcript.py", line 78, in <module>
    a.main()
  File "C:\Python26\mcript.py", line 58, in main
    nl=__compress(__digi(__lengthen(line.strip())))+'\n'
NameError: global name '_Cripto__compress' is not defined

There is no string  '_Cripto__compress'  any whare in the script.

So I dont know what corrections Ineed to make in the script.

I am giving my code below again again .

<code>

#! usr/bin/env python

import ast,random,os,zlib,string
key=5
class Craipto(object):
    def __init__(self,afile):
        self.afile=afile
    def __digi(self,astring):
        y=[]
        for x in astring:
            y.append(ord(x))
        y=str(y)

        return y
    def __undigi(self,astring):

        alist=ast.literal_eval(astring)
        y=[]
        for x in alist:
            y.append(chr(x))
        astring=''.join(y)
        return astring
    def __gen_string(self):
        s=''
        nl=random.sample(string.ascii_letters,key)
        for x in nl:s+=x
        return s

    def __lengthen(self,astring):
         s=list(astring)
         ns=''
         for x in s:
             ns+=x
             ns+=gen_string()
         return ns
    def __shorten(self,astring):

         s=list(astring)
         ns=''
         for x in range(0,len(s),key+1):
            ns+=s[x]
        return ns
    def __compress(self,astring):
        astring=zlib.compress(astring)
        return astring
    def __decompress(self,astring):
        astring=zlib.decompress(astring)
        return astring
    def main(self):
        sorce=open(self.afile,'r')
        data=(sorce.readlines())
        dest=open((os.path.split(self.afile)[0]+os.sep+'temp'),'w')
        if (data[0]).strip()=='flag1':

            ns='flag0\n'
            data=data[1:]
            for line in data:
               nl= __compress(__digi(__lengthen(line.strip())))+'\n'
               ns+=nl
            dest.write(ns)
        elif data[0].strip()=='flag0':
            ns='flag1\n'
            data=data[1:]
            for line in data:
                nl= __shorten((__undigi(__decompress(line.strip()))))+'\n'
                ns+=nl
            dest.write(ns)
        else:prind 'File does not begin with the flag'

        sorce.close()
        dest.close()

        os.remove(self.afile)
        os.rename((os.path.split(self.afile)[0]+os.sep+'temp'),self.afile)


#========
a=Cripto('C:/pp.txt')
a.main()

<\code>

I still see four problems. Your runtime error occurs because you omitted the "self" on the call to self.__compress(). In fact, you omit it nearly everywhere. Without it, Python will look for a global name, which doesn't exist.

Second problem is that you spelled "print" as "prind" in your else clause.

Third is that when you do detect that the file doesn't begin properly, you still truncate the file to nothing. The else clause should be roughly:
    else:
       print "File is malformed, no signature at beginning"
       sorce.close()
       dest.close()
       os.remove(                 the temp file       )
       return

Fourth is that you're separating the "lines" of the encoded file with a newline character, but I suspect that zlib.compress makes no promises about avoiding that character(0a). If I'm right, you'll need another approach. Either escape the byte sequence in __compress(), or use a count instead of a separator. In either case, you'll need binary mode for that part of the file I/O, if you want to be able to run on Windows.

There are a few other comments I could make about the code. First, why did you make it a class? Perhaps your last language was Java, which forces you into that particular paradigm? You never use self in any of the methods except for __init__() and main(). And once main() is finished, you now longer need the object. That should tell you that you could make them all regular functions, and pass the filename directly to main().

Next, why the double-underscore prefix on most of the methods? The only effect that has on execution is that the error message shows a "mangled" version of the function name. When it's a global, it mangles it with the module name (_Cripto__compress), and when it's a method, it uses the class name.

DaveA
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to