From 9ecf9ce161bf3c1eafebe3c7f6d06b4f2458ff4e Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 22 Oct 2002 05:15:17 +0000 Subject: Patches #626105: Replaces the _center function in the calendar module with the center method for strings. For situations with uneven padding, the behavior is slightly different in that the center method puts the extra space on the right instead of the left. --- Lib/calendar.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/Lib/calendar.py b/Lib/calendar.py index d172b24..3f9b7ba 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -122,13 +122,6 @@ def monthcalendar(year, month): rows.append(row) return rows -def _center(str, width): - """Center a string in a field.""" - n = width - len(str) - if n <= 0: - return str - return ' '*((n+1)/2) + str + ' '*((n)/2) - def prweek(theweek, width): """Print a single week (no newline).""" print week(theweek, width), @@ -141,7 +134,7 @@ def week(theweek, width): s = '' else: s = '%2i' % day # right-align single-digit days - days.append(_center(s, width)) + days.append(s.center(width)) return ' '.join(days) def weekheader(width): @@ -152,7 +145,7 @@ def weekheader(width): names = day_abbr days = [] for i in range(_firstweekday, _firstweekday + 7): - days.append(_center(names[i%7][:width], width)) + days.append(names[i%7][:width].center(width)) return ' '.join(days) def prmonth(theyear, themonth, w=0, l=0): @@ -163,7 +156,7 @@ def month(theyear, themonth, w=0, l=0): """Return a month's calendar string (multi-line).""" w = max(2, w) l = max(1, l) - s = (_center(month_name[themonth] + ' ' + `theyear`, + s = ((month_name[themonth] + ' ' + `theyear`).center( 7 * (w + 1) - 1).rstrip() + '\n' * l + weekheader(w).rstrip() + '\n' * l) for aweek in monthcalendar(theyear, themonth): @@ -180,8 +173,8 @@ def format3c(a, b, c, colwidth=_colwidth, spacing=_spacing): def format3cstring(a, b, c, colwidth=_colwidth, spacing=_spacing): """Returns a string formatted from 3 strings, centered within 3 columns.""" - return (_center(a, colwidth) + ' ' * spacing + _center(b, colwidth) + - ' ' * spacing + _center(c, colwidth)) + return (a.center(colwidth) + ' ' * spacing + b.center(colwidth) + + ' ' * spacing + c.center(colwidth)) def prcal(year, w=0, l=0, c=_spacing): """Print a year's calendar.""" @@ -193,7 +186,7 @@ def calendar(year, w=0, l=0, c=_spacing): l = max(1, l) c = max(2, c) colwidth = (w + 1) * 7 - 1 - s = _center(`year`, colwidth * 3 + c * 2).rstrip() + '\n' * l + s = `year`.center(colwidth * 3 + c * 2).rstrip() + '\n' * l header = weekheader(w) header = format3cstring(header, header, header, colwidth, c).rstrip() for q in range(January, January+12, 3): -- cgit v0.12