pyportal
A Python wrapper around the API for the Natural History Museum's Data Portal.
This module is new and under development, so does not include all actions available through the API; it prioritises the record search functionality. It is liable to change dramatically between versions.
We have more generic (and comprehensive) API documentation available here.
Requirements
- Python 3.7+
Installation
pip install -e git+git://github.com/NaturalHistoryMuseum/nhm-pyportal.git#egg=pyportal
Quickstart Usage
Start by creating an API object:
import pyportal
api = pyportal.API()
Searching
You can either search for all records, or specifically for assets/media. The syntax is very similar for both.
from pyportal import constants
# specify any resource you want, but the IDs for the specimens and index lots resources are built in
resource_id = constants.resources.specimens
# find all records in the specimens resource
search = api.records(resource_id)
# OR find all assets (images) in the specimens resource
search = api.assets(resource_id)
The search above is equivalent to this search on the Data Portal website.
There is a helper method to transform a data portal URL into a dict that can be passed into the search constructor.
url = 'https://data.nhm.ac.uk/dataset/56e711e6-c847-4f99-915a-6894bb5c5dea/resource/05ff2255-c38a-40c9-b657-4ccb55ab2feb?filters=collectionCode%3Abot'
# these two searches are equivalent
print(api.records(constants.resources.specimens, collectionCode='bot').count()) # 775440
print(api.records(**api.from_url(url)).count()) # 775440
You can specify the following parameters (all are optional):
query: a free-text search, e.g.query='bugs'sort: a list of fields and sort directions, e.g.sort=['country asc', 'family desc']fields: a list of fields to return for each record (leave blank to return all), e.g.fields=['country', 'family', 'genus']offset: skip the first n results, e.g.offset=50limit: return only n results per page (defaults to 100), e.g.limit=10
Any other keyword arguments will be considered filters.
search = api.records(constants.resources.specimens, query='bugs', sort=['country asc', 'family desc'], fields=['country', 'family', 'genus'], offset=50, limit=10)
Viewing results
Iterate through all results using .all() (this ignores limit):
for record in search.all():
print(record)
Or just view the first one with .first():
print(search.first())
You could also view a page (blocks of records in the size set by limit) at a time using .next():
try:
page = search.next()
for record in page:
print(record)
except StopIteration: # raised by search.next() if there's no next page
print('No more results.')
If you just want the total number of records, use .count():
print(search.count())
Metadata
Release files for nhm-pyportal 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nhm-pyportal-0.1.tar.gz | 6.2 kB | Without provenance | Details for nhm-pyportal-0.1.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nhm_pyportal-0.1-py3.7.egg | Legacy Egg format | - | - | Details for nhm_pyportal-0.1-py3.7.egg |
| nhm_pyportal-0.1-py3-none-any.whl | Python 3 | none | any | Details for nhm_pyportal-0.1-py3-none-any.whl |
Total release size: 20.9 kB
Release files / nhm-pyportal-0.1.tar.gz
| Download URL | nhm-pyportal-0.1.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ceac4f5584564aab139b8e3fd3fdfb679add2a8a9d24c68b15ffda324b1c25d0
|
|
BLAKE2b-256 checksum How to use checksums |
e18c4712e310a70f51af3af38c4d9c7e2946525673fd5b584e276f928d186b97
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
|
Release files / nhm_pyportal-0.1-py3.7.egg
| Download URL | nhm_pyportal-0.1-py3.7.egg |
|---|---|
| Size | 7.2 kB |
| Tags | Egg |
|
SHA-256 checksum How to use checksums |
c37ec0c5e0b0199eaeba8d22314696efdf2259d352ec27890d982d2793bf18a2
|
|
BLAKE2b-256 checksum How to use checksums |
172366cfa2bc1bcbee6dfbee0cabe6b40c56de91436134edd8680713a2515dca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
|
Release files / nhm_pyportal-0.1-py3-none-any.whl
| Download URL | nhm_pyportal-0.1-py3-none-any.whl |
|---|---|
| Size | 7.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b583336bc87ddd7bf3ceb49d845e80c4b1798a5d5217d29b5d347cb8fe6ff356
|
|
BLAKE2b-256 checksum How to use checksums |
0313eb76bc8c33a1c9264fc016ed92784add3dc61fa8679b348006c627adad21
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
|
Security for nhm-pyportal 0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.