Skip to content

Commit

Permalink
Add docstrings for composite objects--although these
Browse files Browse the repository at this point in the history
classes are going to be replaced by a new ProxyGroup
almost immediately.
  • Loading branch information
shawnbrown committed Aug 2, 2018
1 parent 4601e93 commit 65ae5b8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
39 changes: 35 additions & 4 deletions datatest/_query/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1489,8 +1489,8 @@ def __init__(self, *args, **kwds):


class CompositeQuery(Sequence):
"""A class to wrap multiple Query instances so they can be
worked with like a single Query.
"""A class to wrap multiple :class:`Query` instances so they can
be worked with like a single Query.
"""
def __init__(self, *queries):
for argnum, query in enumerate(queries, 1):
Expand Down Expand Up @@ -1572,8 +1572,39 @@ def fetch(self):


class CompositeSelector(Sequence):
"""A class to wrap multiple Selector instances so they can be
worked with like a single Selector.
"""A class to wrap multiple :class:`Selector` instances so they
can be worked with like a single Selector. The CompositeSelector
can be treated as a sequence that can be unpacked into individual
objects for validation:
.. code-block:: python
:emphasize-lines: 8
...
compare = CompositeSelector(
Selector('detailed_file.csv'),
Selector('reference_file.csv'),
)
validate(*compare({'state'}))
In the example above, we selected the set of values in the "state"
column from each file (using ``{'state'}``) and passed the two
return values to :func:`validate` using *argument unpacking* (with
``*``).
This is a shorthand for:
.. code-block:: python
:emphasize-lines: 6
...
detailed = Select('detailed_file.csv')
reference = Select('reference_file.csv')
validate(detailed({'state'}), reference({'state'}))
"""
def __init__(self, *selectors):
for argnum, select in enumerate(selectors, 1):
Expand Down
26 changes: 26 additions & 0 deletions docs/reference/data-handling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,29 @@ Selecting & Querying Data

The underlying iterator---useful when introspecting
or rewrapping.


*****************
Composite Objects
*****************

.. autoclass:: CompositeSelector

.. autoclass:: CompositeQuery


Working With Composite Objects
==============================

Composite objects are useful when you need to compare data from
multiple sources that are similarly shaped (i.e., they have many
of the same column names). Rather than select the same columns
and build the same queries for each data source, you can wrap
them in a composite object and build the query once::

select1 = Selector('detailed_data.csv')
select2 = Selector('summary_reference.csv')
compare = CompositeSelector(select1, select2)

comparison = compare({'county': 'population'}).sum()

0 comments on commit 65ae5b8

Please sign in to comment.