web.web.utils

Package web :: Package web :: Module utils |
[hide private]
[frames] | no frames]

# Module utils

source code

General Utilities (part of web.py)

Classes | [hide private]
| Storage A Storage object is like a dictionary except `obj.foo` can be used in addition to `obj['foo']`.
| storage A Storage object is like a dictionary except `obj.foo` can be used in addition to `obj['foo']`.
| Counter Keeps count of how many times something is added.
| counter Keeps count of how many times something is added.
| _hack
| TimeoutError
| Memoize 'Memoizes' a function, caching its return values for each input.
| memoize 'Memoizes' a function, caching its return values for each input.
| _re_subm_proxy
| IterBetter Returns an object that can be used as an iterator but can also be used via __getitem__ (although it cannot go backwards -- that is, you cannot request `iterbetter[0]` after requesting `iterbetter[1]`).
| iterbetter Returns an object that can be used as an iterator but can also be used via __getitem__ (although it cannot go backwards -- that is, you cannot request `iterbetter[0]` after requesting `iterbetter[1]`).
| CaptureStdout Captures everything `func` prints to stdout and returns it instead.
| capturestdout Captures everything `func` prints to stdout and returns it instead.
| Profile Profiles `func` and returns a tuple containing its output and a string with human-readable profiling information.
| profile Profiles `func` and returns a tuple containing its output and a string with human-readable profiling information.
| ThreadedDict Thread local storage.
| threadeddict Thread local storage.
| _EmailMessage

Functions | [hide private]
storify(mapping, *requireds, **defaults) Creates a `storage` object from dictionary `mapping`, raising `KeyError` if d doesn't have all of the keys in `requireds` and using the default values for keys found in `defaults`. | source code
_strips(direction, text, remove) | source code
rstrips(text, remove) removes the string `remove` from the right of `text` | source code
lstrips(text, remove) removes the string `remove` from the left of `text` | source code
strips(text, remove) removes the string `remove` from the both sides of `text` | source code
safeunicode(obj, encoding=`'``utf-8``'`) Converts any given object to unicode string. | source code
safestr(obj, encoding=`'``utf-8``'`) Converts any given object to utf-8 encoded string. | source code
utf8(obj, encoding=`'``utf-8``'`) Converts any given object to utf-8 encoded string. | source code
timelimit(timeout) A decorator to limit a function to `timeout` seconds, raising `TimeoutError` if it takes longer. | source code
re_subm(pat, repl, string) Like re.sub, but returns the replacement _and_ the match object. | source code
group(seq, size) Returns an iterator over a series of lists of length size from iterable. | source code
uniq(seq) Removes duplicate elements from a list. | source code
iterview(x) Takes an iterable `x` and returns an iterator over it which prints its progress to stderr as it iterates through. | source code
safeiter(it, cleanup=None, ignore_errors=True) Makes an iterator safe by ignoring the exceptions occured during the iteration. | source code
safewrite(filename, content) Writes the content to a temp file and then moves the temp file to given filename to avoid overwriting the existing file in case of errors. | source code
dictreverse(mapping) Returns a new dictionary with keys and values swapped. | source code
dictfind(dictionary, element) Returns a key whose value in `dictionary` is `element` or, if none exists, None. | source code
dictfindall(dictionary, element) Returns the keys whose values in `dictionary` are `element` or, if none exists, []. | source code
dictincr(dictionary, element) Increments `element` in `dictionary`, setting it to one if it doesn't exist. | source code
dictadd(*dicts) Returns a dictionary consisting of the keys in the argument dictionaries. | source code
requeue(queue, index=-1) Returns the element at index after moving it to the beginning of the queue. | source code
restack(stack, index=0) Returns the element at index after moving it to the top of stack. | source code
listget(lst, ind, default=None) Returns `lst[ind]` if it exists, `default` otherwise. | source code
intget(integer, default=None) Returns `integer` as an int or `default` if it can't. | source code
datestr(then, now=None) Converts a (UTC) datetime object to a nice string representation. | source code
numify(string) Removes all non-digit characters from `string`. | source code
denumify(string, pattern) Formats `string` according to `pattern`, where the letter X gets replaced by characters from `string`. | source code
commify(n) Add commas to an integer `n`. | source code
dateify(datestring) Formats a numified `datestring` properly. | source code
nthstr(n) Formats an ordinal. | source code
cond(predicate, consequence, alternative=None) Function replacement for if-else to use in expressions. | source code
format_exc(limit=None) | source code
tryall(context, prefix=None) Tries a series of functions and prints their results. | source code
autoassign(self, locals) Automatically assigns local variables to `self`. | source code
to36(q) Converts an integer to base 36 (a useful scheme for human-sayable IDs). | source code
safemarkdown(text) Converts text to HTML following the rules of Markdown, but blocking any outside HTML input, so that only the things supported by Markdown can be used. | source code
sendmail(from_address, to_address, subject, message, headers=None, **kw) Sends the email message `message` with mail and envelope headers for from `from_address_` to `to_address` with `subject`. | source code

Variables | [hide private]
| iters = `(<type 'list'>, <type 'tuple'>, <type 'set'>, <type 's`...``
| re_compile = `memoize(re.compile)`
| r_url = `re.compile(r'`(?<!`\(`)``(`http://`(`\S`+``)``)`')`
| __package__ = ``'``web.web``'``

Function Details | [hide private]

### storify(mapping, *requireds, **defaults) | source code
Creates a `storage` object from dictionary `mapping`, raising `KeyError` if d doesn't have all of the keys in `requireds` and using the default values for keys found in `defaults`. For example, `storify({'a':1, 'c':3}, b=2, c=0)` will return the equivalent of `storage({'a':1, 'b':2, 'c':3})`. If a `storify` value is a list (e.g. multiple values in a form submission), `storify` returns the last element of the list, unless the key appears in `defaults` as a list. Thus:
    >>> storify({'a':[1, 2]}).a
    2
    >>> storify({'a':[1, 2]}, a=[]).a
    [1, 2]
    >>> storify({'a':1}, a=[]).a
    [1]
    >>> storify({}, a=[]).a
    []
Similarly, if the value has a `value` attribute, `storify will return _its_ value, unless the key appears in `defaults` as a dictionary.
    >>> storify({'a':storage(value=1)}).a
    1
    >>> storify({'a':storage(value=1)}, a={}).a
    <Storage {'value': 1}>
    >>> storify({}, a={}).a
    {}
Optionally, keyword parameter `_unicode` can be passed to convert all values to unicode.
    >>> storify({'x': 'a'}, _unicode=True)
    <Storage {'x': u'a'}>
    >>> storify({'x': storage(value='a')}, x={}, _unicode=True)
    <Storage {'x': <Storage {'value': 'a'}>}>
    >>> storify({'x': storage(value='a')}, _unicode=True)
    <Storage {'x': u'a'}>

### rstrips(text, remove) | source code
removes the string `remove` from the right of `text`
    >>> rstrips("foobar", "bar")
    'foo'

### lstrips(text, remove) | source code
removes the string `remove` from the left of `text`
    >>> lstrips("foobar", "foo")
    'bar'

### strips(text, remove) | source code
removes the string `remove` from the both sides of `text`
    >>> strips("foobarfoo", "foo")
    'bar'

### safeunicode(obj, encoding=`'``utf-8``'`) | source code
Converts any given object to unicode string.
    >>> safeunicode('hello')
    u'hello'
    >>> safeunicode(2)
    u'2'
    >>> safeunicode('\xe1\x88\xb4')
    u'\u1234'

### safestr(obj, encoding=`'``utf-8``'`) | source code
Converts any given object to utf-8 encoded string.
    >>> safestr('hello')
    'hello'
    >>> safestr(u'\u1234')
    '\xe1\x88\xb4'
    >>> safestr(2)
    '2'

### utf8(obj, encoding=`'``utf-8``'`) | source code
Converts any given object to utf-8 encoded string.
    >>> safestr('hello')
    'hello'
    >>> safestr(u'\u1234')
    '\xe1\x88\xb4'
    >>> safestr(2)
    '2'

### timelimit(timeout) | source code
A decorator to limit a function to `timeout` seconds, raising `TimeoutError` if it takes longer.
    >>> import time
    >>> def meaningoflife():
    ...     time.sleep(.2)
    ...     return 42
    >>>
    >>> timelimit(.1)(meaningoflife)()
    Traceback (most recent call last):
        ...
    TimeoutError: took too long
    >>> timelimit(1)(meaningoflife)()
    42
_Caveat:_ The function isn't stopped after `timeout` seconds but continues executing in a separate thread. (There seems to be no way to kill a thread.) inspired by <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473878>

### re_subm(pat, repl, string) | source code
Like re.sub, but returns the replacement _and_ the match object.
    >>> t, m = re_subm('g(oo+)fball', r'f\1lish', 'goooooofball')
    >>> t
    'foooooolish'
    >>> m.groups()
    ('oooooo',)

### group(seq, size) | source code
Returns an iterator over a series of lists of length size from iterable.
    >>> list(group([1,2,3,4], 2))
    [[1, 2], [3, 4]]
    >>> list(group([1,2,3,4,5], 2))
    [[1, 2], [3, 4], [5]]

### uniq(seq) | source code
Removes duplicate elements from a list.
    >>> uniq([1,2,3,1,4,5,6])
    [1, 2, 3, 4, 5, 6]

### dictreverse(mapping) | source code
Returns a new dictionary with keys and values swapped.
    >>> dictreverse({1: 2, 3: 4})
    {2: 1, 4: 3}

### dictfind(dictionary, element) | source code
Returns a key whose value in `dictionary` is `element` or, if none exists, None.
    >>> d = {1:2, 3:4}
    >>> dictfind(d, 4)
    3
    >>> dictfind(d, 5)

### dictfindall(dictionary, element) | source code
Returns the keys whose values in `dictionary` are `element` or, if none exists, [].
    >>> d = {1:4, 3:4}
    >>> dictfindall(d, 4)
    [1, 3]
    >>> dictfindall(d, 5)
    []

### dictincr(dictionary, element) | source code
Increments `element` in `dictionary`, setting it to one if it doesn't exist.
    >>> d = {1:2, 3:4}
    >>> dictincr(d, 1)
    3
    >>> d[1]
    3
    >>> dictincr(d, 5)
    1
    >>> d[5]
    1

### dictadd(*dicts) | source code
Returns a dictionary consisting of the keys in the argument dictionaries. If they share a key, the value from the last argument is used.
    >>> dictadd({1: 0, 2: 0}, {2: 1, 3: 1})
    {1: 0, 2: 1, 3: 1}

### requeue(queue, index=-1) | source code
Returns the element at index after moving it to the beginning of the queue.
    >>> x = [1, 2, 3, 4]
    >>> requeue(x)
    4
    >>> x
    [4, 1, 2, 3]

### restack(stack, index=0) | source code
Returns the element at index after moving it to the top of stack.
    >>> x = [1, 2, 3, 4]
    >>> restack(x)
    1
    >>> x
    [2, 3, 4, 1]

### listget(lst, ind, default=None) | source code
Returns `lst[ind]` if it exists, `default` otherwise.
    >>> listget(['a'], 0)
    'a'
    >>> listget(['a'], 1)
    >>> listget(['a'], 1, 'b')
    'b'

### intget(integer, default=None) | source code
Returns `integer` as an int or `default` if it can't.
    >>> intget('3')
    3
    >>> intget('3a')
    >>> intget('3a', 0)
    0

### datestr(then, now=None) | source code
Converts a (UTC) datetime object to a nice string representation.
    >>> from datetime import datetime, timedelta
    >>> d = datetime(1970, 5, 1)
    >>> datestr(d, now=d)
    '0 microseconds ago'
    >>> for t, v in {
    ...   timedelta(microseconds=1): '1 microsecond ago',
    ...   timedelta(microseconds=2): '2 microseconds ago',
    ...   -timedelta(microseconds=1): '1 microsecond from now',
    ...   -timedelta(microseconds=2): '2 microseconds from now',
    ...   timedelta(microseconds=2000): '2 milliseconds ago',
    ...   timedelta(seconds=2): '2 seconds ago',
    ...   timedelta(seconds=2*60): '2 minutes ago',
    ...   timedelta(seconds=2*60*60): '2 hours ago',
    ...   timedelta(days=2): '2 days ago',
    ... }.iteritems():
    ...     assert datestr(d, now=d+t) == v
    >>> datestr(datetime(1970, 1, 1), now=d)
    'January  1'
    >>> datestr(datetime(1969, 1, 1), now=d)
    'January  1, 1969'
    >>> datestr(datetime(1970, 6, 1), now=d)
    'June  1, 1970'
    >>> datestr(None)
    ''

### numify(string) | source code
Removes all non-digit characters from `string`.
    >>> numify('800-555-1212')
    '8005551212'
    >>> numify('800.555.1212')
    '8005551212'

### denumify(string, pattern) | source code
Formats `string` according to `pattern`, where the letter X gets replaced by characters from `string`.
    >>> denumify("8005551212", "(XXX) XXX-XXXX")
    '(800) 555-1212'

### commify(n) | source code
Add commas to an integer `n`.
    >>> commify(1)
    '1'
    >>> commify(123)
    '123'
    >>> commify(1234)
    '1,234'
    >>> commify(1234567890)
    '1,234,567,890'
    >>> commify(123.0)
    '123.0'
    >>> commify(1234.5)
    '1,234.5'
    >>> commify(1234.56789)
    '1,234.56789'
    >>> commify('%.2f' % 1234.5)
    '1,234.50'
    >>> commify(None)
    >>>

### nthstr(n) | source code
Formats an ordinal. Doesn't handle negative numbers.
    >>> nthstr(1)
    '1st'
    >>> nthstr(0)
    '0th'
    >>> [nthstr(x) for x in [2, 3, 4, 5, 10, 11, 12, 13, 14, 15]]
    ['2nd', '3rd', '4th', '5th', '10th', '11th', '12th', '13th', '14th', '15th']
    >>> [nthstr(x) for x in [91, 92, 93, 94, 99, 100, 101, 102]]
    ['91st', '92nd', '93rd', '94th', '99th', '100th', '101st', '102nd']
    >>> [nthstr(x) for x in [111, 112, 113, 114, 115]]
    ['111th', '112th', '113th', '114th', '115th']

### cond(predicate, consequence, alternative=None) | source code
Function replacement for if-else to use in expressions.
    >>> x = 2
    >>> cond(x % 2 == 0, "even", "odd")
    'even'
    >>> cond(x % 2 == 0, "even", "odd") + '_row'
    'even_row'

### tryall(context, prefix=None) | source code
    Tries a series of functions and prints their results.
    `context` is a dictionary mapping names to values;
    the value will only be tried if it's callable.

        >>> tryall(dict(j=lambda: True))
        j: True
        ----------------------------------------
        results:
           True: 1

    For example, you might have a file `test/stuff.py`
    with a series of functions testing various things in it.
    At the bottom, have a line:

        if __name__ == "__main__": tryall(globals())

    Then you can run `python test/stuff.py` and get the results of
    all the tests.

### autoassign(self, locals) | source code
    Automatically assigns local variables to `self`.

        >>> self = storage()
        >>> autoassign(self, dict(a=1, b=2))
        >>> self
        <Storage {'a': 1, 'b': 2}>

    Generally used in `__init__` methods, as in:

        def __init__(self, foo, bar, baz=1): autoassign(self, locals())

### to36(q) | source code
Converts an integer to base 36 (a useful scheme for human-sayable IDs).
    >>> to36(35)
    'z'
    >>> to36(119292)
    '2k1o'
    >>> int(to36(939387374), 36)
    939387374
    >>> to36(0)
    '0'
    >>> to36(-393)
    Traceback (most recent call last):
        ...
    ValueError: must supply a positive integer

### safemarkdown(text) | source code
Converts text to HTML following the rules of Markdown, but blocking any outside HTML input, so that only the things supported by Markdown can be used. Also converts raw URLs to links. (requires [markdown.py](http://webpy.org/markdown.py))

### sendmail(from_address, to_address, subject, message, headers=None, **kw) | source code
Sends the email message `message` with mail and envelope headers for from `from_address_` to `to_address` with `subject`. Additional email headers can be specified with the dictionary `headers. Optionally cc, bcc and attachments can be specified as keyword arguments. Attachments must be an iterable and each attachment can be either a filename or a file object or a dictionary with filename, content and optionally content_type keys. If `web.config.smtp_server` is set, it will send the message to that SMTP server. Otherwise it will look for `/usr/sbin/sendmail`, the typical location for the sendmail-style binary. To use sendmail from a different path, set `web.config.sendmail_path`.

Variables Details | [hide private]

  ### iters Value:
      (<type 'list'>, <type 'tuple'>, <type 'set'>, <type 'set'>)