Noobish Python help pl0x

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Noobish Python help pl0x

Post by centip3de on Mon Nov 29, 2010 6:23 pm
([msg=49612]see Noobish Python help pl0x[/msg])

Alright, so this is pretty noobish, and somewhat embarrassing, but I've been racking my brains, books, and Google trying to find an answer. So basically, I devised a way to control my computer via text messages. For instance, if I send a text that says start "hackthissite.py" it would open a file called "hackthissite.py" which would open a browser directed to hackthissite.org. Not too shabby eh? ;) It uses mozilla thunderbird and an add-on for it. (If anyones interested I'll go into greater detail)


Anyway! Back to the crux of the matter. I don't want to use the other programs, because really their just a crutch :/ . So far I've gotten it to retrieve my mailbox, get the emails, place them in a text file and read them. Here's what I want to happen (Mixed with my current code):

Code: Select all
import getpass, poplib, sys, os, time

Mailbox = poplib.POP3_SSL("pop.email_provider.com", 995)
Mailbox.user("MY_EMAIL")
Mailbox.pass_("MY_PASS")
messageCount = len(Mailbox.list() [1])
for i in range(messageCount):
      for j in Mailbox.retr(i+1) [1]:
            ff = open('Mail.txt', 'a')
            ff.write(j)
            ff.close()
                     
infile = open('Mail.txt', "r")
text = infile.read()
infile.close
search = "test"
index = text.find(search)
if text.content(search):
Do cool stuffz here


The "if text.content(search):" Part is pseudo code obv. Anywho, thats the wishful thinking part, the ability to say if "Mail.txt" has "search" in it, then do cool stuffz. But I can't code the if statment -.-

Help a noob pl0x :D

Thanks.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1425
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Noobish Python help pl0x

Post by fashizzlepop on Mon Nov 29, 2010 6:50 pm
([msg=49613]see Re: Noobish Python help pl0x[/msg])

Just a guess but I'm thinking you need to use a regular expression. Try googleing for Python and reg ex.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: Noobish Python help pl0x

Post by insomaniacal on Mon Nov 29, 2010 7:02 pm
([msg=49617]see Re: Noobish Python help pl0x[/msg])

Theres probably an easier way to do this, but just off the top of my head, you could do the following:



1: Break up the words in Mail.txt to a list using the string library.
2: Break up the words in search to a list using the string library.
3: len (search)
4: Compare the lists and keep track of how many matches there are. If the # of matches = len(search), then you've found whatever you're looking for.
IF:
Comparison finds a match (Have a variable be set to 1 if it does):
Do Stuff

ELSE:

1:Whatever you want to happen, probably pass or something.

There's gotta be a pre-built function for something like this, but this method would also work, although it would be kinda slow and messy :/.
It's not who votes that counts, it's who counts the votes
insomaniacal.blog.com
User avatar
insomaniacal
Addict
Addict
 
Posts: 1210
Joined: Sun May 24, 2009 10:21 am
Blog: View Blog (0)


Re: Noobish Python help pl0x

Post by centip3de on Mon Nov 29, 2010 7:13 pm
([msg=49619]see Re: Noobish Python help pl0x[/msg])

insomaniacal wrote:There's gotta be a pre-built function for something like this, but this method would also work, although it would be kinda slow and messy :/.


Yea thanks for the help :) It probably would work, a bit too messy for my taste though. ;)
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1425
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Noobish Python help pl0x

Post by mojo1948 on Mon Nov 29, 2010 7:18 pm
([msg=49620]see Re: Noobish Python help pl0x[/msg])

Depending on what your trying to do, something like this might work.
Code: Select all
text = 'anahsyTESTamjdsy'
search = 'TEST'
index = text.find(search)
print 'index = ',index
if index == -1:
   print 'FAIL'
else:
   print text[index:index+len(search)]
   print 'Do cool stuffz here'
Never stop learning.
User avatar
mojo1948
Experienced User
Experienced User
 
Posts: 60
Joined: Sun Jul 18, 2010 5:45 am
Blog: View Blog (0)


Re: Noobish Python help pl0x

Post by centip3de on Mon Nov 29, 2010 7:33 pm
([msg=49621]see Re: Noobish Python help pl0x[/msg])

mojo1948 wrote:Depending on what your trying to do, something like this might work.
Code: Select all
text = 'anahsyTESTamjdsy'
search = 'TEST'
index = text.find(search)
print 'index = ',index
if index == -1:
   print 'FAIL'
else:
   print text[index:index+len(search)]
   print 'Do cool stuffz here'


I've seen something like this before, but I was unclear on what the -1 part of it meant. So I let it go hoping to find a way I understood..... o.O
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1425
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Noobish Python help pl0x

Post by mojo1948 on Mon Nov 29, 2010 7:57 pm
([msg=49622]see Re: Noobish Python help pl0x[/msg])

The -1 is the value that is returned when the find function doesn't find a match. Return vales vary depending on what function you are calling. Try changing TEST to TAST and you will see what I mean.
Never stop learning.
User avatar
mojo1948
Experienced User
Experienced User
 
Posts: 60
Joined: Sun Jul 18, 2010 5:45 am
Blog: View Blog (0)


Re: Noobish Python help pl0x

Post by Defience on Mon Nov 29, 2010 9:44 pm
([msg=49624]see Re: Noobish Python help pl0x[/msg])

I'd use regular expression as fashizzlepop suggested:

Code: Select all
import re

infile=open("Mail.txt","r")
lines=infile.readlines()
for line in lines:
    if re.search("test",line):
        print "Let's do coolz stuff here!"
User avatar
Defience
Addict
Addict
 
Posts: 1281
Joined: Thu Jun 12, 2008 3:16 pm
Blog: View Blog (0)


Re: Noobish Python help pl0x

Post by centip3de on Mon Nov 29, 2010 9:50 pm
([msg=49626]see Re: Noobish Python help pl0x[/msg])

Defience wrote:I'd use regular expression as fashizzlepop suggested:

Code: Select all
import re

infile=open("Mail.txt","r")
lines=infile.readlines()
for line in lines:
    if re.search("test",line):
        print "Let's do coolz stuff here!"


<3

***EDIT

Damn, I now have the error:

Traceback (most recent call last):
File "C:\Python26\POP3.py", line 15, in <module>
for line in lines:
NameError: name 'lines' is not defined


Current code:
Code: Select all
import getpass, poplib, sys, os, time, re

Mailbox = poplib.POP3_SSL("XXXX", 995)
Mailbox.user("XXXX")
Mailbox.pass_("XXXX")
messageCount = len(Mailbox.list() [1])
for i in range(messageCount):
      for j in Mailbox.retr(i+1) [1]:
            ff = open('Mail.txt', 'a')
            ff.write(j)
            ff.close()
                     
infile = open('Mail.txt', "r")
text = infile.readlines()
for line in lines:
    if re.search("test",line):
        print "Let's do coolz stuff here!"
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1425
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Noobish Python help pl0x

Post by tgoe on Mon Nov 29, 2010 10:42 pm
([msg=49632]see Re: Noobish Python help pl0x[/msg])

Re-read the post by Defience carefully
User avatar
tgoe
Contributor
Contributor
 
Posts: 650
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Next

Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests