Skip to content

Commit

Permalink
Merge pull request #51 from open-contracting/list-all-collections
Browse files Browse the repository at this point in the history
list_collections: return a list of all collections if no source_id is…
  • Loading branch information
jpmckinney committed Sep 15, 2021
2 parents 32d7a9b + eea0562 commit 40bf6cc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
8 changes: 8 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

0.3.6 (2021-09-15)
------------------

Changed
-------

- :meth:`~ocdskingfishercolab.list_collections`: `source_id` is now an optional argument. If omitted, all collections are returned.

0.3.5 (2021-08-09)
------------------

Expand Down
8 changes: 7 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
author = 'Open Contracting Partnership'

# The short X.Y version
version = '0.3.5'
version = '0.3.6'
# The full version, including alpha/beta/rc tags
release = version

Expand All @@ -38,6 +38,7 @@
# ones.
extensions = [
'sphinx.ext.autodoc',
"sphinx.ext.extlinks",
'sphinx.ext.viewcode',
]

Expand Down Expand Up @@ -73,6 +74,11 @@
}
autodoc_member_order = 'bysource'

extlinks = {
"ipython-sql": (
"https://github.com/catherinedevlin/ipython-sql/blob/b24ac6e9410416eafde86ae22fd8d6f34acbe05d/%s", None),
}

on_rtd = os.environ.get('READTHEDOCS', None) == 'True'

if not on_rtd: # only import and set the theme if we're building docs locally
Expand Down
30 changes: 16 additions & 14 deletions ocdskingfishercolab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from libcoveocds.config import LibCoveOCDSConfig
from notebook import notebookapp
from oauth2client.client import GoogleCredentials
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive

# Monkeypatch ipython-sql's sql run function, to add a comment linking to the
# colab notebook that it's run from
Expand Down Expand Up @@ -98,9 +98,11 @@ def list_source_ids(pattern=''):
Returns, as a ResultSet or DataFrame, a list of source IDs matching the given pattern.
:param str pattern: a substring, like "paraguay"
:returns: the results as a pandas DataFrame or an ipython-sql `ResultSet <https://github.com/catherinedevlin/ipython-sql/blob/b24ac6e9410416eafde86ae22fd8d6f34acbe05d/src/sql/run.py#L99>`__, depending on whether ``%config SqlMagic.autopandas`` is ``True`` or ``False`` respectively. This is the same behaviour as ipython-sql's ``%sql`` magic.
:returns: the results as a pandas DataFrame or an ipython-sql :ipython-sql:`ResultSet<src/sql/run.py#L99>`,
depending on whether ``%config SqlMagic.autopandas`` is ``True`` or ``False`` respectively. This is the
same behaviour as ipython-sql's ``%sql`` magic.
:rtype: pandas.DataFrame or sql.run.ResultSet
""" # noqa: E501
"""
sql = """
SELECT source_id
FROM collection
Expand All @@ -115,20 +117,20 @@ def list_source_ids(pattern=''):
return get_ipython().magic(f'sql {sql}')


def list_collections(source_id):
def list_collections(source_id=None):
"""
Returns, as a ResultSet or DataFrame, a list of collections with the given source ID.
:param str source_id: a source ID
:returns: the results as a pandas DataFrame or an ipython-sql `ResultSet <https://github.com/catherinedevlin/ipython-sql/blob/b24ac6e9410416eafde86ae22fd8d6f34acbe05d/src/sql/run.py#L99>`__, depending on whether ``%config SqlMagic.autopandas`` is ``True`` or ``False`` respectively. This is the same behaviour as ipython-sql's ``%sql`` magic.
:returns: the results as a pandas DataFrame or an ipython-sql :ipython-sql:`ResultSet<src/sql/run.py#L99>`,
depending on whether ``%config SqlMagic.autopandas`` is ``True`` or ``False`` respectively. This is the
same behaviour as ipython-sql's ``%sql`` magic.
:rtype: pandas.DataFrame or sql.run.ResultSet
""" # noqa: E501
sql = """
SELECT *
FROM collection
WHERE source_id = :source_id
ORDER BY id DESC
"""
sql = "SELECT * FROM collection"
if source_id:
sql += " WHERE source_id = :source_id"
sql += " ORDER BY id DESC"

# This inspects locals to find source_id
return get_ipython().magic(f'sql {sql}')
Expand Down Expand Up @@ -233,9 +235,9 @@ def get_ipython_sql_resultset_from_query(sql):
Parameters are taken from the scope this function is called from (same behaviour as ipython-sql's ``%sql`` magic).
:param str sql: a SQL statement
:returns: the results as a `ResultSet <https://github.com/catherinedevlin/ipython-sql/blob/b24ac6e9410416eafde86ae22fd8d6f34acbe05d/src/sql/run.py#L99>`__
:returns: the results as a :ipython-sql:`ResultSet<src/sql/run.py#L99>`
:rtype: sql.run.ResultSet
""" # noqa: E501
"""
ipython = get_ipython()
autopandas = ipython.magic('config SqlMagic.autopandas')
# Disable autopandas, so we know that the sql magic call will always return
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='ocdskingfishercolab',
version='0.3.5',
version='0.3.6',
author='Open Contracting Partnership',
author_email='data@open-contracting.org',
url='https://github.com/open-contracting/kingfisher-colab',
Expand All @@ -25,7 +25,7 @@
'libcoveocds',
'notebook',
'oauth2client',
'pydrive',
'pydrive2',
'requests',
'sqlalchemy',
],
Expand Down

0 comments on commit 40bf6cc

Please sign in to comment.