General utility functions used within pesto.
These functions are reserved for internal usage and it is not recommended that users of the API call these functions directly
Wrap a PutbackInput to read as far as a delimiter (after which subsequent reads will return empty strings, as if EOF was reached)
Examples:
>>> from StringIO import StringIO
>>> s = StringIO('one--two--three')
>>> s.seek(0)
>>> p = PutbackInput(s)
>>> DelimitedInput(p, '--').read()
'one'
>>> DelimitedInput(p, '--').read()
'two'
>>> DelimitedInput(p, '--').read()
'three'
>>> DelimitedInput(p, '--').read()
''
Initialize an instance of DelimitedInput.
Return up to size bytes of data from the stream until EOF or delimiter is reached.
Read a single line of up to size bytes from the input stream, or up to delimiter if this is encountered before a complete line is read.
Write-only output object.
Will store data in a StringIO, until more than bufsize bytes are written, at which point it will switch to storing data in a real file object.
Initialize an ExpandableOutput instance.
Switch the storage backend to an instance of TemporaryFile.
write, optimized for the TemporaryFile backend
write, optimized for the StringIO backend.
Dictionary-like object that supports multiple values per key. Insertion order is preserved.
Synopsis:
>>> d = MultiDict([('a', 1), ('a', 2), ('b', 3)])
>>> d['a']
1
>>> d['b']
3
>>> d.getlist('a')
[1, 2]
>>> d.getlist('b')
[3]
>>> list(d.iterallitems())
[('a', 1), ('a', 2), ('b', 3)]
MultiDicts can be constructed in the following ways:
From a sequence of (key, value) pairs:
>>> MultiDict([('a', 1), ('a', 2)]) MultiDict([('a', 1), ('a', 2)])Initialized from another MultiDict:
>>> d = MultiDict([('a', 1), ('a', 2)]) >>> MultiDict(d) MultiDict([('a', 1), ('a', 2)])Initialized from a regular dict:
>>> MultiDict({'a': 1}) MultiDict([('a', 1)])From keyword arguments:
>>> MultiDict(a=1) MultiDict([('a', 1)])
Return a list of (key, value) pairs for each item in the MultiDict. Items with multiple keys will have multiple key-value pairs returned:
>>> d = MultiDict([('a', 1), ('a', 2), ('b', 3)])
>>> d.allitems()
[('a', 1), ('a', 2), ('b', 3)]
Return a shallow copy of the dictionary:
>>> d = MultiDict([('a', 1), ('a', 2), ('b', 3)])
>>> copy = d.copy()
>>> copy
MultiDict([('a', 1), ('a', 2), ('b', 3)])
>>> copy is d
False
Extend the MultiDict with another MultiDict, regular dictionary or a iterable of (key, value) pairs. This is similar to update() except that new keys are added to old keys.
Create a new MultiDict with keys from seq and values set to value.
Example:
>>> MultiDict.fromkeys(['a', 'b'])
MultiDict([('a', None), ('b', None)])
Keys can be repeated:
>>> d = MultiDict.fromkeys(['a', 'b', 'a'])
>>> d.getlist('a')
[None, None]
>>> d.getlist('b')
[None]
Return the first available value for key key, or default if no such value exists:
>>> d = MultiDict([('a', 1), ('a', 2)])
>>> d.get('a')
1
>>> print d.get('b')
None
Return a list of all values for key key.
Return a list of (key, value) tuples. Only the first (key, value) is returned where keys have multiple values:
>>> d = MultiDict([('a', 1), ('a', 2), ('b', 3)])
>>> d.items()
[('a', 1), ('b', 3)]
Like allitems, but return an iterator rather than a list.
Like items, but return an iterator rather than a list.
Iterator for dictionary keys. Each key is returned only once, even if multiple values are present.
>>> d = MultiDict([('a', 1), ('a', 2), ('b', 3)])
>>> d.keys()
['a', 'b']
Like listitems, but return an iterator rather than a list.
Like listvalues, but returns an iterator over the results instead of a list.
Like values, but return an iterator rather than a list.
Return dictionary keys. Each key is returned only once, even if multiple values are present.
>>> d = MultiDict([('a', 1), ('a', 2), ('b', 3)])
>>> d.keys()
['a', 'b']
Like items, but returns lists of values:
>>> d = MultiDict([('a', 1), ('a', 2), ('b', 3)])
>>> d.listitems()
[('a', [1, 2]), ('b', [3])]
Return a list of values. Each item returned is a list of values associated with a single key.
Example usage:
>>> d = MultiDict([('a', 1), ('a', 2), ('b', 3)])
>>> d.listvalues()
[[1, 2], [3]]
Dictionary pop method. Return and remove the value associated with key. If more than one value is associated with key, only the first is returned.
Example usage:
>>> d = MultiDict([('a', 1), ('a', 2), ('b', 3)])
>>> d.pop('a')
1
>>> d
MultiDict([('a', 2), ('b', 3)])
>>> d.pop('a')
2
>>> d
MultiDict([('b', 3)])
>>> d.pop('a')
Traceback (most recent call last):
...
KeyError: 'a'
Return and remove a (key, value) pair from the dictionary.
The item popped is always the most recently added key and the first value associated with it:
>>> d = MultiDict([('a', 1), ('a', 2), ('b', 3)])
>>> d.popitem()
('b', 3)
>>> d.popitem()
('a', 1)
>>> d.popitem()
('a', 2)
>>> d.popitem()
Traceback (most recent call last):
...
KeyError: 'popitem(): dictionary is empty'
Update the MultiDict from another MultiDict, regular dictionary or a iterable of (key, value) pairs. New keys overwrite old keys - use extend() if you want new keys to be added to old keys.
Updating from another MultiDict:
>>> d = MultiDict([('name', 'eric'), ('occupation', 'lumberjack')])
>>> d.update(MultiDict([('mood', 'okay')]))
>>> d
MultiDict([('name', 'eric'), ('occupation', 'lumberjack'), ('mood', 'okay')])
from a dictionary:
>>> d = MultiDict([('name', 'eric'), ('hobby', 'shopping')])
>>> d.update({'hobby': 'pressing wild flowers'})
>>> d
MultiDict([('name', 'eric'), ('hobby', 'pressing wild flowers')])
an iterable of (key, value) pairs:
>>> d = MultiDict([('name', 'eric'), ('occupation', 'lumberjack')])
>>> d.update([('hobby', 'shopping'), ('hobby', 'pressing wild flowers')])
>>> d
MultiDict([('name', 'eric'), ('occupation', 'lumberjack'), ('hobby', 'shopping'), ('hobby', 'pressing wild flowers')])
or keyword arguments:
>>> d = MultiDict([('name', 'eric'), ('occupation', 'lumberjack')])
>>> d.update(mood='okay')
Return values from the dictionary. Where keys have multiple values, only the first is returned:
>>> d = MultiDict([('a', 1), ('a', 2), ('b', 3)])
>>> d.values()
[1, 3]
Wrap a file-like object to allow data read to be returned to the buffer.
Only supports serial read-access, ie no seek or write methods.
Example:
>>> from StringIO import StringIO
>>> s = StringIO("the rain in spain\nfalls mainly\non the plain\n")
>>> p = PutbackInput(s)
>>> line = p.readline()
>>> line
'the rain in spain\n'
>>> p.putback(line)
>>> p.readline()
'the rain in spain\n'
Initialize a PutbackInput object from input stream io.
Peek ahead size bytes from the stream without consuming any data
Put data back into the stream
Return up to size bytes from the input stream.
Read a single line of up to size bytes from the input stream.
Mixin that defines readlines and the iterator interface in terms of underlying readline method.
Wrap an IO object to prevent reading beyond length bytes.
Example:
>>> from StringIO import StringIO
>>> s = StringIO("the rain in spain\nfalls mainly\non the plain\n")
>>> s = SizeLimitedInput(s, 24)
>>> len(s.read())
24
>>> s.seek(0)
>>> s.read()
'the rain in spain\nfalls '
>>> s.seek(0)
>>> s.readline()
'the rain in spain\n'
>>> s.readline()
'falls '
Return the minimum of requested and the number of bytes available in the stream. If requested is negative, return the number of bytes available in the stream.
Return up to size bytes from the input stream.
Read a single line of up to size bytes from the input stream.
Seek to position pos. This is a wrapper for the underlying IO’s seek method.
Return the position of the file pointer in the stream.