Skip to content

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

  1. Better User Experience
  2. Set API key once, use everywhere
  3. No more hardcoded API keys in scripts
  4. Easier to share code examples

  5. Production Ready

  6. Built-in rate limiting
  7. Proper error handling for rate limits
  8. Secure API key storage

  9. Well Documented

  10. Comprehensive README
  11. Detailed guide for new features
  12. Examples and best practices

  13. Maintainable

  14. Clean code organization
  15. Separated concerns (config, search, tests)
  16. Easy to extend

  17. Developer Friendly

  18. Command-line utility for API key management
  19. Automatic configuration
  20. 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):

# One-time setup
pycomptox-setup set abc123xyz789
pycomptox-setup test

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:

  1. โœ… Production use
  2. โœ… Expanding to other API sections (Chemical Details, Properties, etc.)
  3. โœ… Adding more advanced features (batch operations, caching, async)
  4. โœ… Creating unit tests with pytest
  5. โœ… Publishing to PyPI

โœจ Summary

All requested improvements have been successfully implemented:

  1. โœ… Tests moved to tests/ folder
  2. โœ… API key saved and auto-loaded via save_api_key() function
  3. โœ… Rate limiting implemented with time_delay_between_calls parameter
  4. โœ… Default delay set to 0.0 seconds as requested

The package is now more user-friendly, production-ready, and well-documented! ๐ŸŽ‰