Apprise and AppriseConfig truth value support added (#155)

This commit is contained in:
Chris Caron 2019-09-28 14:19:55 -04:00 committed by GitHub
parent 011fbc9b5f
commit 1047f36c6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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