PyCompTox v0.2.0 - Improvements Summary¶
๐ New Features Implemented¶
1. โ Persistent API Key Storage¶
Problem Solved: Users no longer need to provide their API key every time they use PyCompTox.
Implementation:
- Created config.py module with API key management functions
- API keys are saved in user's application data directory:
- Windows: %APPDATA%\PyCompTox\api_key.txt
- macOS/Linux: ~/.pycomptox/api_key.txt
- File permissions set to user-only (600) on Unix systems
New Functions:
- save_api_key(api_key) - Save API key to config file
- load_api_key() - Load API key from config or environment
- delete_api_key() - Delete saved API key
- get_config_info() - Get configuration information
- get_config_dir() - Get configuration directory path
Usage:
from pycomptox import save_api_key, Chemical
# One-time setup
save_api_key("your_api_key_here")
# Future use - no API key needed!
client = Chemical()
2. โ Command-Line API Key Management Utility¶
CLI Command: pycomptox-setup
Commands:
pycomptox-setup set YOUR_API_KEY # Save API key
pycomptox-setup test # Test connection
pycomptox-setup show # Show masked key
pycomptox-setup delete # Delete saved key
Features: - Shows current configuration status - Masks API key for security when displaying - Tests API connection with a real search - User-friendly error messages
3. โ Built-in Rate Limiting¶
Problem Solved: Prepare for future API rate limits and be a good API citizen.
Implementation:
- Added time_delay_between_calls parameter to Chemical class
- Added _last_call_time tracking attribute
- Added _enforce_rate_limit() method
- Updated _make_request() to enforce delays automatically
- Added 429 (Too Many Requests) error handling
Default Value: 0.0 seconds (no delay)
Usage:
# Create client with 0.5 second delay between calls
client = Chemical(time_delay_between_calls=0.5)
# Make multiple calls - delay is enforced automatically
for formula in ["C15H16O2", "C16H24N2O5S"]:
results = client.search_by_msready_formula(formula)
How It Works: 1. Tracks timestamp of last API call 2. Before each call, calculates time elapsed 3. If elapsed < delay, sleeps for remaining time 4. Makes the call and updates timestamp
4. โ Reorganized Project Structure¶
Changes:
- Created tests/ folder for all test files
- Moved test_api.py to tests/
- Moved example.py to tests/
- Created tests/__init__.py
- Created docs/ folder for documentation
New Structure:
PyCompTox/
โโโ src/
โ โโโ pycomptox/
โ โโโ __init__.py # Updated with new exports
โ โโโ config.py # NEW: API key management
โ โโโ search.py # Updated with rate limiting
โโโ tests/ # NEW: Test folder
โ โโโ __init__.py
โ โโโ test_api.py # Updated tests
โ โโโ example.py # Updated examples
โโโ docs/ # NEW: Documentation folder
โ โโโ API_KEY_AND_RATE_LIMITING.md
โโโ requirements.txt
โโโ README.md # Updated with new features
โโโ LICENSE
Note: The CLI tool pycomptox-setup is now part of the package (in src/pycomptox/__main__.py).
5. โ Updated Test Suite¶
File: tests/test_api.py
Improvements:
- Automatic API key loading (no manual setup needed)
- Added rate limiting test (--test-rate-limit flag)
- Better error messages
- Updated path resolution for new structure
Features Tested: - โ Automatic API key loading - โ Search by exact value - โ Search by formula - โ Search by substring - โ Rate limiting (with flag)
Test Results:
โ API key loaded successfully
โ Found chemical: Bisphenol A
โ Found 297 chemicals with formula C15H16O2
โ Found 881 chemicals containing 'Bisphenol'
โ Rate limiting is working correctly!
6. โ Enhanced Chemical Class¶
Updated Constructor:
def __init__(
self,
api_key: Optional[str] = None, # Now optional!
base_url: str = "https://comptox.epa.gov/ctx-api",
time_delay_between_calls: float = 0.0 # NEW parameter
):
New Attributes:
- time_delay_between_calls: float - Minimum delay between calls
- _last_call_time: float - Timestamp of last call
New Methods:
- _enforce_rate_limit() - Internal method to enforce delays
API Key Loading Priority:
1. api_key parameter (if provided)
2. COMPTOX_API_KEY environment variable
3. Saved configuration file
4. Raise ValueError if none found
7. โ Updated Documentation¶
README.md: - Added API Key Setup section - Added Rate Limiting section - Updated Quick Start examples - Updated Project Structure - Added API key storage location info
New Documentation:
- docs/API_KEY_AND_RATE_LIMITING.md - Comprehensive guide
- Includes examples, best practices, troubleshooting
Updated Files:
- IMPLEMENTATION.md - Updated with new features
- Example usage in all test files
๐งช Testing Results¶
All features have been tested and verified:
API Key Management¶
โ API key saved to: C:\Users\aliak\AppData\Roaming\PyCompTox\api_key.txt
โ API key loaded successfully
โ Test search successful: Found Bisphenol A
Automatic API Key Loading¶
โ Client initialized successfully (no API key parameter needed)
โ Search by exact value: Found Bisphenol A
โ Search by formula: Found 297 chemicals
Rate Limiting¶
โ Created client with 0.5 second delay
โ Made 3 calls in 1.912 seconds (expected: ~1.0s minimum)
โ Rate limiting is working correctly!
๐ Code Statistics¶
New Files: 3
- src/pycomptox/config.py (143 lines) - Configuration management
- src/pycomptox/__main__.py (141 lines) - CLI tool
- tests/__init__.py (3 lines)
- docs/API_KEY_AND_RATE_LIMITING.md (343 lines)
Modified Files: 6
- src/pycomptox/__init__.py - Added config exports
- src/pycomptox/search.py - Added rate limiting
- tests/test_api.py - Updated for new features
- tests/example.py - Updated for new features
- README.md - Comprehensive updates
- IMPLEMENTATION.md - Updated summary
Total Lines Added: ~700 lines (including docs)
๐ฏ Benefits¶
- Better User Experience
- Set API key once, use everywhere
- No more hardcoded API keys in scripts
-
Easier to share code examples
-
Production Ready
- Built-in rate limiting
- Proper error handling for rate limits
-
Secure API key storage
-
Well Documented
- Comprehensive README
- Detailed guide for new features
-
Examples and best practices
-
Maintainable
- Clean code organization
- Separated concerns (config, search, tests)
-
Easy to extend
-
Developer Friendly
- Command-line utility for API key management
- Automatic configuration
- Clear error messages
๐ Usage Example¶
Before (Old way):
from pycomptox.chemical import Chemical
# Had to provide API key every time
client = Chemical(api_key="abc123xyz789")
results = client.search_by_exact_value("Bisphenol A")
After (New way):
from pycomptox.chemical import Chemical
# API key loaded automatically!
client = Chemical()
results = client.search_by_exact_value("Bisphenol A")
# With rate limiting for batch operations
client = Chemical(time_delay_between_calls=0.5)
for chemical in large_list:
results = client.search_by_exact_value(chemical)
๐ Next Steps¶
The Chemical Search Resource implementation is complete with all requested improvements. Ready for:
- โ Production use
- โ Expanding to other API sections (Chemical Details, Properties, etc.)
- โ Adding more advanced features (batch operations, caching, async)
- โ Creating unit tests with pytest
- โ Publishing to PyPI
โจ Summary¶
All requested improvements have been successfully implemented:
- โ
Tests moved to
tests/folder - โ
API key saved and auto-loaded via
save_api_key()function - โ
Rate limiting implemented with
time_delay_between_callsparameter - โ Default delay set to 0.0 seconds as requested
The package is now more user-friendly, production-ready, and well-documented! ๐