A case insensitive lists that has some caseless methods. Only allows strings as list members. Most methods that would normally return a list, return a caselessList. (Except list() and lowercopy()) Sequence Methods implemented are : __contains__, remove, count, index, append, extend, inse
| 19 | # E-mail fuzzyman AT atlantibots DOT org DOT uk (or michael AT foord DOT me DO |
| 20 | |
| 21 | class caselessList(list): |
| 22 | """A case insensitive lists that has some caseless methods. Only allows strings as list members. |
| 23 | Most methods that would normally return a list, return a caselessList. (Except list() and lowercopy()) |
| 24 | Sequence Methods implemented are : |
| 25 | __contains__, remove, count, index, append, extend, insert, |
| 26 | __getitem__, __setitem__, __getslice__, __setslice__ |
| 27 | __add__, __radd__, __iadd__, __mul__, __rmul__ |
| 28 | Plus Extra methods: |
| 29 | findentry, copy , lowercopy, list |
| 30 | Inherited methods : |
| 31 | __imul__, __len__, __iter__, pop, reverse, sort |
| 32 | """ |
| 33 | def __init__(self, inlist=[]): |
| 34 | list.__init__(self) |
| 35 | for entry in inlist: |
| 36 | if not isinstance(entry, str): raise TypeError('Members of this object must be strings. You supplied \"%s\" which is \"%s\"' % (entry, type(entry))) |
| 37 | self.append(entry) |
| 38 | |
| 39 | def findentry(self, item): |
| 40 | """A caseless way of checking if an item is in the list or not. |
| 41 | It returns None or the entry.""" |
| 42 | if not isinstance(item, str): raise TypeError('Members of this object must be strings. You supplied \"%s\"' % type(item)) |
| 43 | for entry in self: |
| 44 | if item.lower() == entry.lower(): return entry |
| 45 | return None |
| 46 | |
| 47 | def __contains__(self, item): |
| 48 | """A caseless way of checking if a list has a member in it or not.""" |
| 49 | for entry in self: |
| 50 | if item.lower() == entry.lower(): return True |
| 51 | return False |
| 52 | |
| 53 | def remove(self, item): |
| 54 | """Remove the first occurence of an item, the caseless way.""" |
| 55 | for entry in self: |
| 56 | if item.lower() == entry.lower(): |
| 57 | list.remove(self, entry) |
| 58 | return |
| 59 | raise ValueError(': list.remove(x): x not in list') |
| 60 | |
| 61 | def copy(self): |
| 62 | """Return a caselessList copy of self.""" |
| 63 | return caselessList(self) |
| 64 | |
| 65 | def list(self): |
| 66 | """Return a normal list version of self.""" |
| 67 | return list(self) |
| 68 | |
| 69 | def lowercopy(self): |
| 70 | """Return a lowercase (list) copy of self.""" |
| 71 | return [entry.lower() for entry in self] |
| 72 | |
| 73 | def append(self, item): |
| 74 | """Adds an item to the list and checks it's a string.""" |
| 75 | if not isinstance(item, str): raise TypeError('Members of this object must be strings. You supplied \"%s\"' % type(item)) |
| 76 | list.append(self, item) |
| 77 | |
| 78 | def extend(self, item): |
no outgoing calls
no test coverage detected