beeseek-devs team mailing list archive
-
beeseek-devs team
-
Mailing list archive
-
Message #00088
[Branch ~beeseek-devs/beeseek/trunk] Rev 188: Add a class for case-insensitive dicts.
------------------------------------------------------------
revno: 188
committer: Andrea Corbellini <andrea.corbellini@xxxxxxxxxxx>
branch nick: trunk
timestamp: Wed 2008-12-31 14:18:38 +0100
message:
Add a class for case-insensitive dicts.
modified:
beeseek/utils/__init__.py
------------------------------------------------------------
revno: 187.1.3
committer: Andrea Corbellini <andrea.corbellini@xxxxxxxxxxx>
branch nick: utils
timestamp: Wed 2008-12-31 14:17:26 +0100
message:
* Fixed bugs in update() and pop()
* Add ``NULL`` object for situations where None can't be used
modified:
beeseek/utils/__init__.py
------------------------------------------------------------
revno: 187.1.2
committer: Andrea Corbellini <andrea.corbellini@xxxxxxxxxxx>
branch nick: utils
timestamp: Wed 2008-12-31 13:43:41 +0100
message:
Add a class for case-insensitive dicts.
modified:
beeseek/utils/__init__.py
------------------------------------------------------------
revno: 187.1.1
committer: Andrea Corbellini <andrea.corbellini@xxxxxxxxxxx>
branch nick: utils
timestamp: Tue 2008-12-30 16:45:40 +0100
message:
MultipleFiles are no longer used.
modified:
beeseek/utils/__init__.py
=== modified file 'beeseek/utils/__init__.py'
--- a/beeseek/utils/__init__.py 2008-11-13 13:42:08 +0000
+++ b/beeseek/utils/__init__.py 2008-12-31 13:17:26 +0000
@@ -15,109 +15,67 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-"""Utilities for Hive."""
-
-class MultipleFiles(object):
- """Perform operations on more than a file at once.
-
- This object, which inherit `file`, allow to perform the same operation on
- more than a file at once. Files must be opened for writing.
-
- >>> fp = MultipleFiles(('log_file.log', sys.stdout), 'w')
- >>> fp.write('Hello, World!\\n')
- Hello, World!
- >>> fp.flush()
- >>> file('log_file.log').read()
- 'Hello, World!\\n'
-
- """
- def __init__(self, files, mode='w', buffering=None):
- """Initialize the object.
-
- Calls `append()` for each file in the list given.
-
- @param files: A list of objects to be given to `append()`.
- @param mode: The I/O mode for the files to be opened.
- @param buffering: The buffer size for the files to be opened.
-
- """
- self._files = []
- self._modes = []
- for fp in files:
- self.append(fp, mode, buffering)
-
- @property
- def closed(self):
- if not self._files:
- return True
- for fp in self._files:
- if fp.closed:
- return True
- return False
-
- def write(self, data):
- """Call the `write()` method for each file."""
- for fp in self._files:
- fp.write(data)
-
- def writelines(self, data):
- """Call the `writelines()` method for each file."""
- self.write(''.join(data))
-
- def flush(self):
- """Call the `flush()` method for each file."""
- for fp in self._files:
- fp.flush()
-
-
- def append(self, fp, mode='w', buffering=None):
- """Add a new file object.
-
- @param fp: It can be either a string containing a path or a file
- object.
- @param mode: The I/O mode for the files to be opened. It is given to
- the builtin function `file()` if `fp` is a string.
- @param buffering: The buffer size to be given to `file()`.
-
- """
- def check_mode(mode):
- # Check if the file is opened for writing.
-
- if len(mode) > 1 and mode[1] == '+':
- mode = mode[:2]
- else:
- mode = mode[0]
- if mode == 'r':
- raise ValueError('All file objects must be opend for '
- 'writing.')
-
- if isinstance(fp, file):
- check_mode(fp.mode)
- self._files.append(fp)
- else:
- check_mode(mode)
- self._files.append(file(fp, mode, buffering))
-
- def remove(self, fp):
- """Remove the first occurrence of the file from the list.
-
- @param fp: It can be both the file object to remove or it's name.
-
- """
- if isinstance(fp, file):
- self._files.remove(fp)
- else:
- name = fp
- for fp in self._files:
- if fp.name == name:
- self._files.remove(fp)
- return
- raise ValueError('fp not in list.')
-
- def pop(self, index=-1):
- """Remove and return the item at the specified index."""
- return self._files.pop(index)
-
- def __iter__(self):
- return iter(self._files)
+"""Common utilities for the BeeSeek code."""
+
+
+class NULL(object):
+ """This is a special class useful when ``None`` can't be used."""
+ pass
+
+
+class CaseInsensitiveDict(dict):
+ """A dict subclass with case-insensitive keys."""
+
+ def __init__(self, seq=None, **kwargs):
+ self.update(seq, **kwargs)
+
+ def update(self, seq=None, **kwargs):
+ for (key, value) in seq or ():
+ self[key] = value
+ for (key, value) in kwargs.iteritems():
+ self[key] = value
+
+
+ def __getitem__(self, key):
+ return dict.__getitem__(self, key.lower())[1]
+
+ def __setitem__(self, key, value):
+ dict.__setitem__(self, key.lower(), (key, value))
+
+ def __delitem__(self, key):
+ dict.__delitem__(self, key.lower())
+
+ def pop(self, key, default=NULL):
+ if default is NULL:
+ return dict.pop(self, key.lower())
+ else:
+ return dict.pop(self, key.lower(), default)
+
+ def popitem(self):
+ return dict.popitem(self)[1]
+
+
+ def __contains__(self, key):
+ return dict.__contains__(self, key.lower())
+
+ def has_key(self, key):
+ return dict.has_key(self, key.lower())
+
+
+ def iteritems(self):
+ return (value for value in dict.itervalues(self))
+
+ def iterkeys(self):
+ return (value[0] for value in dict.itervalues(self))
+
+ def iterkeys(self):
+ return (value[1] for value in dict.itervalues(self))
+
+ def items(self):
+ return list(self.iteritems())
+
+ def keys(self):
+ return list(self.iterkeys())
+
+ def values(self):
+ return list(self.itervalues())
--
BeeSeek mainline
https://code.launchpad.net/~beeseek-devs/beeseek/trunk
You are receiving this branch notification because you are subscribed to it.