[Tutor] need a little help/OOPS!

Tim Johnson tim at johnsons-web.com
Mon Dec 29 19:06:35 EST 2003


* Tim Johnson <tim at johnsons-web.com> [031229 15:04]:
> * matteo <matteolovatti at mac.com> [031229 14:18]:
> > hi everybody i'm new to the list 
> > i'm now starting to build my first  "pyhtons" but i'm hang with this... 
> > 
> > import poplib 
> > 
> > M = poplib.POP3("serverpop")  
> > 
> > M.user("aaa")  
> > 
> > M.pass_("bbb")  
> > 
> > numMessages = len(M.list()[1])  
> > for i in range(numMessages):  
> > 	for j in M.retr(i+1)[1]:  
> > 		print j 
> > 
> > i really would like to have j printed only if the line in the "j" 
> > variable begins with the string "subject:" 
> > but i'm not able to do that  
> > can someone please help me ? 
OOPS! I got a little ahead of myself, not 
'binding' the console session to your example.
I should have use 'j' as the variable name
See below:
> Hi Matteo: 
>   Below is a console session that might help.
>   Since I don't have a working version of your code to
>   test with.
>   I proceed with the assumption that `j` is a string.
> >>> j = "subject: Question"
> >>> j.find("subject:")
> 0     # succesful find, index is 0
> 
> # Example of not found
> >>> j.find("Subject:")
> -1
> 
> You cound get around case sensitivity
> by forcing both string to upper case
> or lower case
> >>> j.upper().find("Subject:".upper())
> 0
> 
> So your test construct might be something like this:
> >>> if not j.upper().find("Subject".upper()):
> ...     print j
> ...
> subject: Question
> 
> # Yikes! That's counterintuitive, isn't it.
> # That's why I might write a function like:
> def is_head(Str,sub):
>     found = Str.find(sub)
>     if found == 0: return 1
>     else: return None    # OR return 0 if you like
>     # Notice that this function doesn't address 
>     # Case, an optional argument for case
>     # Sensitivity would be in order
> 
> Then our test would be    #untested code
> if is_head(j.upper(),"Subject".upper()):
>     print j
> 
> I hope this helps a bit. Try it with substring
> "Question" , and see what happens
> 
> Tim
>   
> > thanks 
> > 
> > 
> > matteo 
> > 
> 
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> 
> 
> -- 
> Tim Johnson <tim at johnsons-web.com>
>       http://www.alaska-internet-solutions.com
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Tim Johnson <tim at johnsons-web.com>
      http://www.alaska-internet-solutions.com



More information about the Tutor mailing list