Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions sources/alt_chapter_talmud_rename/alt_toc_repopulate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import django
import csv
import time

django.setup()

from sefaria.model import *

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you purposely put this import below the django.setup()? PEP8 recommends having all the imports at the top

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if I recall correctly, django.setup() is necessary before importing the model, although it lends itself to a clunky import section at the top of the file.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8 recommends not to import everything from a package (... import *) and only import the things you need, even when it's a long list - due to potential namespace pollution and reduced code readability

Copy link
Contributor Author

@saengel saengel Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, except for sefaria.model I was told this is best practice.

See here



def retrieve_new_chapter_names():
data = {}
with open("perek_names - final.csv", newline="", encoding="utf-8") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
key = f"{row['Masekhet']} {row['#']}"
data[key] = row['English']
return data

def retrieve_babylonian_talmud_masechtot():
return library.get_indexes_in_corpus('Bavli')

def name_changer(masechet_index, new_names_data):
"""
This function iterates through the alt structure nodes for a given Masechet and renames
the title, then saves the Index.
:param masechet_index: The Index object for the given Masechet
:param new_names_data: The data dict for the new names, where the key is the masechet name and chapter number
and the value is the updated name for that chapter.
:return: None
"""
chap_num = 0
for node in masechet_index.alt_structs['Chapters']['nodes']:
chap_num += 1
new_title = new_names_data[f"{masechet} {chap_num}"]
print(f">> Updating {masechet} {chap_num} to {new_title}")
chapter_title_list = node["titles"]
for title in chapter_title_list:
if title['lang'] == 'en':
title['text'] = new_title
masechet_index.save()


if __name__ == '__main__':

start = time.time()

# Ingest CSV
new_names_data = retrieve_new_chapter_names()

# Retrieve all masechtot
masechtot = retrieve_babylonian_talmud_masechtot()

# Run the name-changer on a masechet-by-masechet basis
for masechet in masechtot:
masechet_index = Ref(masechet).index
print(masechet_index)
name_changer(masechet_index, new_names_data)

end = time.time()

print(f"Total run time {end - start}")

# In this case, the Hebrew title name for Bava Kamma 2 also needed to be updated correcting a typo.
bk_index = Ref("Bava Kamma 2").index
bk_index.alt_structs["Chapters"]["nodes"][1]["titles"][1]["text"] = "כיצד הרגל"
bk_index.save()

Loading