-1

Which implementation of current_quarter function would be more pythonic, and why?

First version seems to be easier to extend. However, it's a kind of function you would not extend because the notion of a quarter wouldn't change.

In the second version it is easier to grasp the mechanics.

def current_quarter(input_date):
    months_baskets = [(1,2,3), (4,5,6), (7,8,9), (10,11,12)]
    quarters = (1,2,3,4)
    for idx, basket in enumerate(months_baskets):
        if input_date.month in basket:
            return quarters[idx]
            break

def current_quarter(input_date):
    if input_date.month in (1, 2, 3):
        return 1
    elif input_date.month in (4, 5, 6):
        return 2
    elif input_date.month in (7, 8, 9):
        return 3
    elif input_date.month in (10, 11, 12):
        return 4
BP_
  • 2,497
  • 5
  • 18
  • 18

2 Answers2

3

Neither is; it is much simpler to compute the quarter mathematically:

def current_quarter(input_date):
    return (input_date.month - 1) // 3 + 1

Quick demo:

>>> for month in range(1, 13):
...     quarter = (month - 1) // 3 + 1
...     print 'Month: {0:02d}, Quarter: {1:d}'.format(month, quarter)
... 
Month: 01, Quarter: 1
Month: 02, Quarter: 1
Month: 03, Quarter: 1
Month: 04, Quarter: 2
Month: 05, Quarter: 2
Month: 06, Quarter: 2
Month: 07, Quarter: 3
Month: 08, Quarter: 3
Month: 09, Quarter: 3
Month: 10, Quarter: 4
Month: 11, Quarter: 4
Month: 12, Quarter: 4
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

I am not sure if I got what Martjin is trying to propose, so I would take this liberty to propose my solution

>>> def current_quarter(input_date):
        return (input_date.month - 1) // 3 + 1
>>>
>>> from datetime import datetime
>>> [(m, current_quarter(datetime(2014, m, 1))) for m in range(1,13)]
[(1, 1), (2, 1), (3, 1), (4, 2), (5, 2), (6, 2), (7, 3), (8, 3), (9, 3), (10, 4), (11, 4), (12, 4)]
Lukas Graf
  • 30,317
  • 8
  • 77
  • 92
Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • 1
    `input_date` is a date object, not an integer. – Martijn Pieters Oct 25 '14 at 21:41
  • 1
    Ok, I'm confused here - this is basically @MartijnPieter's answer copied over, and "demonstrated" by code that doesn't match what the function expects? – Lukas Graf Oct 25 '14 at 21:54
  • @LukasGraf: Not exactly. Check his edit :-). I just realized he edited his question and it matches mine. But its now too late as this is an accepted answer – Abhijit Oct 25 '14 at 21:56
  • I see. The demo code is still wrong though (and the indentation of the function body). – Lukas Graf Oct 25 '14 at 21:59
  • @LukasGraf: Code copied from Idle, and I believe people understand's that. Regarding the example code, I was bit lazy, but now its fixed. – Abhijit Oct 25 '14 at 22:01