Home arrow Python arrow Page 5 - Python and IRC

IRC Mathematics - Python

IRC is becoming an increasingly popular medium for communication. In this article, Peyton McCullough explains how to make Python and IRC work together.

TABLE OF CONTENTS:
  1. Python and IRC
  2. Hello IRC World
  3. Can You Hear Me?
  4. I Can Hear You!
  5. IRC Mathematics
By: Peyton McCullough
Rating: starstarstarstarstar / 34
February 16, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: