Skip to content

Instantly share code, notes, and snippets.

@borntyping
borntyping / gist:877775
Created March 19, 2011 20:20
Command importer
import os, sys
commandlist = {}
for module in os.listdir(os.path.dirname(__file__)):
if module == '__init__.py' or module[-3:] != '.py':
continue
__import__(module[:-3], locals(), globals())
name = 'tools.'+module[:-3]
print "Importing "+name+"..."
try:
commands = getattr(sys.modules[name], 'commands')
@borntyping
borntyping / bones.py
Created March 31, 2011 20:49
Does dice stuff I suppose.
#!/usr/bin/python
import random
class Die:
def __init__(self,sides):
self.sides = sides
self.facet = False
def __str__(self):
import re
regex = {
'prefix' : re.compile("^:(?P<nick>.+)(?:!(?P<user>.+))(?:@(?P<host>.+))")
}
def read_message (message):
"""
Turn an IRC message into an object
example_arguments = ["arg1 arg2", "arg1 arg2 :Long argument with spaces in", ":Long argument with spaces in"]
if arguments[-1][0] == ':':
message = arguments[-1]
arguments = arguments[:-1]
@borntyping
borntyping / design.txt
Created January 15, 2012 16:44
Design for Tactics irc bot
Instructions
- Restricted commands may only be done by admins, on quotes that belong to the user or on quotes about the user
- Gender
- Set gender: Set the users gender in the database
- "(?=I am|My gender is) (male|female|androgynous)", "Refer to me (by name|randomly)", "Refer to me with <list of five pronouns>"
- "{nick}: I am no refering to you with the pronouns <list of five pronouns>"
- Get gender: Ask the bot if the user has get a gender
- "What is my gender?", "What gender is (?P<person>.+)?
- "You are {gender}", "Person is {gender}"
- Aliases
import re
class Event (object):
message = re.compile("^(?::(?P<prefix>\S+) )?(?P<command>\w+|\d\d\d) (?P<params>.+)$")
def __new__ (cls, line, **kwargs):
message = cls.message.search(line)
if message:
prefix, command, params = message.groups()
print prefix, command, params
@borntyping
borntyping / parser.py
Created January 31, 2012 02:40
Parser for irc messages
"""
Parses incoming IRC messages, and formats outgoing IRC messages.
"""
def parse_message (string):
""" Parse an IRC message in the form:
[:{source} ]{command}[ parameters][ :{trailing}]
"""
# Default variables for parameters and trailing,
# as it is possible neither will be assigned
"""
`argumented` provides a way of 'multiplying' functions - usually test cases -
allowing them to be called with multiple argument sets and still appear as
seperate test cases.
In the following example, each of the test cases would be replaced with two test
cases, each of which would call the test case with the given arguments.
`@argument(*args, **kwargs)`
@borntyping
borntyping / packaging.sh
Created November 25, 2012 17:36
My python packaging cheatsheet
# Includes commands for multi-version python packages
# Ignore the commands you don't need :)
# Run tests
nosetests
# Create python package
python setup.py sdist upload
sudo python setup.py install
@borntyping
borntyping / mongoengine_example.py
Created January 15, 2013 15:52
The result of me trying to get mongoengine deletion rules to work, as the documentation is a little confusing (or even misleading).
from mongoengine import *
class QuerySetProperty(object):
"""A set of documents belonging to an entity from another collection
Basically, provides the functionality a backref would provide."""
def __init__(self, cls):
self.cls = cls
def __get__(self, instance, owner):