SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old January 30th, 2004, 06:46 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Parsing output

Is there any way for me to parse this output

Code:
chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello


to get the first word, and everything after it? For example, in the end "arg[0] == .say" and "arg[1] == 'Hello'"

Reply With Quote
  #2  
Old January 30th, 2004, 07:04 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Can you mark in bold the bits you want? Not sure exactly waht you want here although i can tell you it's not gonna be very hard.

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old January 30th, 2004, 07:18 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hehe I was hoping it wouldn't be, but I'm a real noob with regex (that's what I tried).

Code:
chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello


I basicaly need the ".say" in arg[0] (or any other command it may be) and then the argument(s) in arg[1], arg[2], etc. In this case it would be "Hello" in arg[1]. Use another storage method if you know a better one

Thanks in advance

Reply With Quote
  #4  
Old January 30th, 2004, 07:31 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Ok here it is, this may yet need to be changed depending on format of other commands but it works perfectly on this...

Code:
>>> parse = 'chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello'
>>> index = parse.find(':')
>>> value = parse[index + 1:]
>>> command, message = value.split()
>>> command
'.say'
>>> message
'Hello'
>>> 


This is because it works by finding the first ':' char and using it as an index; since i don't know enough about IRC commands format.

Mark.

Reply With Quote
  #5  
Old January 30th, 2004, 08:24 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ahhhh! I forgot a ':' at the beginning of the string... sorry about that? I hope it isn't hard to fix.

It all seems to look like this

Code:
:chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello

Reply With Quote
  #6  
Old January 31st, 2004, 04:11 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Naw not hard, just needs a different aproch

Code:
>>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello'
>>> value = parse.split(':')
>>> value = value[-1]
>>> command, message = value.split()
>>> command
'.say'
>>> message
'Hello'
>>> 


Note: This should work, and with any similar format (string:.command value).

Mark.

Reply With Quote
  #7  
Old January 31st, 2004, 08:46 AM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks alot Mark!

Reply With Quote
  #8  
Old January 31st, 2004, 08:55 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Another method to remember when searching through strings is that you can simplify a lot of problems if you reverse the string, then search, then reverse the result. This time, it's easy to do without; other times, it's invaluable.

Reply With Quote
  #9  
Old January 31st, 2004, 11:41 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Perc, why not just use the rfind() string-method to search the string from the right?

Code:
>>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello'
>>> index = parse.rfind(':') + 1
>>> value = parse[index:]
>>> command, message = value.split()
>>> command
'.say'
>>> message
'Hello'
>>> 


It seems so much simpler than doing...

Code:
>>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello'
>>> value = parse[::-1]
>>> index = value.find(':')
>>> value = value[:index]
>>> value = value[::-1]
>>> command, message = value.split()
>>> command
'.say'
>>> message
'Hello'
>>> 


...like you're recomending. So no need to manually reverse the string or results

Mark.

Reply With Quote
  #10  
Old January 31st, 2004, 12:53 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Nice work netytan

btw, where did you learn the 'PRIVMSG', 'JOIN', etc stuff for your bot?

Reply With Quote
  #11  
Old January 31st, 2004, 12:53 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Except that searching for the first ":" from the right isn't the right way to parse IRC messages

First, a quick caveat. I know you're just playing around with this stuff to learn python, and that's cool and all. But, please realize that there are good IRC libraries out there that do this, so don't agonize over making a new one that does all this

Okay, back to the topic at hand. You'll want to read the IRC RFC (1763, I think) to figure out the message structure. But the rightmost colon isn't necessarily what you are looking for. This is a valid message, for example:

Code:
:chris_!~chris@bleh.net PRIVMSG #thisisatest :This is a valid message: foo


And " foo" isn't the message you want, you want "This is a valid message: foo".
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
  #12  
Old January 31st, 2004, 01:10 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hmm.. well that complicated things

I'm not planning this to be a complicated bot so I want to try and write it without other libs. I just need the parser working right.

By the way, wouldn't this work?

Code:
>>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello:Hi'
>>> index = parse.rfind(':.') + 1
>>> value = parse[index:]
>>> value = value.split()
>>> value             
['.say', 'Hello:Hi']

(netytan's code)

Then, say the command was ".say" I could do something like this:

Code:
>>> command = value[0]
>>> text = ' '.join(value[1:])
>>> command
'.say'
>>> text
'Hello:Hi'

Last edited by XxChris : January 31st, 2004 at 01:22 PM.

Reply With Quote
  #13  
Old January 31st, 2004, 05:03 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
To be honest i know nothing about IRC commands; I was talking to Xlord on yahoo and he was explaining his functions so i just asked him some questions and wrote the IRC class for him

Anyway just thought i'd mention that you can loose the whole join() method call etc by telling split() on the first space...

Code:
>>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello:Hi baby'
>>> index = parse.rfind(':.') + 1
>>> value = parse[index:]
>>> command, message = value.split(' ', 1)
>>> comamnd
'.say'
>>> message
'Hello:Hi baby'
>>> 


Nothing wrong with rewriting things - i do it all the time - its a good way to learn. Plus, sometimes you end up with a better version than whats already out there. I mean.. would you have the IRC libs you have today if everyone just stuck with the first one?

Mark.

Reply With Quote
  #14  
Old January 31st, 2004, 05:28 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
what is the '.' at the beginning of the message for?

Reply With Quote
  #15  
Old January 31st, 2004, 05:37 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)