Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
calendar Namespace Reference

Data Structures

class  _indexer
 
class  _localized_month
 
class  _localized_day
 

Functions

def firstweekday
 
def setfirstweekday
 
def isleap
 
def leapdays
 
def weekday
 
def monthrange
 
def monthcalendar
 
def prweek
 
def week
 
def weekheader
 
def prmonth
 
def month
 
def format3c
 
def format3cstring
 
def prcal
 
def calendar
 
def timegm
 

Variables

list __all__
 
 error = ValueError
 
int January = 1
 
int February = 2
 
list mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
 
tuple day_name = _localized_day('%A')
 
tuple day_abbr = _localized_day('%a')
 
tuple month_name = _localized_month('%B')
 
tuple month_abbr = _localized_month('%b')
 
int _firstweekday = 0
 
int _colwidth = 7
 
int _spacing = 6
 
int EPOCH = 1970
 

Detailed Description

Calendar printing functions

Note when comparing these calendars to the ones printed by cal(1): By
default, these calendars have Monday as the first day of the week, and
Sunday as the last (the European convention). Use setfirstweekday() to
set the first day of the week (0=Monday, 6=Sunday).

Function Documentation

def calendar.calendar (   year,
  w = 0,
  l = 0,
  c = _spacing 
)
Returns a year's calendar as a multi-line string.

Definition at line 199 of file calendar.py.

References format3cstring(), sre_parse.max, monthcalendar(), string.rstrip(), week(), and weekheader().

200 def calendar(year, w=0, l=0, c=_spacing):
201  """Returns a year's calendar as a multi-line string."""
202  w = max(2, w)
203  l = max(1, l)
204  c = max(2, c)
205  colwidth = (w + 1) * 7 - 1
206  s = _center(`year`, colwidth * 3 + c * 2).rstrip() + '\n' * l
207  header = weekheader(w)
208  header = format3cstring(header, header, header, colwidth, c).rstrip()
209  for q in range(January, January+12, 3):
210  s = (s + '\n' * l +
211  format3cstring(month_name[q], month_name[q+1], month_name[q+2],
212  colwidth, c).rstrip() +
213  '\n' * l + header + '\n' * l)
214  data = []
215  height = 0
216  for amonth in range(q, q + 3):
217  cal = monthcalendar(year, amonth)
218  if len(cal) > height:
219  height = len(cal)
220  data.append(cal)
221  for i in range(height):
222  weeks = []
223  for cal in data:
224  if i >= len(cal):
225  weeks.append('')
226  else:
227  weeks.append(week(cal[i], w))
228  s = s + format3cstring(weeks[0], weeks[1], weeks[2],
229  colwidth, c).rstrip() + '\n' * l
230  return s[:-l] + '\n'
def calendar.firstweekday ( )

Definition at line 81 of file calendar.py.

81 
82 def firstweekday():
83  return _firstweekday
def calendar.format3c (   a,
  b,
  c,
  colwidth = _colwidth,
  spacing = _spacing 
)
Prints 3-column formatting for year calendars

Definition at line 186 of file calendar.py.

References format3cstring().

187 def format3c(a, b, c, colwidth=_colwidth, spacing=_spacing):
188  """Prints 3-column formatting for year calendars"""
189  print format3cstring(a, b, c, colwidth, spacing)
def calendar.format3cstring (   a,
  b,
  c,
  colwidth = _colwidth,
  spacing = _spacing 
)
Returns a string formatted from 3 strings, centered within 3 columns.

Definition at line 190 of file calendar.py.

191 def format3cstring(a, b, c, colwidth=_colwidth, spacing=_spacing):
192  """Returns a string formatted from 3 strings, centered within 3 columns."""
193  return (_center(a, colwidth) + ' ' * spacing + _center(b, colwidth) +
194  ' ' * spacing + _center(c, colwidth))
def calendar.isleap (   year)
Return 1 for leap years, 0 for non-leap years.

Definition at line 92 of file calendar.py.

92 
93 def isleap(year):
94  """Return 1 for leap years, 0 for non-leap years."""
95  return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
def calendar.leapdays (   y1,
  y2 
)
Return number of leap years in range [y1, y2).
   Assume y1 <= y2.

Definition at line 96 of file calendar.py.

96 
97 def leapdays(y1, y2):
98  """Return number of leap years in range [y1, y2).
99  Assume y1 <= y2."""
100  y1 -= 1
101  y2 -= 1
102  return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400)
def calendar.month (   theyear,
  themonth,
  w = 0,
  l = 0 
)
Return a month's calendar string (multi-line).

Definition at line 171 of file calendar.py.

References sre_parse.max, monthcalendar(), string.rstrip(), week(), and weekheader().

172 def month(theyear, themonth, w=0, l=0):
173  """Return a month's calendar string (multi-line)."""
174  w = max(2, w)
175  l = max(1, l)
176  s = (_center(month_name[themonth] + ' ' + `theyear`,
177  7 * (w + 1) - 1).rstrip() +
178  '\n' * l + weekheader(w).rstrip() + '\n' * l)
179  for aweek in monthcalendar(theyear, themonth):
180  s = s + week(aweek, w).rstrip() + '\n' * l
181  return s[:-l] + '\n'
182 
# Spacing of month columns for 3-column year calendar
def calendar.monthcalendar (   year,
  month 
)
Return a matrix representing a month's calendar.
   Each row represents a week; days outside this month are zero.

Definition at line 119 of file calendar.py.

References monthrange().

120 def monthcalendar(year, month):
121  """Return a matrix representing a month's calendar.
122  Each row represents a week; days outside this month are zero."""
123  day1, ndays = monthrange(year, month)
124  rows = []
125  r7 = range(7)
126  day = (_firstweekday - day1 + 6) % 7 - 5 # for leading 0's in first week
127  while day <= ndays:
128  row = [0, 0, 0, 0, 0, 0, 0]
129  for i in r7:
130  if 1 <= day <= ndays: row[i] = day
131  day = day + 1
132  rows.append(row)
133  return rows
def calendar.monthrange (   year,
  month 
)
Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for
   year, month.

Definition at line 110 of file calendar.py.

References isleap(), and weekday().

111 def monthrange(year, month):
112  """Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for
113  year, month."""
114  if not 1 <= month <= 12:
115  raise ValueError, 'bad month number'
116  day1 = weekday(year, month, 1)
117  ndays = mdays[month] + (month == February and isleap(year))
118  return day1, ndays
def calendar.prcal (   year,
  w = 0,
  l = 0,
  c = _spacing 
)
Print a year's calendar.

Definition at line 195 of file calendar.py.

196 def prcal(year, w=0, l=0, c=_spacing):
197  """Print a year's calendar."""
198  print calendar(year, w, l, c),
def calendar.prmonth (   theyear,
  themonth,
  w = 0,
  l = 0 
)
Print a month's calendar.

Definition at line 167 of file calendar.py.

References month().

168 def prmonth(theyear, themonth, w=0, l=0):
169  """Print a month's calendar."""
170  print month(theyear, themonth, w, l),
def calendar.prweek (   theweek,
  width 
)
Print a single week (no newline).

Definition at line 141 of file calendar.py.

References week().

142 def prweek(theweek, width):
143  """Print a single week (no newline)."""
144  print week(theweek, width),
def calendar.setfirstweekday (   weekday)
Set weekday (Monday=0, Sunday=6) to start each week.

Definition at line 84 of file calendar.py.

84 
85 def setfirstweekday(weekday):
86  """Set weekday (Monday=0, Sunday=6) to start each week."""
87  global _firstweekday
88  if not MONDAY <= weekday <= SUNDAY:
89  raise ValueError, \
90  'bad weekday number; must be 0 (Monday) to 6 (Sunday)'
91  _firstweekday = weekday
def calendar.timegm (   tuple)
Unrelated but handy function to calculate Unix timestamp from GMT.

Definition at line 232 of file calendar.py.

References isleap(), and leapdays().

233 def timegm(tuple):
234  """Unrelated but handy function to calculate Unix timestamp from GMT."""
235  year, month, day, hour, minute, second = tuple[:6]
236  assert year >= EPOCH
237  assert 1 <= month <= 12
238  days = 365*(year-EPOCH) + leapdays(EPOCH, year)
239  for i in range(1, month):
240  days = days + mdays[i]
241  if month > 2 and isleap(year):
242  days = days + 1
243  days = days + day - 1
244  hours = days*24 + hour
245  minutes = hours*60 + minute
246  seconds = minutes*60 + second
247  return seconds
def calendar.week (   theweek,
  width 
)
Returns a single week in a string (no newline).

Definition at line 145 of file calendar.py.

References dospath.join().

146 def week(theweek, width):
147  """Returns a single week in a string (no newline)."""
148  days = []
149  for day in theweek:
150  if day == 0:
151  s = ''
152  else:
153  s = '%2i' % day # right-align single-digit days
154  days.append(_center(s, width))
155  return ' '.join(days)
def calendar.weekday (   year,
  month,
  day 
)
Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
   day (1-31).

Definition at line 103 of file calendar.py.

References tzparse.localtime().

104 def weekday(year, month, day):
105  """Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
106  day (1-31)."""
107  secs = mktime((year, month, day, 0, 0, 0, 0, 0, 0))
108  tuple = localtime(secs)
109  return tuple[6]
def calendar.weekheader (   width)
Return a header for a week.

Definition at line 156 of file calendar.py.

References dospath.join().

157 def weekheader(width):
158  """Return a header for a week."""
159  if width >= 9:
160  names = day_name
161  else:
162  names = day_abbr
163  days = []
164  for i in range(_firstweekday, _firstweekday + 7):
165  days.append(_center(names[i%7][:width], width))
166  return ' '.join(days)

Variable Documentation

list __all__
Initial value:
1 = ["error","setfirstweekday","firstweekday","isleap",
2  "leapdays","weekday","monthrange","monthcalendar",
3  "prmonth","month","prcal","calendar","timegm",
4  "month_name", "month_abbr", "day_name", "day_abbr"]

Definition at line 14 of file calendar.py.

int _colwidth = 7

Definition at line 183 of file calendar.py.

int _firstweekday = 0

Definition at line 79 of file calendar.py.

int _spacing = 6

Definition at line 184 of file calendar.py.

tuple day_abbr = _localized_day('%a')

Definition at line 70 of file calendar.py.

tuple day_name = _localized_day('%A')

Definition at line 69 of file calendar.py.

int EPOCH = 1970

Definition at line 231 of file calendar.py.

error = ValueError

Definition at line 20 of file calendar.py.

int February = 2

Definition at line 24 of file calendar.py.

int January = 1

Definition at line 23 of file calendar.py.

list mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

Definition at line 27 of file calendar.py.

tuple month_abbr = _localized_month('%b')

Definition at line 74 of file calendar.py.

tuple month_name = _localized_month('%B')

Definition at line 73 of file calendar.py.