From 1047f36c6ebf04074d4fbe7e8eee7b4049fa5821 Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Sat, 28 Sep 2019 14:19:55 -0400 Subject: [PATCH] Apprise and AppriseConfig truth value support added (#155) --- apprise/Apprise.py | 14 ++++++++++++++ apprise/AppriseConfig.py | 14 ++++++++++++++ test/test_api.py | 8 ++++++++ test/test_apprise_config.py | 8 ++++++++ 4 files changed, 44 insertions(+) diff --git a/apprise/Apprise.py b/apprise/Apprise.py index ee199c4b..0bddfc49 100644 --- a/apprise/Apprise.py +++ b/apprise/Apprise.py @@ -519,6 +519,20 @@ class Apprise(object): # If we reach here, then we indexed out of range raise IndexError('list index out of range') + def __bool__(self): + """ + Allows the Apprise object to be wrapped in an Python 3.x based 'if + statement'. True is returned if at least one service has been loaded. + """ + return len(self) > 0 + + def __nonzero__(self): + """ + Allows the Apprise object to be wrapped in an Python 2.x based 'if + statement'. True is returned if at least one service has been loaded. + """ + return len(self) > 0 + def __iter__(self): """ Returns an iterator to each of our servers loaded. This includes those diff --git a/apprise/AppriseConfig.py b/apprise/AppriseConfig.py index a07ef4b4..808a02f3 100644 --- a/apprise/AppriseConfig.py +++ b/apprise/AppriseConfig.py @@ -276,6 +276,20 @@ class AppriseConfig(object): """ return self.configs[index] + def __bool__(self): + """ + Allows the Apprise object to be wrapped in an Python 3.x based 'if + statement'. True is returned if at least one service has been loaded. + """ + return True if self.configs else False + + def __nonzero__(self): + """ + Allows the Apprise object to be wrapped in an Python 2.x based 'if + statement'. True is returned if at least one service has been loaded. + """ + return True if self.configs else False + def __iter__(self): """ Returns an iterator to our config list diff --git a/test/test_api.py b/test/test_api.py index d156a0fd..a88df9f8 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -68,6 +68,10 @@ def test_apprise(): # no items assert(len(a) == 0) + # Apprise object can also be directly tested with 'if' keyword + # No entries results in a False response + assert(not a) + # Create an Asset object asset = AppriseAsset(theme='default') @@ -85,6 +89,10 @@ def test_apprise(): # 2 servers loaded assert(len(a) == 2) + # Apprise object can also be directly tested with 'if' keyword + # At least one entry results in a True response + assert(a) + # We can retrieve our URLs this way: assert(len(a.urls()) == 2) diff --git a/test/test_apprise_config.py b/test/test_apprise_config.py index d3fa9d35..e5b6596e 100644 --- a/test/test_apprise_config.py +++ b/test/test_apprise_config.py @@ -55,6 +55,10 @@ def test_apprise_config(tmpdir): # There are no servers loaded assert len(ac) == 0 + # Object can be directly checked as a boolean; response is False + # when there are no entries loaded + assert not ac + # lets try anyway assert len(ac.servers()) == 0 @@ -83,6 +87,10 @@ def test_apprise_config(tmpdir): # One configuration file should have been found assert len(ac) == 1 + # Object can be directly checked as a boolean; response is True + # when there is at least one entry + assert ac + # We should be able to read our 3 servers from that assert len(ac.servers()) == 3