webhelpers.misc¶
Helpers that are neither text, numeric, container, or date.
Data processing¶
-
webhelpers.misc.all(seq[, pred])¶ Is
pred(elm)true for all elements?With the default predicate, this is the same as Python 2.5’s
all()function; i.e., it returns true if all elements are true.>>> all(["A", "B"]) True >>> all(["A", ""]) False >>> all(["", ""]) False >>> all(["A", "B", "C"], lambda x: x <= "C") True >>> all(["A", "B", "C"], lambda x: x < "C") False
From recipe in itertools docs.
-
webhelpers.misc.any(seq[, pred])¶ Is
pred(elm)is true for any element?With the default predicate, this is the same as Python 2.5’s
any()function; i.e., it returns true if any element is true.>>> any(["A", "B"]) True >>> any(["A", ""]) True >>> any(["", ""]) False >>> any(["A", "B", "C"], lambda x: x <= "C") True >>> any(["A", "B", "C"], lambda x: x < "C") True
From recipe in itertools docs.
-
webhelpers.misc.no(seq[, pred])¶ Is
pred(elm)false for all elements?With the default predicate, this returns true if all elements are false.
>>> no(["A", "B"]) False >>> no(["A", ""]) False >>> no(["", ""]) True >>> no(["A", "B", "C"], lambda x: x <= "C") False >>> no(["X", "Y", "Z"], lambda x: x <="C") True
From recipe in itertools docs.
-
webhelpers.misc.count_true(seq[, pred])¶ How many elements is
pred(elm)true for?With the default predicate, this counts the number of true elements.
>>> count_true([1, 2, 0, "A", ""]) 3 >>> count_true([1, "A", 2], lambda x: isinstance(x, int)) 2
This is equivalent to the
itertools.quantifyrecipe, which I couldn’t get to work.
-
webhelpers.misc.convert_or_none(value, type_)¶ Return the value converted to the type, or None if error.
type_may be a Python type or any function taking one argument.>>> print convert_or_none("5", int) 5 >>> print convert_or_none("A", int) None
-
webhelpers.misc.flatten(iterable)¶ Recursively iterate lists and tuples.
Examples:
>>> list(flatten([1, [2, 3], 4])) [1, 2, 3, 4] >>> list(flatten([1, (2, 3, [4]), 5])) [1, 2, 3, 4, 5]
Exceptions and deprecation¶
-
webhelpers.misc.deprecate(message, pending=False, stacklevel=2)¶ Issue a deprecation warning.
message: the deprecation message.pending: if true, usePendingDeprecationWarning. If false (default), useDeprecationWarning. Python displays deprecations and ignores pending deprecations by default.stacklevel: passed towarnings.warn. The default level 2 makes the traceback end at the caller’s level. Higher numbers make it end at higher levels.
-
webhelpers.misc.format_exception(exc=None)¶ Format the exception type and value for display, without the traceback.
This is the function you always wished were in the
tracebackmodule but isn’t. It’s different fromtraceback.format_exception, which includes the traceback, returns a list of lines, and has a trailing newline.If you don’t provide an exception object as an argument, it will call
sys.exc_info()to get the current exception.
-
class
webhelpers.misc.DeclarativeException(message=None)¶ A simpler way to define an exception with a fixed message.
Subclasses have a class attribute
.message, which is used if no message is passed to the constructor. The default message is the empty string.Example:
>>> class MyException(DeclarativeException): ... message="can't frob the bar when foo is enabled" ... >>> try: ... raise MyException() ... except Exception, e: ... print e ... can't frob the bar when foo is enabled
-
message= ''¶
-
-
class
webhelpers.misc.OverwriteError(filename, message="not overwriting '%s'")¶ Refusing to overwrite an existing file or directory.