Skip to main content
Code Review

Return to Answer

added 38 characters in body
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

You have a bug: if dt is a Sunday, then it outputs the date range starting from the previous Sunday. That is because datetime.isocalendar() represents the day of week as a number from 1 (= Monday) to 7 (= Sunday), but you want to subtract 0 to 6 days.

>>> print week_string(date(2014, 01, 19))
Jan 12 - 18, 2014

My recommendation:

def week_string(dt):
 # Use underscore to indicate disinterest in the year and week
 _, _, weekday = dt.isocalendar()
 week_0 = dt - timedelta(days=weekday % 7) # Sunday
 week_1 = week_0 + timedelta(days=6) # Saturday

Then, I would write the following six lines more compactly:

 day_0, month_0, year_0 = week_0.strftime('%e-%b-%Y').lstrip().split('-')
 day_1, month_1, year_1 = week_1.strftime('%e-%b-%Y').lstrip().split('-')

The rest of it seems fine.

You have a bug: if dt is a Sunday, then it outputs the date range starting from the previous Sunday. That is because datetime.isocalendar() represents the day of week as a number from 1 (= Monday) to 7 (= Sunday).

>>> print week_string(date(2014, 01, 19))
Jan 12 - 18, 2014

My recommendation:

def week_string(dt):
 # Use underscore to indicate disinterest in the year and week
 _, _, weekday = dt.isocalendar()
 week_0 = dt - timedelta(days=weekday % 7)
 week_1 = week_0 + timedelta(days=6)

Then, I would write the following six lines more compactly:

 day_0, month_0, year_0 = week_0.strftime('%e-%b-%Y').lstrip().split('-')
 day_1, month_1, year_1 = week_1.strftime('%e-%b-%Y').lstrip().split('-')

The rest of it seems fine.

You have a bug: if dt is a Sunday, then it outputs the date range starting from the previous Sunday. That is because datetime.isocalendar() represents the day of week as a number from 1 (= Monday) to 7 (= Sunday), but you want to subtract 0 to 6 days.

>>> print week_string(date(2014, 01, 19))
Jan 12 - 18, 2014

My recommendation:

def week_string(dt):
 # Use underscore to indicate disinterest in the year and week
 _, _, weekday = dt.isocalendar()
 week_0 = dt - timedelta(days=weekday % 7) # Sunday
 week_1 = week_0 + timedelta(days=6) # Saturday

Then, I would write the following six lines more compactly:

 day_0, month_0, year_0 = week_0.strftime('%e-%b-%Y').lstrip().split('-')
 day_1, month_1, year_1 = week_1.strftime('%e-%b-%Y').lstrip().split('-')

The rest of it seems fine.

added link to documentation
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

You have a bug: if dt is a Sunday, then it outputs the date range starting from the previous Sunday. That is because datetime.isocalendar()datetime.isocalendar() represents the day of week as a number from 1 (= Monday) to 7 (= Sunday).

>>> print week_string(date(2014, 01, 19))
Jan 12 - 18, 2014

My recommendation:

def week_string(dt):
 # Use underscore to indicate disinterest in the year and week
 _, _, weekday = dt.isocalendar()
 week_0 = dt - timedelta(days=weekday % 7)
 week_1 = week_0 + timedelta(days=6)

Then, I would write the following six lines more compactly:

 day_0, month_0, year_0 = week_0.strftime('%e-%b-%Y').lstrip().split('-')
 day_1, month_1, year_1 = week_1.strftime('%e-%b-%Y').lstrip().split('-')

The rest of it seems fine.

You have a bug: if dt is a Sunday, then it outputs the date range starting from the previous Sunday. That is because datetime.isocalendar() represents the day of week as a number from 1 (= Monday) to 7 (= Sunday).

>>> print week_string(date(2014, 01, 19))
Jan 12 - 18, 2014

My recommendation:

def week_string(dt):
 # Use underscore to indicate disinterest in the year and week
 _, _, weekday = dt.isocalendar()
 week_0 = dt - timedelta(days=weekday % 7)
 week_1 = week_0 + timedelta(days=6)

Then, I would write the following six lines more compactly:

 day_0, month_0, year_0 = week_0.strftime('%e-%b-%Y').lstrip().split('-')
 day_1, month_1, year_1 = week_1.strftime('%e-%b-%Y').lstrip().split('-')

The rest of it seems fine.

You have a bug: if dt is a Sunday, then it outputs the date range starting from the previous Sunday. That is because datetime.isocalendar() represents the day of week as a number from 1 (= Monday) to 7 (= Sunday).

>>> print week_string(date(2014, 01, 19))
Jan 12 - 18, 2014

My recommendation:

def week_string(dt):
 # Use underscore to indicate disinterest in the year and week
 _, _, weekday = dt.isocalendar()
 week_0 = dt - timedelta(days=weekday % 7)
 week_1 = week_0 + timedelta(days=6)

Then, I would write the following six lines more compactly:

 day_0, month_0, year_0 = week_0.strftime('%e-%b-%Y').lstrip().split('-')
 day_1, month_1, year_1 = week_1.strftime('%e-%b-%Y').lstrip().split('-')

The rest of it seems fine.

Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

You have a bug: if dt is a Sunday, then it outputs the date range starting from the previous Sunday. That is because datetime.isocalendar() represents the day of week as a number from 1 (= Monday) to 7 (= Sunday).

>>> print week_string(date(2014, 01, 19))
Jan 12 - 18, 2014

My recommendation:

def week_string(dt):
 # Use underscore to indicate disinterest in the year and week
 _, _, weekday = dt.isocalendar()
 week_0 = dt - timedelta(days=weekday % 7)
 week_1 = week_0 + timedelta(days=6)

Then, I would write the following six lines more compactly:

 day_0, month_0, year_0 = week_0.strftime('%e-%b-%Y').lstrip().split('-')
 day_1, month_1, year_1 = week_1.strftime('%e-%b-%Y').lstrip().split('-')

The rest of it seems fine.

lang-py

AltStyle によって変換されたページ (->オリジナル) /