Mike Orr avatar Mike Orr committed c1d419f

Create webhelpers.pylonslib; move Flash to it. Rename NoDefault to NotGiven.

Comments (0)

Files changed (2)

webhelpers/containers.py

             return 'defaultdict(%s, %s)' % (self.default_factory,
                                             dict.__repr__(self))
 
-class NoDefault(object):
+class NotGiven(object):
+    """A default value for function args.
+
+    Use this when you need to distinguish between ``None`` and no value.
+
+    >>> def foo(arg=NotGiven):
+    ...     print arg is NotGiven
+    ...
+    >>> foo()
+    True
+    >>> foo(None)
+    False
+    """
     pass
 
 
         self.__dict__.update(kw)
 
 
-class Flash(object):
-    """Accumulate a list of messages to show at the next page request.
-
-    This class is useful when you want to redirect to another page and also
-    show a status message on that page, such as "Changes saved" or 
-    "No previous search found; returning to home page".
-
-    THIS IMPLEMENTATION DEPENDS ON PYLONS.  However, it can easily be adapted
-    for another web framework.
-
-    Normally you instantiate a Flash object in myapp/lib/helpers.py:
-
-        from webhelpers.tools import Flash as _Flash
-        flash = _Flash()
-
-    The helpers module is then imported into your controllers and
-    templates as `h`.  Whenever you want to set a message, do this::
-
-        h.flash("Record deleted.")
-
-    You can set additional messages too::
-
-        h.flash("Hope you didn't need it.")
-
-    Now make a place in your site template for the messages.  In Mako you
-    might do::
-
-        <% messages = h.flash.pop_messages() %>
-        % if messages:
-        <ul id="flash-messages">
-        % for message in messages:
-            <li>${message}</li>
-        % endfor
-        </ul>
-        % endif
-
-    You can style this to look however you want::
-
-        ul#flash-messages {
-            color: red;
-            background-color: #FFFFCC;
-            font-size: larger;
-            font-style: italic;
-            margin-left: 40px;
-            padding: 4px;
-            list-style: none;
-            }
-    """
-    def __init__(self, session_key="flash"):
-        self.session_key = session_key
-
-    def __call__(self, message):
-        from pylons import session
-        session.setdefault(self.session_key, []).append(message)
-        session.save()
-
-    def pop_messages(self):
-        from pylons import session
-        messages = session.pop(self.session_key, [])
-        session.save()
-        return messages
-
-
 class Counter(object):
     """I count the number of occurrences of each value registered with me.
     
             r2[k] = v
     return r1, r2
 
-def ordered_items(dic, key_order, other_keys=True, default=NoDefault):
+def ordered_items(dic, key_order, other_keys=True, default=NotGiven):
     """Like dict.iteritems() but with a specified key order.
 
     ``dic`` is any mapping.
     for key in key_order:
         if key in d:
             yield key, d.pop(key)
-        elif default is not NoDefault:
+        elif default is not NotGiven:
             yield key, default
     if other_keys:
         for key, value in d.iteritems():

webhelpers/pylonslib.py

+"""Helpers for the Pylons web framework
+
+These helpers depend on Pylons' ``request``, ``response``, ``session``
+objects or some other aspect of Pylons.  Most of them can be easily ported to
+another framework by changing the API calls.
+"""
+
+# Do not import Pylons at module level; only within functions.  All WebHelpers
+# modules should be importable on any Python system for the standard
+# regression tests.
+
+class Flash(object):
+    """Accumulate a list of messages to show at the next page request.
+
+    This class is useful when you want to redirect to another page and also
+    show a status message on that page, such as "Changes saved" or 
+    "No previous search found; returning to home page".
+
+    THIS IMPLEMENTATION DEPENDS ON PYLONS.  However, it can easily be adapted
+    for another web framework.
+
+    Normally you instantiate a Flash object in myapp/lib/helpers.py:
+
+        from webhelpers.tools import Flash as _Flash
+        flash = _Flash()
+
+    The helpers module is then imported into your controllers and
+    templates as `h`.  Whenever you want to set a message, do this::
+
+        h.flash("Record deleted.")
+
+    You can set additional messages too::
+
+        h.flash("Hope you didn't need it.")
+
+    Now make a place in your site template for the messages.  In Mako you
+    might do::
+
+        <% messages = h.flash.pop_messages() %>
+        % if messages:
+        <ul id="flash-messages">
+        % for message in messages:
+            <li>${message}</li>
+        % endfor
+        </ul>
+        % endif
+
+    You can style this to look however you want::
+
+        ul#flash-messages {
+            color: red;
+            background-color: #FFFFCC;
+            font-size: larger;
+            font-style: italic;
+            margin-left: 40px;
+            padding: 4px;
+            list-style: none;
+            }
+    """
+    def __init__(self, session_key="flash"):
+        self.session_key = session_key
+
+    def __call__(self, message):
+        from pylons import session
+        session.setdefault(self.session_key, []).append(message)
+        session.save()
+
+    def pop_messages(self):
+        from pylons import session
+        messages = session.pop(self.session_key, [])
+        session.save()
+        return messages
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.