1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 04:02:32 +01:00

extend and rename util.CustomNone

This commit is contained in:
Mike Fährmann 2022-12-06 18:44:46 +01:00
parent 6afb3cc766
commit 43c211f1a7
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
2 changed files with 32 additions and 3 deletions

View File

@ -528,8 +528,8 @@ def parse_inputfile(file, log):
yield line
class UniversalNone():
"""None-style object that supports more operations than None itself"""
class CustomNone():
"""None-style type that supports more operations than regular None"""
__slots__ = ()
def __getattribute__(self, _):
@ -538,10 +538,28 @@ class UniversalNone():
def __getitem__(self, _):
return self
def __iter__(self):
return self
def __call__(self, *args, **kwargs):
return self
@staticmethod
def __next__():
raise StopIteration
@staticmethod
def __bool__():
return False
@staticmethod
def __len__():
return 0
@staticmethod
def __format__(_):
return "None"
@staticmethod
def __str__():
return "None"
@ -549,7 +567,7 @@ class UniversalNone():
__repr__ = __str__
NONE = UniversalNone()
NONE = CustomNone()
EPOCH = datetime.datetime(1970, 1, 1)
SECOND = datetime.timedelta(0, 1)
WINDOWS = (os.name == "nt")

View File

@ -618,10 +618,21 @@ class TestOther(unittest.TestCase):
obj = util.NONE
self.assertFalse(obj)
self.assertEqual(len(obj), 0)
self.assertEqual(str(obj), str(None))
self.assertEqual(repr(obj), repr(None))
self.assertEqual(format(obj), str(None))
self.assertEqual(format(obj, "%F"), str(None))
self.assertIs(obj.attr, obj)
self.assertIs(obj["key"], obj)
self.assertIs(obj(), obj)
self.assertIs(obj(1, "a"), obj)
self.assertIs(obj(foo="bar"), obj)
i = 0
for _ in obj:
i += 1
self.assertEqual(i, 0)
class TestExtractor():