From 3b2bafdf12f8e18d8565e094c506152952891f48 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Tue, 27 Mar 2012 12:08:33 +0200 Subject: [PATCH] Centralize "empty" comparisons And always use "is" instead of "==". --- weboob/applications/videoob/videoob.py | 6 ++--- weboob/capabilities/base.py | 24 +++++++++++++------- weboob/tools/application/formatters/table.py | 7 +++--- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/weboob/applications/videoob/videoob.py b/weboob/applications/videoob/videoob.py index 6e865a11..75f9fe10 100644 --- a/weboob/applications/videoob/videoob.py +++ b/weboob/applications/videoob/videoob.py @@ -24,7 +24,7 @@ import sys import os from weboob.capabilities.video import ICapVideo, BaseVideo -from weboob.capabilities.base import NotLoaded, NotAvailable +from weboob.capabilities.base import empty from weboob.tools.application.repl import ReplApplication from weboob.tools.application.media_player import InvalidMediaPlayer, MediaPlayer, MediaPlayerNotFound from weboob.tools.application.formatters.iformatter import IFormatter @@ -49,9 +49,9 @@ class VideoListFormatter(IFormatter): else: result = u'%s* (%s) %s%s\n' % (self.BOLD, item['id'], item['title'], self.NC) result += ' %s' % (item['duration'] if item['duration'] else item['date']) - if item['author'] not in (NotLoaded, NotAvailable, None): + if not empty(item['author']): result += ' - %s' % item['author'] - if item['rating'] not in (NotLoaded, NotAvailable, None): + if not empty(item['rating']): result += u' (%s/%s)' % (item['rating'], item['rating_max']) return result diff --git a/weboob/capabilities/base.py b/weboob/capabilities/base.py index feb650cb..869642fe 100644 --- a/weboob/capabilities/base.py +++ b/weboob/capabilities/base.py @@ -28,7 +28,19 @@ from weboob.tools.ordereddict import OrderedDict __all__ = ['FieldNotFound', 'NotAvailable', 'NotLoaded', 'IBaseCap', 'Field', 'IntField', 'FloatField', 'StringField', 'BytesField', - 'DateField', 'DeltaField', 'CapBaseObject'] + 'DateField', 'DeltaField', 'CapBaseObject', 'empty'] + + +def empty(value): + """ + Checks if a value is empty (None, NotLoaded or NotAvailable). + + :rtype: :class:`bool` + """ + for cls in (None, NotLoaded, NotAvailable): + if value is cls: + return True + return False class FieldNotFound(Exception): @@ -278,8 +290,7 @@ class CapBaseObject(object): :param excepts: if specified, do not change fields listed """ for key, old_value in self.iter_fields(): - if old_value in (None, NotLoaded, NotAvailable) and \ - not key in excepts: + if empty(old_value) and key not in excepts: setattr(self, key, value) def iter_fields(self): @@ -314,7 +325,7 @@ class CapBaseObject(object): except KeyError: object.__setattr__(self, name, value) else: - if value not in (NotLoaded, NotAvailable, None): + if not empty(value): try: # Try to convert value to the wanted one. nvalue = attr.convert(value) @@ -329,10 +340,7 @@ class CapBaseObject(object): # raise ValueError. pass - if not isinstance(value, attr.types) and \ - value is not NotLoaded and \ - value is not NotAvailable and \ - value is not None: + if not isinstance(value, attr.types) and not empty(value): raise ValueError( 'Value for "%s" needs to be of type %r, not %r' % ( name, attr.types, type(value))) diff --git a/weboob/tools/application/formatters/table.py b/weboob/tools/application/formatters/table.py index 86d59844..5baf5ceb 100644 --- a/weboob/tools/application/formatters/table.py +++ b/weboob/tools/application/formatters/table.py @@ -20,7 +20,7 @@ from prettytable import PrettyTable -from weboob.capabilities.base import NotLoaded, NotAvailable +from weboob.capabilities.base import empty from .iformatter import IFormatter @@ -52,7 +52,7 @@ class TableFormatter(IFormatter): for i in xrange(len(self.keys)): available = False for line in self.queue: - if line[i] is not NotLoaded and line[i] is not NotAvailable and line[i] is not None: + if not empty(line[i]): available = True break if available: @@ -63,7 +63,7 @@ class TableFormatter(IFormatter): s = '' if self.display_header and self.header: if self.HTML: - s+= '

%s

' % self.header + s += '

%s

' % self.header else: s += self.header s += "\n" @@ -92,5 +92,6 @@ class TableFormatter(IFormatter): def set_header(self, string): self.header = string + class HTMLTableFormatter(TableFormatter): HTML = True