mirror of
https://github.com/jlu5/PyLink.git
synced 2025-01-11 20:52:42 +01:00
structures: rework classes & implement (IRC)CaseInsensitiveSet
This commit is contained in:
parent
1cdb5fc025
commit
2700e42ebf
@ -26,7 +26,39 @@ class KeyedDefaultdict(collections.defaultdict):
|
|||||||
value = self[key] = self.default_factory(key)
|
value = self[key] = self.default_factory(key)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
class CaseInsensitiveDict(collections.abc.MutableMapping):
|
class CaseInsensitiveFixedSet(collections.abc.Set):
|
||||||
|
"""
|
||||||
|
Implements a fixed set storing items case-insensitively.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, *, data=None):
|
||||||
|
if data is not None:
|
||||||
|
assert isinstance(data, set)
|
||||||
|
self._data = data
|
||||||
|
else:
|
||||||
|
self._data = set()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _keymangle(key):
|
||||||
|
"""Converts the given key to lowercase."""
|
||||||
|
return key.lower()
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "%s(%s)" % (self.__class__.__name__, self._data)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(self._data)
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self._data)
|
||||||
|
|
||||||
|
def __contains__(self, key):
|
||||||
|
return self._data.__contains__(self._keymangle(key))
|
||||||
|
|
||||||
|
def copy(self, *args, **kwargs):
|
||||||
|
return self._data.copy(*args, **kwargs)
|
||||||
|
|
||||||
|
class CaseInsensitiveDict(collections.abc.MutableMapping, CaseInsensitiveFixedSet):
|
||||||
"""
|
"""
|
||||||
A dictionary storing items case insensitively.
|
A dictionary storing items case insensitively.
|
||||||
"""
|
"""
|
||||||
@ -37,10 +69,6 @@ class CaseInsensitiveDict(collections.abc.MutableMapping):
|
|||||||
else:
|
else:
|
||||||
self._data = {}
|
self._data = {}
|
||||||
|
|
||||||
def _keymangle(self, key):
|
|
||||||
"""Converts the given key to lowercase."""
|
|
||||||
return key.lower()
|
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
key = self._keymangle(key)
|
key = self._keymangle(key)
|
||||||
|
|
||||||
@ -52,24 +80,32 @@ class CaseInsensitiveDict(collections.abc.MutableMapping):
|
|||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
del self._data[self._keymangle(key)]
|
del self._data[self._keymangle(key)]
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
return iter(self._data)
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return len(self._data)
|
|
||||||
|
|
||||||
def copy(self, *args, **kwargs):
|
|
||||||
return self._data.copy(*args, **kwargs)
|
|
||||||
|
|
||||||
def __contains__(self, key):
|
|
||||||
return self._data.__contains__(self._keymangle(key))
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return "%s(%s)" % (self.__class__.__name__, self._data)
|
|
||||||
|
|
||||||
class IRCCaseInsensitiveDict(CaseInsensitiveDict):
|
class IRCCaseInsensitiveDict(CaseInsensitiveDict):
|
||||||
"""
|
"""
|
||||||
A dictionary storing channels case insensitively. Channel objects are initialized on access.
|
A dictionary storing items case insensitively, using IRC case mappings.
|
||||||
|
"""
|
||||||
|
def __init__(self, irc, *, data=None):
|
||||||
|
super().__init__(data=data)
|
||||||
|
self._irc = irc
|
||||||
|
|
||||||
|
def _keymangle(self, key):
|
||||||
|
"""Converts the given key to lowercase."""
|
||||||
|
return self._irc.to_lower(key)
|
||||||
|
|
||||||
|
class CaseInsensitiveSet(collections.abc.MutableSet, CaseInsensitiveFixedSet):
|
||||||
|
"""
|
||||||
|
A mutable set storing items case insensitively.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def add(self, key):
|
||||||
|
self._data.add(self._keymangle(key))
|
||||||
|
|
||||||
|
def discard(self, key):
|
||||||
|
self._data.discard(self._keymangle(key))
|
||||||
|
|
||||||
|
class IRCCaseInsensitiveSet(CaseInsensitiveSet):
|
||||||
|
"""
|
||||||
|
A set storing items case insensitively, using IRC case mappings.
|
||||||
"""
|
"""
|
||||||
def __init__(self, irc, *, data=None):
|
def __init__(self, irc, *, data=None):
|
||||||
super().__init__(data=data)
|
super().__init__(data=data)
|
||||||
|
Loading…
Reference in New Issue
Block a user