From 7f46e1c35c8c9d3c24d55908af884ece65fe4add Mon Sep 17 00:00:00 2001 From: James Lu Date: Sun, 24 Apr 2016 21:11:36 -0700 Subject: [PATCH] Separate KeyedDefaultdict into new structures module (#199) --- classes.py | 3 ++- structures.py | 19 +++++++++++++++++++ utils.py | 14 -------------- 3 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 structures.py diff --git a/classes.py b/classes.py index 063ce16..c7519f5 100644 --- a/classes.py +++ b/classes.py @@ -19,6 +19,7 @@ from copy import deepcopy from log import * import world import utils +import structures ### Exceptions @@ -113,7 +114,7 @@ class Irc(): internal=True, desc=self.serverdata.get('serverdesc') or self.botdata['serverdesc'])} self.users = {} - self.channels = utils.KeyedDefaultdict(IrcChannel) + self.channels = structures.KeyedDefaultdict(IrcChannel) # This sets the list of supported channel and user modes: the default # RFC1459 modes are implied. Named modes are used here to make diff --git a/structures.py b/structures.py new file mode 100644 index 0000000..8717baf --- /dev/null +++ b/structures.py @@ -0,0 +1,19 @@ +""" +structures.py - PyLink data structures module. + +This module contains custom data structures that may be useful in various situations. +""" + +import collections + +class KeyedDefaultdict(collections.defaultdict): + """ + Subclass of defaultdict allowing the key to be passed to the default factory. + """ + def __missing__(self, key): + if self.default_factory is None: + # If there is no default factory, just let defaultdict handle it + super().__missing__(self, key) + else: + value = self[key] = self.default_factory(key) + return value diff --git a/utils.py b/utils.py index 5b0c517..e3d6279 100644 --- a/utils.py +++ b/utils.py @@ -10,25 +10,11 @@ import re import inspect import importlib import os -import collections from log import log import world import conf -class KeyedDefaultdict(collections.defaultdict): - """ - Subclass of defaultdict allowing the key to be passed to the default factory. - """ - def __missing__(self, key): - if self.default_factory is None: - # If there is no default factory, just let defaultdict handle it - super().__missing__(self, key) - else: - value = self[key] = self.default_factory(key) - return value - - class NotAuthenticatedError(Exception): """ Exception raised by checkAuthenticated() when a user fails authentication