Chris Mutel avatar Chris Mutel committed d6d9b15

Add some more methods to Query class

Comments (0)

Files changed (1)


 class Result(object):
-    """The result of a query"""
+    """A container that wraps a filtered dataset. Returned by a calling a ``Query`` object. A result object functions like a read-only dictionary; you can call ``Result[some_key]``, or ``some_key in Result``, or ``len(Result)``.
+    The dataset can also be sorted, using ``sort(field)``; the underlying data is then a ``collections.OrderedDict``.
+    Args:
+        *result* (dict): The filtered dataset.
+    """
     def __init__(self, result):
         self.result = result
         return "Query result: (total %i)\n" % len(self.result) \
             + "\n".join(["%s: %s" % (key, data[key]["name"]) for key in data])
+    def sort(self, field, reverse=False):
+        """Sort the filtered dataset. Acts (effectively) in place; does not return anything.
+        Args:
+            *field* (str): The key used for sorting.
+            *reverse* (bool, optional): Reverse normal sorting order.
+        """
+        self.result = collections.OrderedDict(sorted(self.result.iteritems(),
+            key=lambda t: t[1].get(field, None)), reverse=reverse)
+    # Generic dictionary methods
+    def __len__(self):
+        return len(self.result)
+    def __iter__(self):
+        return iter(self.result)
+    def keys(self):
+        return self.result.keys()
+    def iteritems(self):
+        return self.result.iteritems()
     def __getitem__(self, key):
         return self.result[key]
     def __contains__(self, key):
         return key in self.result
-    def sort(self, field):
-        self.result = collections.OrderedDict(sorted(self.result.iteritems(),
-            key=lambda t: t[1].get(field, None)))
-    def __len__(self):
-        return len(self.result)
 class Query(object):
-    """A Database or Method query."""
-    def __init__(self, *queries):
-        self.queries = list(queries)
+    """A container for a set of filters applied to a dataset.
-    def add(self, query):
-        self.queries.append(query)
+    Filters are applied by calling the ``Query`` object, and passing the dataset to filter as the argument. Calling a ``Query`` returns a ``Result`` object with the filtered dataset.
+    Args:
+        *filters* (``Filter``(s)): One or more Filter objects.
+    """
+    def __init__(self, *filters):
+        self.filters = list(filters)
+    def add(self, filter_):
+        """Add another filter.
+        Args:
+            *filter_* (``Filter``): A Filter object.
+        """
+        self.filters.append(filter_)
     def __call__(self, data):
-        for query in self.queries:
-            data = query(data)
+        for filter_ in self.filters:
+            data = filter_(data)
         return Result(data)
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
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.