Enh/thrustcurve api cache#881
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #881 +/- ##
===========================================
+ Coverage 80.27% 80.32% +0.05%
===========================================
Files 104 106 +2
Lines 12769 13001 +232
===========================================
+ Hits 10250 10443 +193
- Misses 2519 2558 +39 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Gui-FernandesBR
left a comment
There was a problem hiding this comment.
This way the cache will be deleted everytime you restart a python session. It doesn't solve 100% of the problem.
My idea is for us to create a .rocketpy_cache folder and save every download inside it. Before downloading a motor rom the API, we simply read it from file.
There should be options like "no_cache = Tre" to skip cache.
Please dedicate a few minutes to research how other python libraries tackle the same problem (caching downloads from rest API requests). I know OSMnx is a good example, but certainly there are many other libs doing the same thing.
Can you use these guidelines to improve this PR pls? I will mark it as a draft for now, but pls lemme know whener you finish it again so I can review it
Adittionaly, I think you have a problem with git tree, as I can see 43 commits on the commits section. I recommend you running the following: git rebase -i develop, then you select which commits you really need in this PR, and drop the others.
cc45dc7 to
e6c0351
Compare
|
Hi @Gui-FernandesBR , thank you for the feedback! I have implemented the changes you requested: Persistent Caching: I updated the logic to use a persistent cache directory located at Path.home() / ".rocketpy_cache". The code now checks if the motor file exists locally before making a network request, persisting data across Python sessions. no_cache Option: I added a no_cache=False argument to load_from_thrustcurve_api. Setting it to True forces a fresh download from the API. I also ensured that the unit tests use a temporary directory for the cache (via monkeypatch) so they don't clutter the user's home folder during testing. |
Amazing! You're a star @Monta120 !! |
Gui-FernandesBR
left a comment
There was a problem hiding this comment.
LGTM, only need a final touch on the CHANGELOG file.
And let's wait for copilot's review as well.
Great work
There was a problem hiding this comment.
Pull request overview
This PR adds caching functionality to the ThrustCurve API integration to avoid redundant network requests when loading the same motor multiple times. The implementation uses a file-based cache stored in ~/.rocketpy_cache and introduces a no_cache parameter to allow bypassing the cache when needed.
Key Changes
- Introduced automatic caching of ThrustCurve API responses to avoid repeated downloads
- Added
no_cacheparameter to force fresh API downloads when needed - Implemented comprehensive test coverage for caching behavior
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
| rocketpy/motors/motor.py | Adds cache directory initialization, implements caching logic in _call_thrustcurve_api, and adds no_cache parameter to load_from_thrustcurve_api |
| tests/unit/motors/test_genericmotor.py | Adds new test test_thrustcurve_api_cache to verify caching works correctly, including cache reads, writes, and bypass functionality |
| docs/user/motors/genericmotor.rst | Updates documentation to explain caching behavior and the no_cache parameter with examples |
| CHANGELOG.md | Documents the new caching feature and related changes |
Comments suppressed due to low confidence (1)
rocketpy/motors/motor.py:2018
- The
no_cacheparameter is missing from the docstring. According to NumPy docstring conventions, all function parameters should be documented in the "Parameters" section. Add documentation for theno_cacheparameter, including its type (bool), default value (False), and description.
def load_from_thrustcurve_api(name: str, no_cache: bool = False, **kwargs):
"""
Creates a Motor instance by downloading a .eng file from the ThrustCurve API
based on the given motor name.
Parameters
----------
name : str
The motor name according to the API (e.g., "Cesaroni_M1670" or "M1670").
Both manufacturer-prefixed and shorthand names are commonly used; if multiple
motors match the search, the first result is used.
**kwargs :
Additional arguments passed to the Motor constructor or loader, such as
dry_mass, nozzle_radius, etc.
|
@Monta120 pls mark all the comments as "solved" after solving it, so we can merge it directly |
|
I'm working on testing exception handling for cache operations to ensure above 80.27% coverage. |
Pull request type
Checklist
black rocketpy/ tests/) has passed locallypytest tests -m slow --runslow) have passed locallyCHANGELOG.mdhas been updated (if relevant)Current behavior
Every call to
GenericMotor.load_from_thrustcurve_apitriggers a new download from the ThrustCurve API, even if the same motor has already been requested. This results in repeated network requests and slower test runs.New behavior
Breaking change