1. Atsushi Odagiri
  2. WebHelpers


slug...@gmail.com  committed 2bd660d

New helper ``deprecate()``.

  • Participants
  • Parent commits a697852
  • Branches trunk

Comments (0)

Files changed (3)

File docs/modules/misc.rst

View file
  • Ignore whitespace
 .. autofunction:: no(seq[, pred])
 .. autofunction:: count_true(seq[, pred])
 .. autofunction:: convert_or_none
+.. autofunction:: deprecate
 .. autoclass:: DeclarativeException

File docs/whats_new.rst

View file
  • Ignore whitespace
 New helpers to flatten nested lists and tuples, and to
 gather all the subclasses of a specified class. There's an exception
-``OverwriteError``, and a ``DeclarativeException`` class for making your own
-exceptions with constant messages.
+``OverwriteError``, a ``DeclarativeException`` class for making your own
+exceptions with constant messages, and a ``deprecate`` function.

File webhelpers/misc.py

View file
  • Ignore whitespace
 import itertools
 import types
+import warnings
 def all(seq, pred=None):
     """Is ``pred(elm)`` true for all elements?
         Exception.__init__(self, message)
         self.filename = filename
+def deprecate(message, pending=False, stacklevel=2):
+    """Issue a deprecation warning.
+    ``message``: the deprecation message.
+    ``pending``: if true, use ``PendingDeprecationWarning``. If false (default), 
+    use ``DeprecationWarning``. Python displays deprecations and ignores
+    pending deprecations by default.
+    ``stacklevel``: passed to ``warnings.warn``. The default level 2 makes the
+    traceback end at the caller's level. Higher numbers make it end at higher
+    levels.
+    """
+    category = pending and PendingDeprecationWarning or DeprecationWarning
+    warnings.warn(message, category, stacklevel)
 if __name__ == "__main__":
     import doctest