Python Forum
Printing Easter date occurrences - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Printing Easter date occurrences (/thread-16562.html)



Printing Easter date occurrences - samsonite - Mar-05-2019

Hi all.
The following code computes the Easter dates in a given interval. My goal is to print out only the ones with a given string, i.e. 04-21, like the actual year Easter date. It should be found (1957,2019,2030,2041).
Thanks in advance.

# ---------- easter.py ---------------
from datetime import date

def calc_easter(self, year):
    "Returns Easter as a date object."
    a = year % 19
    b = year // 100
    c = year % 100
    d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
    e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
    f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
    month = f // 31
    day = f % 31 + 1    
    return date(year, month, day)

start_interval= 1950
end_interval= 2050

r = range(start_interval, end_interval+1)
for n in r:
	print (calc_easter(date, n))
# ------------------------------
Output:
1950-04-09 1951-03-25 1952-04-13 1953-04-05 1954-04-18 1955-04-10 1956-04-01 1957-04-21 1958-04-06 1959-03-29 1960-04-17 1961-04-02 1962-04-22 1963-04-14 1964-03-29 1965-04-18 1966-04-10 1967-03-26 1968-04-14 1969-04-06 1970-03-29 1971-04-11 1972-04-02 1973-04-22 1974-04-14 1975-03-30 1976-04-18 1977-04-10 1978-03-26 1979-04-15 1980-04-06 1981-04-19 1982-04-11 1983-04-03 1984-04-22 1985-04-07 1986-03-30 1987-04-19 1988-04-03 1989-03-26 1990-04-15 1991-03-31 1992-04-19 1993-04-11 1994-04-03 1995-04-16 1996-04-07 1997-03-30 1998-04-12 1999-04-04 2000-04-23 2001-04-15 2002-03-31 2003-04-20 2004-04-11 2005-03-27 2006-04-16 2007-04-08 2008-03-23 2009-04-12 2010-04-04 2011-04-24 2012-04-08 2013-03-31 2014-04-20 2015-04-05 2016-03-27 2017-04-16 2018-04-01 2019-04-21 2020-04-12 2021-04-04 2022-04-17 2023-04-09 2024-03-31 2025-04-20 2026-04-05 2027-03-28 2028-04-16 2029-04-01 2030-04-21 2031-04-13 2032-03-28 2033-04-17 2034-04-09 2035-03-25 2036-04-13 2037-04-05 2038-04-25 2039-04-10 2040-04-01 2041-04-21 2042-04-06 2043-03-29 2044-04-17 2045-04-09 2046-03-25 2047-04-14 2048-04-05 2049-04-18 2050-04-10



RE: Printing Easter date occurrences - samsonite - Mar-05-2019

Failed tentative, as follows:
# ---------- ea.py ---------------
from datetime import date

def calc_easter(self, year):
    "Returns Easter as a date object."
    a = year % 19
    b = year // 100
    c = year % 100
    d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
    e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
    f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
    month = f // 31
    day = f % 31 + 1    
    return date(year, month, day)

start_interval= 1950
end_interval= 2050

r = range(start_interval, end_interval+1)
for n in r:
	s=calc_easter(date, n)
	c=s[5:]
#	if c == '04-21'
#		print (c)
# ------------------------------
Arising error
Error:
C:\Training>python ea.py Traceback (most recent call last): File "ea.py", line 22, in <module> c=s[5:] TypeError: 'datetime.date' object is not subscriptable
Help required. Thx


RE: Printing Easter date occurrences - woooee - Mar-05-2019

Easter is a lunar holiday, i.e. the first Sunday, after the first full moon, after the Spring Equinox. I do not see you calculating any of these, although it is next to impossible to tell what variable names a, b, c, etc. are supposed to contain.


RE: Printing Easter date occurrences - samsonite - Mar-05-2019

(Mar-05-2019, 05:38 PM)woooee Wrote: Easter is a lunar holiday, i.e. the first Sunday, after the first full moon, after the Spring Equinox. I do not see you calculating any of these, although it is next to impossible to tell what variable names a, b, c, etc. are supposed to contain.
You are fully wrong, such an algorithm was invented by Friedrick Gauss.


RE: Printing Easter date occurrences - samsonite - Mar-06-2019

My last trial! Please, may I have help? Thank you
# ---------- ea2.py ---------------
# https://docs.python.org/2/library/datetime.html
from datetime import date

def calc_easter(self, year):
    "Returns Easter as a date object."
    a = year % 19
    b = year // 100
    c = year % 100
    d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
    e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
    f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
    month = f // 31
    day = f % 31 + 1    
    return date(year, month, day)

start_interval= 1950
end_interval= 1960

r = range(start_interval, end_interval+1)
for n in r:
	s=calc_easter(date, n)
	print(date.month)  # my intention is to extract the month number (3 or 4),
	                   # same story for the day
                       # and then if condition will solve the matter
# -------- OUTPUT ----------
#     Where is my fault?
# -------------------------- 
#C:\Training>python ea2.py
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
# ---------------------------------------------



RE: Printing Easter date occurrences - buran - Mar-06-2019

1. you don't need self in calc_easter(). It's not a class attribute. Thus you don't need to pass date as first argument when call it.
2. when you pass date you pass just generic datetime.date object (i.e. not an instance) and when you print date.month it's just the attribute month of that date object.

from datetime import date
 
def calc_easter(year):
    "Returns Easter as a date object."
    a = year % 19
    b = year // 100
    c = year % 100
    d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
    e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
    f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
    month = f // 31
    day = f % 31 + 1    
    return date(year, month, day)
 
start_interval = 1950
end_interval = 2042
for year in range(start_interval, end_interval):
    easter = calc_easter(year=year)
    if easter.month == 4 and easter.day == 21:
        print(easter.strftime('%Y-%m-%d'))



RE: Printing Easter date occurrences - samsonite - Mar-06-2019

Thank you very much, buran. Problem solved, according to your precious drifts. The core of solution goes around this command easter = calc_easter(year=year) along with the erasing of (self) attribute. Good, now. Final code, better detailed in comments, is below, and best wishes
# ---------- easter_final.py ----------
from datetime import date
  
def calc_easter(year):
    "Returns Easter as a date object."
    a = year % 19
    b = year // 100
    c = year % 100
    d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
    e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
    f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
    month = f // 31
    day = f % 31 + 1    
    return date(year, month, day)
    
# ----- Years interval ---------  
start_interval = 1950
end_interval = 2050
# ----- Wished month/day occurrence ---------
m_target= 4    # 3 or 4
d_target= 21   # 1 to 31
#
for year in range(start_interval, end_interval):
    easter = calc_easter(year=year)
    if easter.month == m_target and easter.day == d_target:
        print(easter.strftime('%Y-%m-%d'))
#
# --------- OUTPUT ------------
# C:\Training>python easter_final.py
# 1957-04-21
# 2019-04-21
# 2030-04-21
# 2041-04-21
# ------------------------------



RE: Printing Easter date occurrences - buran - Mar-06-2019

I used easter.month and easter.day in order to be as close as possible to your attempt (based on attempted printing date.month). But you can do

if easter == date(year, m_target, d_target):
    print(easter.strftime('%Y-%m-%d'))



RE: Printing Easter date occurrences - samsonite - Mar-06-2019

Superb, buran! That date(year, m_target, d_target) is what I was looking for at the beginning of my investigations. Thanks again, and cheers

# ---------- pasqua.py ----------
from datetime import date
   
def calc_easter(year):
    "Returns Easter as a date object."
    a = year % 19
    b = year // 100
    c = year % 100
    d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
    e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
    f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
    month = f // 31
    day = f % 31 + 1    
    return date(year, month, day)
     
# ----- Years interval ---------  
start_interval = 1950
end_interval = 2050
# ----- Wished month/day occurrence ---------
m_target= 4    # 3 or 4
d_target= 21   # 1 to 31
#
for year in range(start_interval, end_interval):
	easter = calc_easter(year=year)
	if easter == date(year, m_target, d_target):	
		print(easter.strftime('%Y-%m-%d'))
#
# --------- OUTPUT ------------
# C:\Training>python pasqua.py
# 1957-04-21
# 2019-04-21
# 2030-04-21
# 2041-04-21
# ------------------------------