Python client for the Guidelinely Environmental Guidelines API
Project description
Guidelinely Python Client
Python client library for the Guidelinely API - an environmental guideline calculation and search API.
Calculate context-dependent environmental guideline values for chemical parameters (Aluminum, Copper, Lead, etc.) in various media (water, soil, sediment) based on environmental conditions (pH, hardness, temperature, etc.).
This Python client mirrors the functionality of the R client.
Features
- ✅ Metadata Queries: List parameters, search, explore media types and sources
- ✅ Single Calculations: Calculate guidelines for individual parameters
- ✅ Batch Calculations: Efficiently calculate multiple parameters (up to 50)
- ✅ Context-Aware: Support for pH, hardness, temperature, and other environmental factors
- ✅ Unit Conversion: Optional target unit specification
- ✅ Type Safety: Full Pydantic model support for request/response validation
- ✅ Comprehensive Tests: Mock-based test suite with responses library
Installation
# Clone the repository
git clone https://github.com/mpdavison/envguidelines-py.git
cd envguidelines-py
# Install in development mode
pip install -e .
# Or install with dev dependencies
pip install -e ".[dev]"
Quick Start
from guidelinely import calculate_guidelines
# Calculate aluminum guidelines in surface water
result = calculate_guidelines(
parameter="Aluminum",
media="surface_water",
context={
"pH": "7.0 1", # pH 7.0 (dimensionless)
"hardness": "100 mg/L" # Water hardness as CaCO3
}
)
print(f"Found {result.total_count} guidelines")
for guideline in result.results:
print(f"{guideline.parameter}: {guideline.value} ({guideline.source})")
API Key
Calculation endpoints optionally accept an API key. Set it as an environment variable:
export GUIDELINELY_API_KEY="your_api_key_here"
Metadata endpoints (list parameters, search, etc.) work without authentication.
Usage Examples
Basic Metadata Queries
from guidelinely import (
health_check,
list_parameters,
search_parameters,
list_media,
list_sources,
get_stats,
)
# Check API health
status = health_check()
# List all chemical parameters
params = list_parameters()
print(f"Available parameters: {len(params)}")
# Search for ammonia-related parameters
ammonia = search_parameters("ammon")
print(ammonia) # ['Ammonia', 'Ammonium', ...]
# Get available media types
media = list_media()
# {'surface_water': 'Surface Water', 'soil': 'Soil', ...}
# View guideline sources
sources = list_sources()
# Database statistics
stats = get_stats()
Single Parameter Calculation
from guidelinely import calculate_guidelines
import os
os.environ["GUIDELINELY_API_KEY"] = "your_key"
result = calculate_guidelines(
parameter="Copper",
media="surface_water",
context={
"pH": "7.5 1",
"hardness": "150 mg/L",
"temperature": "15 °C",
"chloride": "75 mg/L"
},
target_unit="μg/L" # Optional unit conversion
)
# Filter results
chronic_aquatic = [
g for g in result.results
if g.receptor == "Aquatic Life" and g.exposure_duration == "chronic"
]
Batch Calculations
from guidelinely import calculate_batch
# Calculate multiple parameters at once (more efficient)
result = calculate_batch(
parameters=["Aluminum", "Copper", "Lead", "Zinc", "Cadmium"],
media="surface_water",
context={
"pH": "7.5 1",
"hardness": "150 mg/L",
"temperature": "15 °C"
}
)
print(f"Total: {result.total_count} guidelines")
# With per-parameter unit conversion
result = calculate_batch(
parameters=[
"Aluminum",
{"name": "Copper", "target_unit": "μg/L"},
{"name": "Lead", "target_unit": "mg/L"}
],
media="surface_water",
context={"pH": "7.0 1", "hardness": "100 mg/L"}
)
Soil Calculations
result = calculate_guidelines(
parameter="Lead",
media="soil",
context={
"pH": "6.5 1",
"organic_matter": "3.5 %",
"cation_exchange_capacity": "15 meq/100g"
}
)
Environmental Context Parameters
All context parameters must be strings with units (Pint format):
Water (surface_water, groundwater)
context = {
"pH": "7.0 1", # Dimensionless - use "1" as unit
"hardness": "100 mg/L", # mg/L as CaCO3
"temperature": "20 °C",
"chloride": "50 mg/L"
}
Soil
context = {
"pH": "6.5 1",
"organic_matter": "3.5 %",
"cation_exchange_capacity": "15 meq/100g"
}
Sediment
context = {
"pH": "7.0 1",
"organic_matter": "2.5 %",
"grain_size": "0.5 mm"
}
Data Models
The library uses Pydantic for type-safe data handling:
from guidelinely.models import GuidelineResponse, CalculationResponse
# All API responses are strongly typed
result: CalculationResponse = calculate_guidelines(...)
# Access typed fields
for guideline in result.results:
guideline.parameter # str
guideline.value # str (PostgreSQL unitrange format)
guideline.lower # Optional[float]
guideline.upper # Optional[float]
guideline.unit # str
guideline.is_calculated # bool
Guideline Value Format
Guidelines use PostgreSQL unitrange format:
[10 μg/L,100 μg/L]- Range from 10 to 100 μg/L(,87.0 μg/L]- Upper limit only (≤87.0 μg/L)[5.0 mg/L,)- Lower limit only (≥5.0 mg/L)
Parsed into lower, upper, and unit fields.
Development
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run tests with coverage
pytest --cov=guidelinely --cov-report=html
# Format code
black guidelinely/ tests/ examples/
# Type checking
mypy guidelinely/
# Linting
ruff check guidelinely/ tests/
API Reference
Client Functions
health_check()- Service health checkreadiness_check()- Database readiness checklist_parameters()- List all chemical parameterssearch_parameters(q, media)- Search parameterslist_media()- List media typeslist_sources()- List guideline sourcesget_stats()- Database statisticscalculate_guidelines(parameter, media, context, target_unit, api_key)- Calculate single parametercalculate_batch(parameters, media, context, api_key)- Batch calculate (max 50 parameters)
Models
GuidelineResponse- Single guideline resultCalculationResponse- Calculation endpoint responseCalculateRequest- Single calculation request bodyBatchCalculateRequest- Batch calculation request body
Examples
See the examples/ directory for complete working examples:
01_basic_metadata.py- Basic metadata queries02_calculate_single_parameter.py- Single parameter calculations03_batch_calculations.py- Batch calculations04_soil_calculations.py- Soil-specific calculations05_advanced_workflow.py- Advanced filtering and analysis
Resources
- API Documentation: https://guidelines.1681248.com/docs
- OpenAPI Spec: https://guidelines.1681248.com/openapi.json
- R Client: https://github.com/mpdavison/envguidelines
- Issue Tracker: https://github.com/mpdavison/envguidelines-py/issues
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file guidelinely-1.0.4.tar.gz.
File metadata
- Download URL: guidelinely-1.0.4.tar.gz
- Upload date:
- Size: 25.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
815241f72864ed5341f0a8918cf7199ff3a17b781f1a3fff9d593ea9e11a6afe
|
|
| MD5 |
b875bcdf96c397f3cc5d33f8baed3f47
|
|
| BLAKE2b-256 |
7477f9298b83e65132fe73852f02704503f6d6b10aa7780f36d7d48d593d9002
|
Provenance
The following attestation bundles were made for guidelinely-1.0.4.tar.gz:
Publisher:
ci.yml on mpdavison/envguidelines-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
guidelinely-1.0.4.tar.gz -
Subject digest:
815241f72864ed5341f0a8918cf7199ff3a17b781f1a3fff9d593ea9e11a6afe - Sigstore transparency entry: 736018013
- Sigstore integration time:
-
Permalink:
mpdavison/envguidelines-py@eb16341692fb90f67ffe9d8f82f41cc3f5b99f6b -
Branch / Tag:
refs/tags/v1.0.4 - Owner: https://github.com/mpdavison
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@eb16341692fb90f67ffe9d8f82f41cc3f5b99f6b -
Trigger Event:
push
-
Statement type:
File details
Details for the file guidelinely-1.0.4-py3-none-any.whl.
File metadata
- Download URL: guidelinely-1.0.4-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff09f0770f86309a2930c02629ccdb69397976859a0ac1a5abb09dbe9104fa46
|
|
| MD5 |
d14d3879356a42ff243702eb28a963f6
|
|
| BLAKE2b-256 |
3dfba01128a50e0a73cb330f18ca2c5eb298812dff4cead52a3133a5c3cd975d
|
Provenance
The following attestation bundles were made for guidelinely-1.0.4-py3-none-any.whl:
Publisher:
ci.yml on mpdavison/envguidelines-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
guidelinely-1.0.4-py3-none-any.whl -
Subject digest:
ff09f0770f86309a2930c02629ccdb69397976859a0ac1a5abb09dbe9104fa46 - Sigstore transparency entry: 736018019
- Sigstore integration time:
-
Permalink:
mpdavison/envguidelines-py@eb16341692fb90f67ffe9d8f82f41cc3f5b99f6b -
Branch / Tag:
refs/tags/v1.0.4 - Owner: https://github.com/mpdavison
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@eb16341692fb90f67ffe9d8f82f41cc3f5b99f6b -
Trigger Event:
push
-
Statement type: