aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRahiel Kasim <rahielkasim@gmail.com>2017-08-26 15:46:32 +0200
committerRahiel Kasim <rahielkasim@gmail.com>2017-08-26 15:46:32 +0200
commit5aed5bf2d4fdaaaf706069d79452ff9e6f6f1301 (patch)
tree8179df1470cd4d7eb0323ef7e4969b7b1f376fd0
parent5bbc166655622aebdd3427ecdcaa0b17898b1c43 (diff)
type annotations
-rw-r--r--bismillah.py59
-rw-r--r--quran.py40
2 files changed, 57 insertions, 42 deletions
diff --git a/bismillah.py b/bismillah.py
index 8c3bba2..a99d653 100644
--- a/bismillah.py
+++ b/bismillah.py
@@ -1,23 +1,22 @@
-###############################################################################
-# BismillahBot -- Explore the Holy Qur'an on Telegram #
-# Copyright (C) 1436-1438 AH Rahiel Kasim #
-# #
-# This program is free software: you can redistribute it and/or modify #
-# it under the terms of the GNU Affero General Public License as published by #
-# the Free Software Foundation, either version 3 of the License, or #
-# (at your option) any later version. #
-# #
-# This program is distributed in the hope that it will be useful, #
-# but WITHOUT ANY WARRANTY; without even the implied warranty of #
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
-# GNU Affero General Public License for more details. #
-# #
-# You should have received a copy of the GNU Affero General Public License #
-# along with this program. If not, see <http://www.gnu.org/licenses/>. #
-###############################################################################
+# BismillahBot -- Explore the Holy Qur'an on Telegram
+# Copyright (C) 1436-1438 AH Rahiel Kasim
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import sys
from time import sleep, time
+from typing import Tuple
import telegram
import ujson as json
@@ -35,34 +34,34 @@ redis_namespace = ""
update_id = None
-def save_user(chat_id, state):
- """State is a list: [surah:int, ayah:int, type:str]"""
+def save_user(chat_id: int, state: Tuple[int, int, str]):
+ """State is a tuple: (surah, ayah, type)"""
r.set(redis_namespace + str(chat_id),
- json.dumps(state), ex=31536000) # keep state for a year
+ json.dumps(state), ex=60 * 60 * 24 * 31 * 6) # keep state for half a year
-def get_user(chat_id):
+def get_user(chat_id: int):
v = r.get(redis_namespace + str(chat_id))
if v is not None:
return json.loads(v)
-def save_file(filename, file_id):
+def save_file(filename: str, file_id: str):
r.set(redis_namespace + "file:" + filename,
- json.dumps(file_id), ex=8035200) # keep for 3 months
+ json.dumps(file_id), ex=60 * 60 * 24 * 31 * 3) # keep for 3 months
-def get_file(filename):
+def get_file(filename: str):
f = r.get(redis_namespace + "file:" + filename)
if f is not None:
return json.loads(f)
-def get_audio_filename(s, a):
+def get_audio_filename(s: int, a: int) -> str:
return "Husary/" + str(s).zfill(3) + str(a).zfill(3) + ".mp3"
-def get_image_filename(s, a):
+def get_image_filename(s: int, a: int) -> str:
return "quran_images/" + str(s) + "_" + str(a) + ".png"
@@ -96,7 +95,7 @@ def send_file(bot, filename, quran_type, **kwargs):
return upload_from_disk()
-def get_default_query_results(quran):
+def get_default_query_results(quran: Quran):
results = []
ayat = [
(13, 28), (33, 56), (2, 62), (10, 31), (17, 36), (5, 32), (39, 9), (17, 44), (28, 88), (17, 84), (33, 6),
@@ -152,7 +151,7 @@ def main():
def serve(bot, data):
global update_id
- def send_quran(s, a, quran_type, chat_id, reply_markup=None):
+ def send_quran(s: int, a: int, quran_type: str, chat_id: int, reply_markup=None):
if quran_type in ("english", "tafsir"):
text = data[quran_type].get_ayah(s, a)
bot.send_message(chat_id=chat_id, text=text[:MAX_MESSAGE_LENGTH],
@@ -172,7 +171,7 @@ def serve(bot, data):
performer="Shaykh Mahmoud Khalil al-Husary",
title="Quran %d:%d" % (s, a),
reply_markup=reply_markup)
- save_user(chat_id, [s, a, quran_type])
+ save_user(chat_id, (s, a, quran_type))
for update in bot.get_updates(offset=update_id, timeout=10):
update_id = update.update_id + 1
@@ -285,7 +284,7 @@ def serve(bot, data):
sys.stdout.flush()
-def parse_ayah(message):
+def parse_ayah(message: str):
match = re.match("/?(\d+)[ :\-;.,]*(\d*)", message)
if match is not None:
s = int(match.group(1))
diff --git a/quran.py b/quran.py
index 483a0f8..1efb9e5 100644
--- a/quran.py
+++ b/quran.py
@@ -1,15 +1,31 @@
+# BismillahBot -- Explore the Holy Qur'an on Telegram
+# Copyright (C) 1436-1438 AH Rahiel Kasim
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import xml.etree.ElementTree as ET
from random import randint
+from typing import Tuple
-def parse_quran(filename):
+def parse_quran(filename: str):
"""Parse Quran text files (with ayah numbers) from http://tanzil.net."""
quran = []
surah = []
s = 1
- def process_verse(verse):
+ def process_verse(verse: str):
"""Add verse and replace for Arabic ligatures (salawat)"""
return (verse.strip()
.replace("– peace and blessings be upon him", "ﷺ‎"))
@@ -86,12 +102,12 @@ def parse_quran_tafsir():
return quran
-class Quran(object):
+class Quran:
"""Interface to get ayahs from the Quran."""
surah_lengths = [0, 7, 286, 200, 176, 120, 165, 206, 75, 129, 109, 123, 111, 43, 52, 99, 128, 111, 110, 98, 135, 112, 78, 118, 64, 77, 227, 93, 88, 69, 60, 34, 30, 73, 54, 45, 83, 182, 88, 75, 85, 54, 53, 89, 59, 37, 35, 38, 29, 18, 45, 60, 49, 62, 55, 78, 96, 29, 22, 24, 13, 14, 11, 11, 18, 12, 12, 30, 52, 52, 44, 28, 28, 20, 56, 40, 31, 50, 40, 46, 42, 29, 19, 36, 25, 22, 17, 19, 26, 30, 20, 15, 21, 11, 8, 8, 19, 5, 8, 8, 11, 11, 8, 3, 9, 5, 4, 7, 3, 6, 3, 5, 4, 5, 6]
# [0] for normal numbering
- def __init__(self, data):
+ def __init__(self, data: str) -> None:
if data == "arabic":
self.text = parse_quran("quran-uthmani.txt")
elif data == "translation":
@@ -99,26 +115,26 @@ class Quran(object):
elif data == "tafsir":
self.text = parse_quran_tafsir()
- def get_surah(self, surah):
+ def get_surah(self, surah: int) -> str:
"""Get surah by number."""
return self.text[surah - 1]
- def get_ayah(self, surah, ayah):
+ def get_ayah(self, surah: int, ayah: int) -> str:
"""Get verse by surah and ayah numbers."""
return self.text[surah - 1][ayah - 1] + " (%d:%d)" % (surah, ayah)
- def get_ayahs(self, surah, a, b):
- """Get range of Ayahs as tuple """
+ def get_ayahs(self, surah: int, a: int, b: int) -> str:
+ """Get range of Ayahs."""
return " ".join(self.text[surah - 1][a - 1:b]) + " (%d:%d-%d)" % (surah, a, b)
@staticmethod
- def get_random_ayah():
+ def get_random_ayah() -> Tuple[int, int]:
surah = randint(1, 114)
ayah = randint(1, Quran.surah_lengths[surah])
return surah, ayah
@staticmethod
- def get_next_ayah(s, a):
+ def get_next_ayah(s: int, a: int) -> Tuple[int, int]:
length = Quran.surah_lengths[s]
if a == length:
s = s + 1 if s < 114 else 1
@@ -128,7 +144,7 @@ class Quran(object):
return s, a
@staticmethod
- def get_previous_ayah(s, a):
+ def get_previous_ayah(s: int, a: int) -> Tuple[int, int]:
if a == 1:
s = s - 1 if s > 1 else 114
a = Quran.surah_lengths[s]
@@ -137,7 +153,7 @@ class Quran(object):
return s, a
@staticmethod
- def exists(s, a):
+ def exists(s: int, a: int) -> bool:
return 0 < s < 115 and 0 < a <= Quran.surah_lengths[s]