From f707ddeeee5c29881c0de96092a01e78a905a401 Mon Sep 17 00:00:00 2001 From: Jordan Cook Date: Sun, 29 Aug 2021 17:55:33 -0500 Subject: Some formatting for examples --- examples/basic_patching.py | 40 ++++++++++++++++++++++++++++++++++++++++ examples/basic_sessions.py | 34 ++++++++++++++++++++++++++++++++++ examples/basic_usage.py | 34 ---------------------------------- examples/generate_test_db.py | 2 +- examples/session_patch.py | 40 ---------------------------------------- 5 files changed, 75 insertions(+), 75 deletions(-) create mode 100755 examples/basic_patching.py create mode 100755 examples/basic_sessions.py delete mode 100755 examples/basic_usage.py delete mode 100755 examples/session_patch.py (limited to 'examples') diff --git a/examples/basic_patching.py b/examples/basic_patching.py new file mode 100755 index 0000000..def0ead --- /dev/null +++ b/examples/basic_patching.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# flake8: noqa: F841 +""" +The same as `basic_sessions.py`, but using {ref}`patching` +""" +import time + +import requests + +import requests_cache + +# After installation, all requests functions and Session methods will be cached +requests_cache.install_cache('example_cache', backend='sqlite') + + +def main(): + # The real request will only be made once; afterward, the cached response is used + for i in range(5): + response = requests.get('http://httpbin.org/get') + + # This is more obvious when calling a slow endpoint + for i in range(5): + response = requests.get('http://httpbin.org/delay/2') + + # Caching can be disabled if we want to get a fresh page and not cache it + with requests_cache.disabled(): + print(requests.get('http://httpbin.org/ip').text) + + # Get some debugging info about the cache + print(requests_cache.get_cache()) + print('Cached URLS:', requests_cache.get_cache().urls) + + # Uninstall to remove caching from all requests functions + requests_cache.uninstall_cache() + + +if __name__ == "__main__": + t = time.time() + main() + print('Elapsed: %.3f seconds' % (time.time() - t)) diff --git a/examples/basic_sessions.py b/examples/basic_sessions.py new file mode 100755 index 0000000..42a6dd2 --- /dev/null +++ b/examples/basic_sessions.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# flake8: noqa: F841 +""" +A simple example using requests-cache with [httpbin](https://httpbin.org) +""" +import time + +from requests_cache import CachedSession + + +def main(): + session = CachedSession('example_cache', backend='sqlite') + + # The real request will only be made once; afterward, the cached response is used + for i in range(5): + response = session.get('http://httpbin.org/get') + + # This is more obvious when calling a slow endpoint + for i in range(5): + response = session.get('http://httpbin.org/delay/2') + + # Caching can be disabled if we want to get a fresh page and not cache it + with session.cache_disabled(): + print(session.get('http://httpbin.org/ip').text) + + # Get some debugging info about the cache + print(session.cache) + print('Cached URLS:', list(session.cache.urls)) + + +if __name__ == "__main__": + t = time.time() + main() + print('Elapsed: %.3f seconds' % (time.time() - t)) diff --git a/examples/basic_usage.py b/examples/basic_usage.py deleted file mode 100755 index 3f0d82d..0000000 --- a/examples/basic_usage.py +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env python -# flake8: noqa: F841 -""" -A simple example using requests-cache with httpbin -""" -import time - -from requests_cache import CachedSession - - -def main(): - session = CachedSession('example_cache', backend='sqlite') - - # The real request will only be made once; afterward, the cached response is used - for i in range(5): - response = session.get('http://httpbin.org/get') - - # This is more obvious when calling a slow endpoint - for i in range(5): - response = session.get('http://httpbin.org/delay/2') - - # Caching can be disabled if we want to get a fresh page and not cache it - with session.cache_disabled(): - print(session.get('http://httpbin.org/ip').text) - - # Get some debugging info about the cache - print(session.cache) - print('Cached URLS:', list(session.cache.urls)) - - -if __name__ == "__main__": - t = time.time() - main() - print('Elapsed: %.3f seconds' % (time.time() - t)) diff --git a/examples/generate_test_db.py b/examples/generate_test_db.py index c58fb2a..7f3dd6b 100755 --- a/examples/generate_test_db.py +++ b/examples/generate_test_db.py @@ -47,7 +47,7 @@ def populate_cache(progress, task): # Cache a variety of different response formats, which may result in different behavior urls = [ - ('GET', 'https://httpbin.org/{endpoint}') + ('GET', f'https://httpbin.org/{endpoint}') for endpoint in HTTPBIN_FORMATS + HTTPBIN_EXTRA_ENDPOINTS ] urls += [(method, 'https://httpbin.org/{method.lower()}') for method in HTTPBIN_METHODS] diff --git a/examples/session_patch.py b/examples/session_patch.py deleted file mode 100755 index 204320d..0000000 --- a/examples/session_patch.py +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env python -# flake8: noqa: F841 -""" -The same as ``basic_usage.py``, but using global session patching -""" -import time - -import requests - -import requests_cache - -# After installation, all requests functions and Session methods will be cached -requests_cache.install_cache('example_cache', backend='sqlite') - - -def main(): - # The real request will only be made once; afterward, the cached response is used - for i in range(5): - response = requests.get('http://httpbin.org/get') - - # This is more obvious when calling a slow endpoint - for i in range(5): - response = requests.get('http://httpbin.org/delay/2') - - # Caching can be disabled if we want to get a fresh page and not cache it - with requests_cache.disabled(): - print(requests.get('http://httpbin.org/ip').text) - - # Get some debugging info about the cache - print(requests_cache.get_cache()) - print('Cached URLS:', requests_cache.get_cache().urls) - - # Uninstall to remove caching from all requests functions - requests_cache.uninstall_cache() - - -if __name__ == "__main__": - t = time.time() - main() - print('Elapsed: %.3f seconds' % (time.time() - t)) -- cgit v1.2.1