From cdba0065e7cb7105494573d68b5b58384147a4e6 Mon Sep 17 00:00:00 2001 From: minhkhul Date: Thu, 18 Jul 2024 18:48:09 -0400 Subject: [PATCH 1/6] move _version_check() call outside class definition --- src/client/delphi_epidata.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/client/delphi_epidata.py b/src/client/delphi_epidata.py index 22fd9c1c8..18ad79125 100644 --- a/src/client/delphi_epidata.py +++ b/src/client/delphi_epidata.py @@ -46,6 +46,8 @@ class Epidata: debug = False # if True, prints extra logging statements sandbox = False # if True, will not execute any queries + _version_checked=False + @staticmethod def log(evt, **kwargs): kwargs['event'] = evt @@ -55,8 +57,10 @@ def log(evt, **kwargs): # Check that this client's version matches the most recent available, runs just once per program execution (on initial module load). @staticmethod def _version_check(): + _version_checked = True try: - latest_version = requests.get('https://pypi.org/pypi/delphi-epidata/json').json()['info']['version'] + request = requests.get('https://pypi.org/pypi/delphi-epidata/json', timeout=5) + latest_version = request.json()['info']['version'] if latest_version != __version__: Epidata.log( "Client version not up to date", @@ -708,3 +712,6 @@ async def async_make_calls(param_combos): future = asyncio.ensure_future(async_make_calls(param_list)) responses = loop.run_until_complete(future) return responses + +if Epidata._version_checked == False: + Epidata._version_check() \ No newline at end of file From a22c8cfcfece62759180c4d1d3ef6bc6986964b3 Mon Sep 17 00:00:00 2001 From: george haff Date: Fri, 19 Jul 2024 10:47:37 -0400 Subject: [PATCH 2/6] more py client version check cleanup/fixes --- src/client/delphi_epidata.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/client/delphi_epidata.py b/src/client/delphi_epidata.py index 18ad79125..e7558a16d 100644 --- a/src/client/delphi_epidata.py +++ b/src/client/delphi_epidata.py @@ -46,7 +46,7 @@ class Epidata: debug = False # if True, prints extra logging statements sandbox = False # if True, will not execute any queries - _version_checked=False + _version_checked = False @staticmethod def log(evt, **kwargs): @@ -54,25 +54,30 @@ def log(evt, **kwargs): kwargs['timestamp'] = time.strftime("%Y-%m-%d %H:%M:%S %z") return sys.stderr.write(str(kwargs) + "\n") - # Check that this client's version matches the most recent available, runs just once per program execution (on initial module load). + # Check that this client's version matches the most recent available. + # This is indended to run just once per program execution, on initial module load. + # See the bottom of this file for the ultimate call to this method. @staticmethod def _version_check(): - _version_checked = True + if Epidata._version_checked: + # already done; nothing to do! + return + + Epidata._version_checked = True + try: request = requests.get('https://pypi.org/pypi/delphi-epidata/json', timeout=5) latest_version = request.json()['info']['version'] - if latest_version != __version__: - Epidata.log( - "Client version not up to date", - client_version=__version__, - latest_version=latest_version - ) except Exception as e: Epidata.log("Error getting latest client version", exception=str(e)) + return - # Run this once on module load. Use dunder method for Python <= 3.9 compatibility - # https://stackoverflow.com/a/12718272 - _version_check.__func__() + if latest_version != __version__: + Epidata.log( + "Client version not up to date", + client_version=__version__, + latest_version=latest_version + ) # Helper function to cast values and/or ranges to strings @staticmethod @@ -713,5 +718,6 @@ async def async_make_calls(param_combos): responses = loop.run_until_complete(future) return responses -if Epidata._version_checked == False: - Epidata._version_check() \ No newline at end of file + + +Epidata._version_check() From 21c7df6dd46cd14240fa4fd566fc0fc2629131de Mon Sep 17 00:00:00 2001 From: george haff Date: Fri, 19 Jul 2024 10:55:22 -0400 Subject: [PATCH 3/6] version check test update --- integrations/client/test_delphi_epidata.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/integrations/client/test_delphi_epidata.py b/integrations/client/test_delphi_epidata.py index d788cada2..f4cf773c6 100644 --- a/integrations/client/test_delphi_epidata.py +++ b/integrations/client/test_delphi_epidata.py @@ -317,7 +317,16 @@ def raise_for_status(self): pass def json(self): return json.loads(self.content) get.reset_mock() get.return_value = MockJson(b'{"info": {"version": "0.0.1"}}', 200) + + # "back up" the value of this private class var and replace w/ default + # so the ._version_check() method runs unencumbered: + e_vdc__save = Epidata._version_checked + Epidata._version_checked = False + # run version check: Epidata._version_check() + # "restore" class var: + Epidata._version_checked = e_vdc__save + captured = self.capsys.readouterr() output = captured.err.splitlines() self.assertEqual(len(output), 1) From 51ee2f110a79943af74edcff656e54d817dbee9e Mon Sep 17 00:00:00 2001 From: Dmitry Shemetov Date: Fri, 19 Jul 2024 12:12:12 -0700 Subject: [PATCH 4/6] fix: simplify client version check --- integrations/client/test_delphi_epidata.py | 13 ------------- src/client/delphi_epidata.py | 15 ++++----------- 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/integrations/client/test_delphi_epidata.py b/integrations/client/test_delphi_epidata.py index f4cf773c6..e607781ba 100644 --- a/integrations/client/test_delphi_epidata.py +++ b/integrations/client/test_delphi_epidata.py @@ -318,14 +318,7 @@ def json(self): return json.loads(self.content) get.reset_mock() get.return_value = MockJson(b'{"info": {"version": "0.0.1"}}', 200) - # "back up" the value of this private class var and replace w/ default - # so the ._version_check() method runs unencumbered: - e_vdc__save = Epidata._version_checked - Epidata._version_checked = False - # run version check: Epidata._version_check() - # "restore" class var: - Epidata._version_checked = e_vdc__save captured = self.capsys.readouterr() output = captured.err.splitlines() @@ -333,12 +326,6 @@ def json(self): return json.loads(self.content) self.assertIn("Client version not up to date", output[0]) self.assertIn("\'latest_version\': \'0.0.1\'", output[0]) - @patch('delphi.epidata.client.delphi_epidata.Epidata._version_check') - def test_version_check_once(self, version_check): - """Test that the _version_check() function is only called once on initial module import.""" - from delphi.epidata.client.delphi_epidata import Epidata - version_check.assert_not_called() - def test_geo_value(self): """test different variants of geo types: single, *, multi.""" diff --git a/src/client/delphi_epidata.py b/src/client/delphi_epidata.py index e7558a16d..e76440d9a 100644 --- a/src/client/delphi_epidata.py +++ b/src/client/delphi_epidata.py @@ -46,25 +46,18 @@ class Epidata: debug = False # if True, prints extra logging statements sandbox = False # if True, will not execute any queries - _version_checked = False - @staticmethod def log(evt, **kwargs): kwargs['event'] = evt kwargs['timestamp'] = time.strftime("%Y-%m-%d %H:%M:%S %z") return sys.stderr.write(str(kwargs) + "\n") - # Check that this client's version matches the most recent available. - # This is indended to run just once per program execution, on initial module load. - # See the bottom of this file for the ultimate call to this method. + # Check that this client's version matches the most recent available. This + # is run just once per program execution, on initial module load (see the + # bottom of the file). This is a function of how Python's module system + # works: https://docs.python.org/3/reference/import.html#the-module-cache @staticmethod def _version_check(): - if Epidata._version_checked: - # already done; nothing to do! - return - - Epidata._version_checked = True - try: request = requests.get('https://pypi.org/pypi/delphi-epidata/json', timeout=5) latest_version = request.json()['info']['version'] From 7cb4ac8ccef43bfe851ec22898cdba09d9160ea8 Mon Sep 17 00:00:00 2001 From: george haff Date: Thu, 25 Jul 2024 09:33:25 -0400 Subject: [PATCH 5/6] comments update --- src/client/delphi_epidata.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/client/delphi_epidata.py b/src/client/delphi_epidata.py index e76440d9a..e547fa305 100644 --- a/src/client/delphi_epidata.py +++ b/src/client/delphi_epidata.py @@ -52,10 +52,9 @@ def log(evt, **kwargs): kwargs['timestamp'] = time.strftime("%Y-%m-%d %H:%M:%S %z") return sys.stderr.write(str(kwargs) + "\n") - # Check that this client's version matches the most recent available. This - # is run just once per program execution, on initial module load (see the - # bottom of the file). This is a function of how Python's module system - # works: https://docs.python.org/3/reference/import.html#the-module-cache + # Check that this client's version matches the most recent available. This + # is indended to run just once per program execution, on initial module load. + # See the bottom of this file for the ultimate call to this method. @staticmethod def _version_check(): try: @@ -713,4 +712,7 @@ async def async_make_calls(param_combos): +# This should only run once per program execution, on initial module load, +# as a result of how Python's module system works: +# https://docs.python.org/3/reference/import.html#the-module-cache Epidata._version_check() From ef1d3965a0b1f41f007d264cb7b8b0cf3e336b07 Mon Sep 17 00:00:00 2001 From: george Date: Thu, 25 Jul 2024 09:37:05 -0400 Subject: [PATCH 6/6] comment typo --- src/client/delphi_epidata.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/delphi_epidata.py b/src/client/delphi_epidata.py index e547fa305..ba6ad3805 100644 --- a/src/client/delphi_epidata.py +++ b/src/client/delphi_epidata.py @@ -53,7 +53,7 @@ def log(evt, **kwargs): return sys.stderr.write(str(kwargs) + "\n") # Check that this client's version matches the most recent available. This - # is indended to run just once per program execution, on initial module load. + # is intended to run just once per program execution, on initial module load. # See the bottom of this file for the ultimate call to this method. @staticmethod def _version_check():