Skip to content

Commit

Permalink
optimize translations for increased performance
Browse files Browse the repository at this point in the history
  • Loading branch information
corei8 committed Jun 20, 2024
1 parent 0ad9605 commit 7ec181f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 51 deletions.
39 changes: 25 additions & 14 deletions ordotools/tools/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
from ordotools.tools.helpers import ladys_office
from ordotools.tools.helpers import leap_year

from ordotools.tools.translations import Translations
from ordotools.tools.temporal import Temporal

from ordotools.sanctoral.diocese.roman import Sanctoral

import logging
# import logging


logging.basicConfig(level=logging.DEBUG)
# logging.basicConfig(level=logging.DEBUG)


class LiturgicalCalendar:
Expand All @@ -25,15 +26,9 @@ def __init__(self, year, diocese, language, country=""):
self.year = year
self.diocese = diocese
self.language = language

# TODO: theoretically we could have more than one... ?
# WARN: theoretically we could have more than one transfer
self.transfers = None

self.temporal = Temporal(self.year).return_temporal()
self.FIRST_ADVENT = LiturgicalYearMarks(self.year).first_advent
self.LAST_ADVENT = LiturgicalYearMarks(self.year).last_advent
self.LENT_BEGINS = LiturgicalYearMarks(self.year).lent_begins
self.LENT_ENDS = LiturgicalYearMarks(self.year).lent_ends

def expand_octaves(self, feast: Feast) -> dict:
octave = ()
Expand Down Expand Up @@ -187,6 +182,13 @@ def add_feasts(self, master: tuple, addition: tuple) -> dict:
addition_expanded = {}
for feast in addition:
addition_expanded.update({feast.date: feast})
# we might be able to speed things up here using set()

# def intersections(set_1, set_2):
# the_set_1 = set(set_1)
# the_set_2 = set(set_2)
# return the_set_1.intersection(the_set_2)

for date in master_expanded.keys():
if date in addition_expanded.keys():
feast = self.rank(
Expand Down Expand Up @@ -225,9 +227,13 @@ def our_ladys_saturday(self, calendar: tuple) -> dict:
office = ladys_office # TODO: add this according to the season
for pos, feast in enumerate(year):
if feast.date.strftime("%w") == str(6):
if self.LENT_BEGINS <= feast.date <= self.LENT_ENDS:
first_advent = LiturgicalYearMarks(self.year).first_advent
last_advent = LiturgicalYearMarks(self.year).last_advent
lent_begins = LiturgicalYearMarks(self.year).lent_begins
lent_ends = LiturgicalYearMarks(self.year).lent_ends
if lent_begins <= feast.date <= lent_ends:
continue
elif self.FIRST_ADVENT <= feast.date <= self.LAST_ADVENT:
elif first_advent <= feast.date <= last_advent:
continue
else:
if feast.rank_n > 20:
Expand All @@ -237,10 +243,14 @@ def our_ladys_saturday(self, calendar: tuple) -> dict:
continue
return tuple(year)

def add_translation(self, compiled_cal: dict) -> list:
def add_translation(self, compiled_calendar: Feast) -> list:
year = []
for feast in compiled_cal.values():
feast.lang = self.language
translations = Translations()
for feast in compiled_calendar:
feast.name = translations.translations()[feast.code][self.language]
print(feast.name)
if isinstance(feast.com_1["name"], int):
feast.com_1 = translations.translations()[feast.com_1["name"]][self.language]
year.append(feast)
return year

Expand All @@ -257,5 +267,6 @@ def build(self) -> list:
full_calendar = self.add_feasts(initialized["temporal"], initialized["sanctoral"])
full_calendar = self.our_ladys_saturday(full_calendar)
full_calendar = self.find_octave(year=full_calendar)
full_calendar = self.add_translation(full_calendar)

return full_calendar
49 changes: 20 additions & 29 deletions ordotools/tools/feast.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
from datetime import datetime
# from ordotools.tools.helpers import days
from ordotools.tools.translations import Translations
import functools


class Feast:

def __init__(self, feast_date: datetime, properties: dict, lang="la"):


@functools.cache
def translate(self, code):
translations = Translations()
translated = translations.translations()[code][self.lang]
return translated

self.date = feast_date
self.code = properties["code"]

self.day_in_octave = properties["day_in_octave"] if "day_in_octave" in properties.keys() else 0

translations = Translations()
self._name = translations.translations()[self.code][lang]
if self.day_in_octave != 0:
self.name(translations.octave(lang, self.day_in_octave, self.code))
self._name = ""
# if self.day_in_octave != 0:
# self.name(translations.octave(lang, self.day_in_octave, self.code))

self.feast_properties = properties

Expand Down Expand Up @@ -77,21 +65,24 @@ def translate(self, code):


def format_commemoration(self, commemoration: dict) -> dict:
if len(commemoration) == 0:
return {
"name": None,
}
elif "code" in commemoration.keys():
return {
"name": commemoration["code"],
"data": commemoration # Feast(self.date, commemoration)
}
elif "oration" in commemoration.keys():
return {
"name": commemoration["oration"],
}
else:
if isinstance(commemoration, str):
return commemoration
else:
if len(commemoration) == 0:
return {
"name": None,
}
elif "code" in commemoration.keys():
return {
"name": commemoration["code"],
"data": commemoration # Feast(self.date, commemoration)
}
elif "oration" in commemoration.keys():
return {
"name": commemoration["oration"],
}
else:
return commemoration

@property
def com_1(self):
Expand Down
2 changes: 0 additions & 2 deletions ordotools/tools/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ def epiphany_octave(self) -> dict:
octave_counter += 1
return y

# TODO: Perhaps merge these two?

def post_epiphany(self) -> list:
""" All of the Sundays and ferias after Epiphany """
sunday_after_epiphany = self.epiphany-findsunday(self.epiphany)+week(1)
Expand Down
12 changes: 6 additions & 6 deletions ordotools/tools/translations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ordotools.tools.liturgical_dates import integer_to_roman
from ordotools.tools.liturgical_dates import nth
import functools

# When adding translations, use the ISO 639-1 codes
# that can be found in this article:
#
Expand All @@ -20,7 +21,6 @@

class Translations:

# @functools.cache
def __init__(self):

self.easy_data = {
Expand Down Expand Up @@ -257,7 +257,7 @@ def __init__(self):

4700: {
"la": "S Josephi Sponsi BMV C",
"en": "",
"en": "St. Joseph, Spouse of the BVM, C",
},

4800: {
Expand All @@ -267,12 +267,12 @@ def __init__(self):

4900: {
"la": "S Gabrielis Arch",
"en": "",
"en": "St. Gabriel, Archangel",
},

5000: {
"la": "In Annuntiatione BMV",
"en": "",
"en": "Annuntiation of the BVM",
},

5100: {
Expand Down Expand Up @@ -2195,6 +2195,7 @@ def sacredheart_ferias(self) -> dict:
} for date in range(1,8)
}

# @functools.lru_cache()
def pentecost_sundays(self) -> dict:
# TODO: add the first 4 Sundays after Pentecost (excluding Trinity)
pentecost_season = {}
Expand All @@ -2214,6 +2215,7 @@ def pentecost_sundays(self) -> dict:
}
return pentecost_season

# @functools.lru_cache()
def pentecost_epiphany_sundays(self) -> dict:
# WARN: This is really rough and might not be too efficient, but
# it works for now.
Expand Down Expand Up @@ -2380,8 +2382,6 @@ def epiphany_octave(self) -> dict:
}
return octave

# NOTE: this actually makes things slower... ?
# @functools.cache
def translations(self) -> dict:
return self.data

Expand Down

0 comments on commit 7ec181f

Please sign in to comment.