HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Simple, pythonic IRC interface

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
interfacepythonicsimpleirc

Problem

All of the other Python IRC packages I've seen out there are overly complicated for simple tasks, and they don't abstract away much of the complexity of the protocol and asynchronicity. I've taken a crack at a better interface, but I'm new to the IRC protocol. It runs under Python2 and 3. How's it look?

```
"""
simple_irc: a simple, Pythonic IRC interface.

This module contains 2 classes: an IRC adapter, and a message wrapper.

Basic usage:
>>> irc = simple_irc.IRC('mynick', '#python', 'irc.myserver.net')
>>> for msg in irc: print(msg.sender + ': ' + msg)
somebody: Hello, simple_irc!
somebody_else: Oh hai there!
>>> irc.write('Hi guys!')
"""

import socket, threading, time, sys
if sys.version_info.major unread messages.
If limit is unspecified, returns all unread messages.'''
i = 0
l = []
for m in self: #self is an iterable!
l.append(m)
i += 1
if limit is not None and i>=limit:
break

return l

def write(self, msg):
'''Write a single message to the connected channel'''
self._writequeue.put(msg)

def writeall(self, msgs):
'''Write all messages in an iterable to the connected channel'''
for m in msgs:
self.write(m)

def close(self):
'''Close the IRC connection'''
self._closed = True
time.sleep(2*_wait) #wait for threads to terminate
self._soc.send("QUIT\r\n".encode('UTF-8'))
self._soc.close()
del self._writequeue, self._readqueue

@property
def closed(self):
'''True if the IRC connection is closed'''
return self._closed

def __enter__(self):
return self

def __exit__(self, exception_type, exception_value, traceback):
self.close()
return True

class message(str):
'''An IRC message.

Properties:
- sender: the sender
- hostname: the hostname of the sender
'''
def __new__(cls, raw):
self = str.__new__(cls, raw.split(':')[2].rstrip())
raw = raw.split(':')
self.sender = raw[1].split('!')[0]
self.hostname = r

Solution

It's hard to read this code because it doesn't follow PEP8.
The violations that stick in the eye the most:

  • Indentation should be 4 spaces



  • Class names should follow CamelCase (message violates that)



  • Do break lines at :, for example in else: import queue as Queue



  • Put spaces around operators, for example 400



  • Put a single blank line in front of method declarations of a class



  • Put a space after #comment in comments



There is a
pep8 command line utility in a package with the same name.
I recommend to install it and run against your script and correct all reported violations.

Some other tips:

  • You do self._soc.send("........".encode('UTF-8')) a lot. It would be better to add a helper method so that you can do self.send("........") instead.



  • I recommend the same for the self._soc.recv calls, even though you only do that twice, for now.



  • IRC is not a great name for a bot. It doesn't have a single clear purpose as a class should. It looks headed to become a God class, doing everything. And why should it be iterable?



  • Avoid single letter variable names like l in the readall method. The letter l is probably one of the worst possible single letter variables, as it's easy mistake it for the number 1, or capital I. In this example I would recommend items` instead.

Context

StackExchange Code Review Q#69860, answer score: 2

Revisions (0)

No revisions yet.