Skip to content

Commit

Permalink
Add type annotations (#44)
Browse files Browse the repository at this point in the history
Fixes #36
  • Loading branch information
chuckwondo committed Apr 23, 2024
1 parent 8738280 commit c9bc301
Show file tree
Hide file tree
Showing 9 changed files with 452 additions and 181 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ jobs:
poetry run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
poetry run flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Check types with mypy
run: |
poetry run mypy cmr tests
- name: Test with pytest
run: |
poetry run pytest
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
accommodation for Python versions older than 3.8 and updated CI build to test
against Python versions 3.8 through 3.12. Also, fixed all flake8 warnings.

### Added

- [issues/36](https://github.com/nasa/python_cmr/issues/36) Added type annotations.

## Fixed

- [issues/42](https://github.com/nasa/python_cmr/issues/42) Fixed bug where a
Expand Down
61 changes: 43 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ Python CMR
[![Python versions](https://img.shields.io/pypi/pyversions/python_cmr.svg)](https://pypi.python.org/pypi/python_cmr)
[![Build Status](https://github.com/nasa/python_cmr/actions/workflows/python-app.yml/badge.svg)](https://github.com/nasa/python_cmr/actions)
[![CodeQL](https://github.com/nasa/python_cmr/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/nasa/python_cmr/actions/workflows/codeql-analysis.yml)
[![Checked with mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)

Python CMR is an easy to use wrapper to the NASA
EOSDIS [Common Metadata Repository API](https://cmr.earthdata.nasa.gov/search/). This package aims to make querying the
API intuitive and less error-prone by providing methods that will preemptively check for invalid input and handle the
URL encoding the CMR API expects.

Getting access to NASA's earth science metadata is as simple as this:

```python
from cmr import CollectionQuery, GranuleQuery, ToolQuery, ServiceQuery, VariableQuery

Expand Down Expand Up @@ -53,15 +55,17 @@ Installation
============

To install from pypi:
```
$ pip install python-cmr
```

To install from github, perhaps to try out the dev branch:
```plain
pip install python-cmr
```
$ git clone https://github.com/nasa/python_cmr
$ cd python-cmr
$ pip install .

To install from GitHub, perhaps to try out the dev branch:

```plain
git clone https://github.com/nasa/python_cmr
cd python-cmr
pip install .
```

Examples
Expand All @@ -72,6 +76,7 @@ methods used to build a query for CMR. Not all parameters provided by the CMR AP
python-cmr.

The following methods are available to both collection and granule queries:

```python
# search for granules matching a specific product/short_name
api.short_name("AST_L1T")
Expand Down Expand Up @@ -120,6 +125,7 @@ api.mode(CMR_UAT)
```

Granule searches support these methods (in addition to the shared methods above):

```python
# search for a granule by its unique ID
api.granule_ur("SC:AST_L1T.003:2150315169")
Expand All @@ -138,12 +144,13 @@ api.cloud_cover(25, 75)
api.instrument("MODIS")
api.platform("Terra")

# filter by a sort_key note: sort_keys are require some other fields to find some existing granules before they can be sorted

# filter by a sort_key note: sort_keys are require some other fields to find
# some existing granules before they can be sorted
api.parameters(short_name="OMNO2", version="003", provider='GES_DISC', sort_key='-start_date')
```

Collection searches support these methods (in addition to the shared methods above):

```python
# search for collections from a specific archive center
api.archive_center("LP DAAC")
Expand All @@ -162,6 +169,7 @@ api.service_concept_id('S1962070864-POCLOUD')
```

Service searches support the following methods

```python
# Search via provider
api = ServiceQuery()
Expand All @@ -178,6 +186,7 @@ api.concept_id('S1962070864-POCLOUD')
```

Tool searches support the following methods

```python
# Search via provider
api = ToolQuery()
Expand All @@ -194,6 +203,7 @@ api.concept_id('TL2092786348-POCLOUD')
```

Variable searches support the following methods

```python
# Search via provider
api = VariableQuery()
Expand All @@ -211,6 +221,7 @@ api.concept_id('V2112019824-POCLOUD')

As an alternative to chaining methods together to set the parameters of your query, a method exists to allow you to pass
your parameters as keyword arguments:

```python
# search for AST_L1T version 003 granules at latitude 42, longitude -100
api.parameters(
Expand All @@ -224,6 +235,7 @@ Note: the kwarg key should match the name of a method from the above examples, a
parameter that requires multiple values.

To inspect and retrieve results from the API, the following methods are available:

```python
# inspect the number of results the query will return without downloading the results
print(api.hits())
Expand All @@ -240,11 +252,13 @@ granules = api.get_all() # this is a shortcut for api.get(api.hits())

By default the responses will return as json and be accessible as a list of python dictionaries. Other formats can be
specified before making the request:

```python
granules = api.format("echo10").get(100)
```

We can add token to the api calls by setting headers using the following functions:

```python
# Use token function for EDL echo-token or launchpad token
api.token(token)
Expand All @@ -270,55 +284,66 @@ Collection queries also support the following formats:
- dif
- dif10
- opendata
- umm\_json
- umm\_json\_vX\_Y (ex: umm\_json\_v1\_9)
- umm_json
- umm_json_vX_Y (ex: umm_json_v1_9)

# Developing
Developing
==========

python-cmr uses the [poetry](https://python-poetry.org/) build system. Download and install poetry before starting
development

## Install Dependencies
Install Dependencies
--------------------

With dev dependencies:

```shell
poetry install
```

Without dev dependencies:

```shell
poetry install --no-dev
```

## Update Dependencies
Update Dependencies
-------------------

```shell
poetry update
```

## Add new Dependency
Add new Dependency
------------------

```shell
poetry add requests
```

Development-only dependency:

```shell
poetry add --dev pytest
```

## Build project
Build project
-------------

```shell
poetry build
```

## Lint project
Lint project
------------

```shell
poetry run flake8
```

## Run Tests
Run Tests
---------

```shell
poetry run pytest
Expand Down
Empty file added cmr/py.typed
Empty file.
Loading

0 comments on commit c9bc301

Please sign in to comment.