Re: [Tutor] flag to call methods on objects?

2009-07-31 Thread Dave Angel

prasad rao wrote:

hello
 I removed the bugs.But still getting error report.


  

import mcript
  


Traceback (most recent call last):
  File "", line 1, in 
import mcript
  File "C:\Python26\mcript.py", line 78, in 
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 .



#! 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


Re: [Tutor] How to use Python 2.6 with IDLE?

2009-07-31 Thread Dick Moores
Finally, success! Thanks to help from the Ulipad list.

Python is built on wxPython. I upgraded wxPython to v2.8. Then found
that even with 2.8 I was getting an error saying that I didn't have
the comtypes module. After downloading and installing comtypes I now
have Ulipad's shell running 2.6.2 and also Ulipad running scripts
within Ulipad written in 2.6.2. See
, which shows the Ulipad's
shell window on the left, executing correctly a bit of code using a
new 2.6 feature, and also the "Messages" window showing the correct
output of the script 26.py .

I call Ulipad with a Win XP shortcut,
. The contents of the
"Target:" textbox is
"E:\Python26\pythonw.exe E:\Programs\Ulipad3.7\Ulipad.pyw" (no quotes).

Dick Moores
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] concerning help() function

2009-07-31 Thread David
Thanks to you all -- good stuff, as always!

David

Alan Gauld wrote:
>> help('operator')
>>
>> I figured this by trial and error, and I am keen to find out when the
>
> Oops, always try before posting! And don;t assume...
>
> I juast vdid and you are right, it does give help on an unimported
> module!
>
> Sorry, how embarrassing! :-)
>
> Alan G
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

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


Re: [Tutor] mnemonics to better learn Python

2009-07-31 Thread Eduardo Vieira
On Thu, Jul 23, 2009 at 1:24 PM, Che M wrote:
>
>
>> Date: Thu, 23 Jul 2009 14:05:36 +0800
>> From: ld...@gmx.net
>> To: Tutor@python.org
>> Subject: [Tutor] mnemonics to better learn Python
>>
>> Dear List,
>>
>> in order to memorize which Python sequences are mutable or immutable, I
>> focused on the SHAPE of the brackets that are associated with each type
>> of sequence.
>>
>> For instance, a *list* is characterised by square brackets, [].
>> My mnemonic device to memorize that lists are mutable is this: "the
>> brackets have sharp edges, they could be trimmed, taking their edges off".
>>
>> The same thing happens with *dictionaries* (which, okay, are not
>> sequences). Anyway, their brackets, {}, have sharp edges, hence they are
>> mutable.
>>
>> *Tuples*, in turn, have perfectly 'round' brackets, (), and these
>> brackets obviously can't be improved upon by taking anything off them.
>> Hence: tuples are immutable.
>>
>> That leaves us with *strings*, which are also not mutable. Here we have
>> no brackets, and this particular mnemonic device breaks down.
>>
>> What I am interested in is finding out whether you use similar
>> techniques, and if so, which ones? How, for examples, do you make sense
>> of all those special characters that make regular expressions powerful?
>> Do you rely on rote learning, or do you employ some other technique?
>>
>> I reckon that if we could come up with some tips and techniques as to
>> how to uncloud the thick information fog that any beginning programmer
>> has to wade through, the very first steps in learning Python could be
>> made more easy.
>>
>> What insights can you share?
>
Hello, would anybody have a good memorization technique for boolean
results? Like when using 'or'/'and' what it returns when both are
false, the last is false, etc?
I find it so hard to remember that...
Eduardo
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mnemonics to better learn Python

2009-07-31 Thread Alan Gauld


"Eduardo Vieira"  wrote


Hello, would anybody have a good memorization technique for boolean
results? Like when using 'or'/'and' what it returns when both are
false, the last is false, etc?


Hmm, I don't try to remember those, I just work it out based on 
the meaning. 


A and B is true only if both A and B are True
A or B is true if either A or B is True.

Thats it really, what's to remember?

I guess for a non native English speaker it might be more difficult 
because you need to translate and/or into the native language?
But I assume every language has an equivalent for both of those 
concepts?


In hardware engineering its more complex because you have 
nand and nor gates to deal with too, but they don't apply in 
software - at least not directly.



--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] mnemonics to better learn Python

2009-07-31 Thread Robert Lummis
Understand that "if A or B" is short for the phrase "if either A or B
is true", or more fully "if A is true or B is true". When I add "is
true" then the logic seems obvious to me without memorizing anything.

Likewise, "if A and B" means "if A and B are both true", which is the
same as "if A is true and B is true".

On Fri, Jul 31, 2009 at 12:09 PM, Alan Gauld wrote:
>
> "Eduardo Vieira"  wrote
>
>> Hello, would anybody have a good memorization technique for boolean
>> results? Like when using 'or'/'and' what it returns when both are
>> false, the last is false, etc?
>
> Hmm, I don't try to remember those, I just work it out based on the meaning.
> A and B is true only if both A and B are True
> A or B is true if either A or B is True.
>
> Thats it really, what's to remember?
>
> I guess for a non native English speaker it might be more difficult because
> you need to translate and/or into the native language?
> But I assume every language has an equivalent for both of those concepts?
>
> In hardware engineering its more complex because you have nand and nor gates
> to deal with too, but they don't apply in software - at least not directly.
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ___
> Tutor maillist  -  tu...@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Robert Lummis
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mnemonics to better learn Python

2009-07-31 Thread Dave Angel

Eduardo Vieira wrote:


  
Hello, would anybody have a good memorization technique for boolean

results? Like when using 'or'/'and' what it returns when both are
false, the last is false, etc?
I find it so hard to remember that...
Eduardo

  
I don't blame you for finding it hard to remember.  The English language 
(and probably most others, but I wouldn't know) lets people abuse these 
meanings, but programming insists you get it right.


If you think of 0 for False, and 1 for True, then
   or  is  plus   a sum is nonzero if a is 1 or if b is 
1  (or both)
   and   is  times  a product is only nonzero if both a and 
b are 1
   xoris  minusa difference is zero only if a is the 
same as b  (both True, or both False)



In simple electrical circuitry,   'or'  is switches in parallel, 'and'  
is switches in series.


In programming terms,  and is nested ifs, while or is two ifs at the 
same level, either of which does the same thing.


def and(a, b):
if  a:
 if b:
print "this is the and of the two"
return True
   else:
return False
def  or(a, b):
   if a:
print "this is the or"
return True
   if b:
print "this is the or"
return True
   return False


DaveA

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


Re: [Tutor] mnemonics to better learn Python

2009-07-31 Thread Che M



> To: tutor@python.org
> From: alan.ga...@btinternet.com
> Date: Fri, 31 Jul 2009 17:09:48 +0100
> Subject: Re: [Tutor] mnemonics to better learn Python
> 
> 
> "Eduardo Vieira"  wrote
> 
> > Hello, would anybody have a good memorization technique for boolean
> > results? Like when using 'or'/'and' what it returns when both are
> > false, the last is false, etc?
> 
> Hmm, I don't try to remember those, I just work it out based on 
> the meaning. 
> 
> A and B is true only if both A and B are True
> A or B is true if either A or B is True.
> 
> Thats it really, what's to remember?

I tend to agree, but since he asked for a mnemonic...

What Python Needs to Return a Boolean
AND:  Both I demand! 
OR:One or more.  

So,

if A and B are False:
 [think "Both I demand...are False")
if A or B are False:
 [think "One or more...are False")
if A and B are True:
 [think "Both I demand...are True")
etc.

Che


_
Windows Live™ SkyDrive™: Store, access, and share your photos. See how.
http://windowslive.com/Online/SkyDrive?ocid=TXT_TAGLM_WL_CS_SD_photos_072009___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mnemonics to better learn Python

2009-07-31 Thread Eduardo Vieira
On Fri, Jul 31, 2009 at 12:27 PM, Che M wrote:
>
>
>> To: tutor@python.org
>> From: alan.ga...@btinternet.com
>> Date: Fri, 31 Jul 2009 17:09:48 +0100
>> Subject: Re: [Tutor] mnemonics to better learn Python
>>
>>
>> "Eduardo Vieira"  wrote
>>
>> > Hello, would anybody have a good memorization technique for boolean
>> > results? Like when using 'or'/'and' what it returns when both are
>> > false, the last is false, etc?
>>
>> Hmm, I don't try to remember those, I just work it out based on
>> the meaning.
>>
>> A and B is true only if both A and B are True
>> A or B is true if either A or B is True.
>>
>> Thats it really, what's to remember?
>
> I tend to agree, but since he asked for a mnemonic...
>
> What Python Needs to Return a Boolean
> AND:  Both I demand!
> OR:    One or more.
>
> So,
>
> if A and B are False:
>  [think "Both I demand...are False")
> if A or B are False:
>  [think "One or more...are False")
> if A and B are True:
>  [think "Both I demand...are True")
> etc.
>
> Che
>
>
> 
> Windows Live™ SkyDrive™: Store, access, and share your photos. See how.
> ___
> Tutor maillist  -  tu...@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
It's not that it's so hard, but I still have to stop and think for a
while... "humm, how's that?"
Like in the IDLE
2 and 4 and 0
Result: 0
2 and 4 and 5
Result: 5
2 and 0 and 6
Result: 0
So, what I need to memorize is that with AND, if there's a false item,
then that false is returned, otherwise the last one is returned
With OR, the first one is returned if true, otherwise, the next "true"
value is returned
2 or 3 or 0
Result: 2
2 or 0 or 3
Result: 2
0 or 2 or 3
Result: 2
0 or '' or 4
Result: 4

Do you see how this can demand a little more of brainwork?

Eduardo
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mnemonics to better learn Python

2009-07-31 Thread Alan Gauld


"Eduardo Vieira"  wrote 




Like in the IDLE
2 and 4 and 0
Result: 0


Ah! Now I see. Its the results of short-circuit evaluation 
you want a nmemonic for, not the and/or logic itself


I agree thats much harder to figure out, you do kind 
of have to work through the tests until the first failure

for and or the first success for or.

It might be cute to have an aide d'memoire for that.


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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