Skip to content

Commit

Permalink
Separate source module to separate spackl repo (#28)
Browse files Browse the repository at this point in the history
These changes split out much of the database and file source
functionality to its own repository. This should keep both sections of
code much cleaner and more focused.
  • Loading branch information
alecraso committed Mar 9, 2019
1 parent cac0a35 commit 9e6e897
Show file tree
Hide file tree
Showing 28 changed files with 300 additions and 2,042 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
@@ -1,6 +1,13 @@
CHANGELOG
=========

0.4.0 (2019-03-09)
------------------

- BREAKING - All ``source`` modules and methods have been stripped out
- Functionality has been moved to the ``spackl`` package (``pip install spackl``)
- The ``comparator`` package will expect ``spackl`` to be used for all ``left`` and ``right`` data sources

0.4.0rc3 (2018-12-05)
---------------------

Expand Down
45 changes: 28 additions & 17 deletions README.rst
Expand Up @@ -24,20 +24,21 @@ Overview

.. code:: python
import comparator as cpt
from spackl import db
conf = cpt.DbConfig()
import comparator as cpt
l = cpt.db.PostgresDb(**conf.default)
r = cpt.db.PostgresDb(**conf.other_db)
conf = db.Config()
l = db.Postgres(**conf.default)
r = db.Postgres(**conf.other_db)
query = 'SELECT * FROM my_table ORDER BY 1'
c = cpt.Comparator(l, r, query)
c = cpt.Comparator(l, query, r)
c.run_comparisons()
::

[('first_eq_comp', True)]
[('basic_comp', True)]

Included Comparisons
~~~~~~~~~~~~~~~~~~~~
Expand All @@ -49,7 +50,7 @@ passed using constants.
from comparator.comps import BASIC_COMP, LEN_COMP
c = cpt.Comparator(l, r, query, comps=[BASIC_COMP, LEN_COMP])
c = cpt.Comparator(l, query, r, comps=[BASIC_COMP, LEN_COMP])
c.run_comparisons()
::
Expand All @@ -66,9 +67,9 @@ raise exceptions if that’s your speed.
lq = 'SELECT * FROM my_table ORDER BY 1'
rq = 'SELECT id, uuid, name FROM reporting.my_table ORDER BY 1'
comparisons = [cpt.BASIC_COMP, cpt.LEN_COMP]
comparisons = [BASIC_COMP, LEN_COMP]
c = cpt.Comparator(l, r, left_query=lq, right_query=rq, comps=comparisons)
c = cpt.Comparator(l, lq, r, rq, comps=comparisons)
for result in c.compare():
if not result:
Expand All @@ -78,10 +79,9 @@ Custom Comparisons
~~~~~~~~~~~~~~~~~~

You’ll probably want to define your own comparison checks. You can do so
by defining functions that accept ``left`` and ``right`` args, which, if
coming from one of the included database classes, will be a QueryResult
class representing your query’s output. Perform whatever magic you like,
and return a boolean (or not… your choice).
by defining functions that accept ``left`` and ``right`` args, which correspond
to the results of the queries against your "left" and "right" data source,
respectively. Perform whatever magic you like, and return a boolean (or not… your choice).

.. code:: python
Expand All @@ -92,15 +92,15 @@ and return a boolean (or not… your choice).
def totals_are_equal(left, right):
# Return True if sum(left) == sum(right)
sl = sr = 0
sl, sr = 0, 0
for row in left:
sl += int(row[1])
for row in right:
sr += int(row[1])
return sl == sr
c = cpt.Comparator(l, r, query, comps=[left_is_longer, totals_are_equal])
c = cpt.Comparator(l, query, r, comps=[left_is_longer, totals_are_equal])
c.run_comparisons()
::
Expand All @@ -123,7 +123,7 @@ resulting value of such a comparison is simple.
return len(left) - len(right)
c = cpt.Comparator(l, r, query, comps=len_diff)
c = cpt.Comparator(l, query, r, comps=len_diff)
res = c.run_comparisons()[0]
if res == 0:
print('They match')
Expand All @@ -132,6 +132,10 @@ resulting value of such a comparison is simple.
else:
print('Left is longer by {}'.format(res.result))
It's recommended that you use the ``spackl`` package for instantiating your
"left" and "right" data source objects (``pip install spackl``). This package
was originally part of ``comparator``, and provides the following functionality:

Query results are contained in the ``QueryResult`` class, which provides
simple yet powerful ways to look up and access the output of the query.
Data can be retrieved as a dict, list, json string, or pandas DataFrame.
Expand All @@ -141,7 +145,10 @@ lookup functionality, as well as standard operators (<, >, =, etc).

.. code:: python
pg = cpt.db.PostgresDb(**conf.default)
from spackl import db
conf = db.Config()
pg = db.Postgres(**conf.default)
res = pg.query(query_string)
res # [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}, {'a': 7, 'b': 8, 'c': 9}]
Expand All @@ -163,6 +170,10 @@ For example, accessing the result of a query as a pandas DataFrame
allows for an endless variety of checks/manipulations do be done on a
single query output.

Support is being added to ``spackl`` to allow for querying from files and APIs
using the same methods, allowing for easy comparison between many disparate
data sources. Stay tuned.

.. |Comparator| image:: https://raw.githubusercontent.com/aaronbiller/comparator/master/docs/comparator.jpg
.. |pypi| image:: https://img.shields.io/pypi/v/comparator.svg
:target: https://pypi.org/project/comparator/
Expand Down
14 changes: 3 additions & 11 deletions comparator/__init__.py
@@ -1,12 +1,4 @@
from comparator import db
from comparator.comps import (
BASIC_COMP,
LEN_COMP,
FIRST_COMP,
DEFAULT_COMP)
from comparator.config import DbConfig
from comparator.models import Comparator, ComparatorSet, QueryPair
from .compare import Comparator, ComparatorSet, SourcePair


__all__ = [db, BASIC_COMP, LEN_COMP, FIRST_COMP, DEFAULT_COMP, DbConfig, Comparator, ComparatorSet, QueryPair]
__version__ = '0.4.0rc3'
__all__ = [Comparator, ComparatorSet, SourcePair]
__version__ = '0.4.0'

0 comments on commit 9e6e897

Please sign in to comment.