[Tutor] flag to call methods on objects?

Dave Angel davea at ieee.org
Thu Jul 30 17:02:46 CEST 2009


prasad rao wrote:
> hello
>     I wanted to prevent other users of my computers to read my files.
> So tried to creat a module to achieve it.
> I used a flag to deside which methods to be called on the object.
> somehow it is not working.I realise after 2 days of struggle that
> as  it is usuel , I am blind to my faults.
> I tested all the metheds except main() in idle interactively,found them
> working.
> So there is a bug in the main(), kan anyone point it out to me?
>
> <code>
>
>
> #! usr/bin/env python
>
> import ast,random,os,zlib,string
> key=5
> class Cripto(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[:1]=='flag1\n':
>             ns='flag0\n'
>             data=data[1:]
>             for line in data:
>                nl=__compress((__digi(__lengthen(line.strip()))))+'\n'
>                ns+=nl
>             dest.write(ns)
>         elif data[:1]=='flag0\n':
>             ns='flag1\n'
>             data=data[1:]
>             for line in data:
>                 nl=__decompress((__undigi(__shorten(line.strip()))))+'\n'
>                 ns+=nl
>             dest.write(ns)
>
>         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>
>
>   
First problem is the indentation.  It won't compile as it is.  I'll 
assume that was somehow a pasting error.

Next problem is where you're comparing for 'flag1'    Your line   if 
data[:1] == 'flag1\n'     is comparing a list on the left side to a 
string on the right.  If it were me, I'd replace that test, and the 
similar one below it, with
        if data[0].rstrip()=='flag1':


The reason for the rstrip() is to make it easier for a test harness, 
where it may be a pain to get the newlines on each line.  Also, it's 
harder to spot a whitespace error.

Next problem/question is whether this is only going to be used for files 
that happen to begin with a line     flag1.   If so, it should be 
documented as part of the code, along with all the other missing 
documentation/docstrings.


That's as far as I went;  I did not test the actual encoding logic  But 
some free advice for next time:    When you ask a question, please 
supply a compilable program, and either test data, or enough description 
that we can build such.  And please tell us what the symptom is.

"it is not working."  is not very descriptive.


DaveA


More information about the Tutor mailing list