From 9b8e34179f2dc17cecb290f8573ad3b57e7178c5 Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 6 Apr 2018 00:39:02 +0200 Subject: Coalescing nullable DTO properties --- jfr_playoff/dto.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'jfr_playoff') diff --git a/jfr_playoff/dto.py b/jfr_playoff/dto.py index a1b3a7f..a88cd2b 100644 --- a/jfr_playoff/dto.py +++ b/jfr_playoff/dto.py @@ -1,11 +1,18 @@ import sys +def coalesce(*arg): + for el in arg: + if el is not None: + return el + return None + + class Team(object): name = '' score = 0.0 def __unicode__(self): - return u'%s (%.1f)' % (self.name, self.score) + return u'%s (%.1f)' % (coalesce(self.name, ''), self.score) def __repr__(self): return unicode(self).encode(sys.stdin.encoding) @@ -23,7 +30,7 @@ class Match(object): def __repr__(self): return (u'#%d (%s) %s [%s]' % ( - self.id, self.link, [unicode(team) for team in self.teams], + self.id, coalesce(self.link, ''), [unicode(team) for team in self.teams], u'finished' if self.running < 0 else ( u'%d boards' % self.running)) ).encode(sys.stdin.encoding) @@ -37,7 +44,7 @@ class Phase(object): def __repr__(self): return u'%s (%s) <%d matches> [%srunning]' % ( - self.title, self.link, + self.title, coalesce(self.link, ''), len(self.matches), '' if self.running else 'not ') __all__ = ('Team', 'Match', 'Phase') -- cgit v1.2.3