Skip to content

Commit

Permalink
Merge pull request #77 from sckott/occ-count
Browse files Browse the repository at this point in the history
occurrences count changes
  • Loading branch information
sckott authored Mar 5, 2021
2 parents f83c594 + 78b8ba0 commit 4735946
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ jobs:
python-version: [3.6, 3.7, 3.8, 3.9]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GBIF_USER: ${{ secrets.GBIF_USER }}
GBIF_PWD: ${{ secrets.GBIF_PWD }}
GBIF_EMAIL: ${{ secrets.GBIF_EMAIL }}
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand Down
12 changes: 11 additions & 1 deletion pygbif/gbifutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,21 @@ def check_data(x, y):


def len2(x):
if x.__class__ is str:
if isinstance(x, int):
return len([x])
elif isinstance(x, str):
return len([x])
else:
return len(x)

def stuff(**kwargs):
return kwargs

def check_param_lens(**kwargs):
tmp = {k: v for k, v in kwargs.items() if v is not None}
for k,v in tmp.items():
if len2(v) > 1:
raise TypeError(k + " must be length 1")

def get_meta(x):
if has_meta(x):
Expand Down
10 changes: 9 additions & 1 deletion pygbif/occurrences/count.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pygbif.gbifutils import gbif_baseurl, bool2str, gbif_GET
from pygbif.gbifutils import gbif_baseurl, bool2str, gbif_GET, check_param_lens


def count(
Expand All @@ -16,6 +16,10 @@ def count(
"""
Returns occurrence counts for a predefined set of dimensions
For all parameters below, only one value allowed per function call.
See :func:`~occurrences.search` for passing more than one value
per parameter.
:param taxonKey: [int] A GBIF occurrence identifier
:param basisOfRecord: [str] A GBIF occurrence identifier
:param country: [str] A GBIF occurrence identifier
Expand All @@ -36,6 +40,10 @@ def count(
occurrences.count(isGeoreferenced = True)
occurrences.count(basisOfRecord = 'OBSERVATION')
"""
check_param_lens(taxonKey=taxonKey,basisOfRecord=basisOfRecord,
country=country,isGeoreferenced=isGeoreferenced,
datasetKey=datasetKey,publishingCountry=publishingCountry,
typeStatus=typeStatus,issue=issue,year=year)
url = gbif_baseurl + "occurrence/count"
isGeoreferenced = bool2str(isGeoreferenced)
out = gbif_GET(
Expand Down
6 changes: 5 additions & 1 deletion test/test-occurrences-count.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for occurrences module - count methods"""
import pytest
import vcr
from pygbif import occurrences

Expand Down Expand Up @@ -35,6 +36,10 @@ def test_count():
res = occurrences.count(taxonKey=3329049)
assert int == res.__class__

def test_count_param_length():
"occurrences.count_param_length"
with pytest.raises(TypeError):
occurrences.count(datasetKey=['foo', 'bar'])

@vcr.use_cassette("test/vcr_cassettes/test_count_basisofrecord.yaml")
def test_count_basisofrecord():
Expand Down Expand Up @@ -79,7 +84,6 @@ def test_count_schema():
assert dict == res[0].__class__
assert "dimensions" == list(res[0].keys())[0]


@vcr.use_cassette("test/vcr_cassettes/test_count_publishingcountries.yaml")
def test_count_publishingcountries():
"occurrences.count_publishingcountries - basic test"
Expand Down

0 comments on commit 4735946

Please sign in to comment.