Python
  Home arrow Python arrow Page 5 - Python and IRC
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python and IRC
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 33
    2005-02-16


    Table of Contents:
  • Python and IRC
  • Hello IRC World
  • Can You Hear Me?
  • I Can Hear You!
  • IRC Mathematics

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Python and IRC - IRC Mathematics
    ( Page 5 of 5 )

    Let's apply our knowledge to something useful – a Python-powered IRC bot that performs basic mathematical functions. Let's make the bot perform arithmetic calculations and a few trigonometric functions: sine, cosine and tangent.

    We will first create a file to perform the calculations for us. Create a file named ircMath.py and insert the following code.

    import math

    def arithmatic ( args ):

       args [ 0 ] = args [ 0 ].replace ( '\r\n', '' )
       for letter in 'abcdefghijklmnopqrstuvwxyz':
          args [ 0 ] = args [ 0 ].replace ( letter, '' )
       solution = str ( eval ( args [ 0 ], { '__builtins__' : {} } ) )
       return solution

    def sine ( args ):

       solution = str ( math.sin ( float ( args [ 0 ] ) * ( 2 * math.pi ) / 360 ) )
       return solution

    def cosine ( args ):

       solution = str ( math.cos ( float ( args [ 0 ] ) * ( 2 * math.pi ) / 360 ) )
       return solution

    def tangent ( args ):

       solution = str ( math.tan ( float ( args [ 0 ] ) * ( 2 * math.pi ) / 360 ) )
       return solution

    The guts of ircMath.py aren't too important, and the code should be pretty straightforward, so I won't get into detail.

    We will now create the bot. Its code will be a modified version of the last section's script. We will search the incoming message for the string "%PyIRC" to discriminate between messages we do and do not need. We will then split up the message into a function and arguments. Finally, we will call the appropriate function.

    import ircMath
    import socket

    network = 'irc.insert.a.network.here'
    port = 6667
    irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
    irc.connect ( ( network, port ) )
    irc.recv ( 4096 )
    irc.send ( 'NICK PyIRC\r\n' )
    irc.send ( 'USER PyIRC PyIRC PyIRC :Python IRC\r\n' )
    irc.send ( 'JOIN #pyirc\r\n' )
    irc.send ( 'PRIVMSG #pyirc :Hello.\r\n' )
    while True:
       data = irc.recv ( 4096 )
       if data.find ( 'PING' ) != -1:
          irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )
       elif data.find ( 'PRIVMSG' ) != -1:
          message = ':'.join ( data.split ( ':' ) [ 2: ] )
          if message.lower().find ( '%pyirc' ) == 0:
             nick = data.split ( '!' ) [ 0 ].replace ( ':', '' )
             destination = ''.join ( data.split ( ':' ) [ :2 ] ).split ( ' ' ) [ -2 ]
             function = message.split ( ' ' ) [ 1 ]
             if function == 'calc':
                try:
                   args = message.split ( ' ' ) [ 2: ]
                   solution = ircMath.arithmatic ( args )
                   irc.send ( 'PRIVMSG ' +  destination + ' :' + nick + ': ' + solution + '\r\n' )
                except:
                   pass
             if function == 'sin':
                try:
                   args = message.split ( ' ' ) [ 2: ]
                   solution = ircMath.sine ( args )
                   irc.send ( 'PRIVMSG ' +  destination + ' :' + nick + ': ' + solution + '\r\n' )
                except:
                   pass
             if function == 'cos':
                try:
                   args = message.split ( ' ' ) [ 2: ]
                   solution = ircMath.cosine ( args )
                   irc.send ( 'PRIVMSG ' +  destination + ' :' + nick + ': ' + solution + '\r\n' )
                except:
                   pass
             if function == 'tan':
                try:
                   args = message.split ( ' ' ) [ 2: ]
                   solution = ircMath.tangent ( args )
                   irc.send ( 'PRIVMSG ' +  destination + ' :' + nick + ': ' + solution + '\r\n' )
                except:
                   pass

    Start up the script and enter the specified channel on the specified network. Try saying these lines:

    %PyIRC calc 2+2
    %PyIRC calc 8+10
    %PyIRC sin 30
    %PyIRC cos 45
    %PyIRC tan 27

    Our bot should give us the answer to each of the problems. Pretty neat, huh?

    Conclusion

    You should now know the basics of Python and IRC connectivity. Try to expand on the examples provided in this article to create your own unique scripts. If you would like to learn more about the IRC protocol, the protocol is documented here:

    http://www.irchelp.org/irchelp/rfc



     
     
    >>> More Python Articles          >>> More By Peyton McCullough
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT