hafez 0.2.7__py3-none-any.whl → 0.2.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hafez/data/hafez.json +1 -10591
- hafez/entry.py +4 -2
- hafez/utils/db.py +28 -38
- hafez/utils/formating.py +1 -16
- {hafez-0.2.7.dist-info → hafez-0.2.9.dist-info}/METADATA +1 -1
- hafez-0.2.9.dist-info/RECORD +12 -0
- {hafez-0.2.7.dist-info → hafez-0.2.9.dist-info}/WHEEL +1 -1
- hafez/data/hafez.db +0 -0
- hafez-0.2.7.dist-info/RECORD +0 -13
- {hafez-0.2.7.dist-info → hafez-0.2.9.dist-info}/licenses/LICENSE +0 -0
- {hafez-0.2.7.dist-info → hafez-0.2.9.dist-info}/top_level.txt +0 -0
hafez/entry.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
import random
|
2
|
-
from typing import
|
2
|
+
from typing import Optional
|
3
3
|
import requests
|
4
4
|
import os
|
5
5
|
import pathlib
|
6
|
-
from hafez.utils.db import get_data, search_data
|
6
|
+
from hafez.utils.db import filter_columns, get_data, search_data
|
7
7
|
from hafez.utils.formating import df_to_dict
|
8
8
|
|
9
9
|
URI_MP3_FOLDER = str(
|
@@ -97,6 +97,7 @@ def get_poem(poem_id) -> dict:
|
|
97
97
|
:return: a dictionary containing the poem verses and its interpretation
|
98
98
|
"""
|
99
99
|
df = get_data(poem_id)
|
100
|
+
df = filter_columns(df)
|
100
101
|
lst_poem = df_to_dict(df)
|
101
102
|
|
102
103
|
return lst_poem[0]
|
@@ -109,6 +110,7 @@ def search(query) -> list:
|
|
109
110
|
:return: a list of dictionary containing all the poems which have the queried terms
|
110
111
|
"""
|
111
112
|
df = search_data(query)
|
113
|
+
df = filter_columns(df)
|
112
114
|
lst_poem = df_to_dict(df)
|
113
115
|
|
114
116
|
return lst_poem
|
hafez/utils/db.py
CHANGED
@@ -1,56 +1,46 @@
|
|
1
1
|
import pandas as pd
|
2
|
-
import sqlite3
|
3
|
-
from sqlite3 import Connection
|
4
2
|
import pathlib
|
5
3
|
import os
|
6
4
|
import json
|
7
5
|
|
8
6
|
URI_DB_FOLDER = str(pathlib.Path(os.path.abspath(os.path.dirname(__file__))).parents[0].resolve())
|
9
|
-
URI_SQLITE_DB = URI_DB_FOLDER + "/data/hafez.db"
|
10
7
|
URI_JSON_DB = URI_DB_FOLDER + "/data/hafez.json"
|
11
8
|
|
12
9
|
|
10
|
+
def filter_columns(df: pd.DataFrame) -> pd.DataFrame:
|
11
|
+
"""
|
12
|
+
This function filters the columns of the DataFrame.
|
13
|
+
:param df: DataFrame
|
14
|
+
:return: DataFrame with selected columns
|
15
|
+
"""
|
16
|
+
columns = ["id", "poem", "interpretation", "alt_interpretation", "mp3"]
|
17
|
+
df = df[columns]
|
18
|
+
return df
|
19
|
+
|
13
20
|
def get_data(id: int = None):
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
else:
|
21
|
-
sql_query = f"SELECT * FROM poems WHERE id = {id}"
|
22
|
-
with open(URI_JSON_DB, "r", encoding="utf8") as json_db:
|
23
|
-
json_data = json.load(json_db)
|
24
|
-
if id in json_data.keys():
|
25
|
-
alt_explanation = json_data[id]["explanation"]
|
26
|
-
|
27
|
-
df = pd.read_sql(sql_query, con=conn)
|
28
|
-
df["alt_interpretation"] = alt_explanation
|
21
|
+
|
22
|
+
_, df = get_connection()
|
23
|
+
|
24
|
+
if id is not None:
|
25
|
+
df = df[df["id"] == id]
|
26
|
+
|
29
27
|
return df
|
30
28
|
|
31
29
|
|
32
30
|
def search_data(query: str = None):
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
31
|
+
|
32
|
+
_, df = get_connection()
|
33
|
+
df['poem_string'] = [','.join(map(str, l)) for l in df['poem']]
|
34
|
+
|
35
|
+
if query is not None and len(query) > 0:
|
37
36
|
lst_query = query.split(" ")
|
38
|
-
|
39
|
-
|
40
|
-
for item in lst_query:
|
41
|
-
if i > 0:
|
42
|
-
sql_query += f" AND"
|
43
|
-
sql_query += f" Poem LIKE '%{item}%'"
|
44
|
-
i += 1
|
45
|
-
|
46
|
-
df = pd.read_sql(sql_query, con=conn)
|
47
|
-
df["alt_interpretation"] = ""
|
37
|
+
df = df[df["poem_string"].str.contains('|'.join(lst_query))]
|
38
|
+
|
48
39
|
return df
|
49
40
|
|
50
41
|
|
51
|
-
def get_connection()
|
52
|
-
"""
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
return db_con
|
42
|
+
def get_connection():
|
43
|
+
with open(URI_JSON_DB, "r", encoding="utf8") as json_db:
|
44
|
+
json_data = json.load(json_db)
|
45
|
+
df = pd.DataFrame(json_data)
|
46
|
+
return json_data, df
|
hafez/utils/formating.py
CHANGED
@@ -3,22 +3,7 @@ def mp3_id_formatting(id: int) -> str:
|
|
3
3
|
|
4
4
|
|
5
5
|
def df_to_dict(df):
|
6
|
-
lst_poems =
|
7
|
-
for index, row in df.iterrows():
|
8
|
-
poem_id = row["id"]
|
9
|
-
str_poem = row["Poem"]
|
10
|
-
lst_verses = str_poem.split("\\r\\n")
|
11
|
-
if "" in lst_verses:
|
12
|
-
lst_verses.remove("")
|
13
|
-
|
14
|
-
str_interpretation = row["Interpretation"]
|
15
|
-
str_alt_interpretation = row["alt_interpretation"]
|
16
|
-
dic_poem = {"id": poem_id,
|
17
|
-
"poem": lst_verses,
|
18
|
-
"interpretation": str_interpretation,
|
19
|
-
"alt_interpretation": str_alt_interpretation,
|
20
|
-
"mp3": f"https://de.loveziba.com/2019/10/{mp3_id_formatting(poem_id)}.mp3"}
|
21
|
-
lst_poems.append(dic_poem)
|
6
|
+
lst_poems = df.to_dict(orient='records')
|
22
7
|
return lst_poems
|
23
8
|
|
24
9
|
|
@@ -0,0 +1,12 @@
|
|
1
|
+
hafez/__init__.py,sha256=e6agkcalSQq5BQAqSxDOqOjEappydKY9T3N0LHLV9SU,257
|
2
|
+
hafez/entry.py,sha256=JoKaNsnhisrrmPkwt6nHY7syaMlDPq4ZvwhRi30jr-E,3754
|
3
|
+
hafez/data/hafez.json,sha256=SZW-vQCAHOcy75MB4pF0Wc0v7Wuaa6_PXccQvcVNuuI,2430767
|
4
|
+
hafez/data/audio/do_not_delete_me.txt,sha256=Gh6YTvONnauMYf7pVXjjXiik5C_FSW-e9orhoxC3Nns,23
|
5
|
+
hafez/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
|
+
hafez/utils/db.py,sha256=peU1B0rSc1IUEhy802-MkGNN_9mQwKucBiVNI2WRFOM,1178
|
7
|
+
hafez/utils/formating.py,sha256=SlGtfVb30Q0Yg0qysgdoFcN4f-TbuxsxaN-aRE0HDSM,493
|
8
|
+
hafez-0.2.9.dist-info/licenses/LICENSE,sha256=TSObqi3glpU9S9P6kSv8d488pt0Cc3xDgoUF6yHlx3g,1099
|
9
|
+
hafez-0.2.9.dist-info/METADATA,sha256=wVWbH_DUGWhn0Y2TqGYhgHQAoDaDY8HEjl409KBWvFw,4012
|
10
|
+
hafez-0.2.9.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
|
11
|
+
hafez-0.2.9.dist-info/top_level.txt,sha256=zl_xBZxOuP1dJA41E3Cnxb-LZrnLFhpu7JCz3UW5uDs,6
|
12
|
+
hafez-0.2.9.dist-info/RECORD,,
|
hafez/data/hafez.db
DELETED
Binary file
|
hafez-0.2.7.dist-info/RECORD
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
hafez/__init__.py,sha256=e6agkcalSQq5BQAqSxDOqOjEappydKY9T3N0LHLV9SU,257
|
2
|
-
hafez/entry.py,sha256=aU6nOYKbZUFq5HeYrLEFrc7MEZuW_YqKxk9QBMI6Yzg,3687
|
3
|
-
hafez/data/hafez.db,sha256=qwUucdZ6kAi74IK76h8szl_3VUYi13bj8q8MWAIN1CQ,954368
|
4
|
-
hafez/data/hafez.json,sha256=HvKVYI86wtWGRP9G0CLI5sE5ExoBgRHB78n6d8Prl_U,797858
|
5
|
-
hafez/data/audio/do_not_delete_me.txt,sha256=Gh6YTvONnauMYf7pVXjjXiik5C_FSW-e9orhoxC3Nns,23
|
6
|
-
hafez/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
7
|
-
hafez/utils/db.py,sha256=e0LG7keJwT_uEMkV8mkdQ4cnbopAqhRS3y-VUHwFIyQ,1766
|
8
|
-
hafez/utils/formating.py,sha256=-E7L-w-6xSDPSqK4ZLKMOAfVzRoGRjilLclOwJHphUg,1128
|
9
|
-
hafez-0.2.7.dist-info/licenses/LICENSE,sha256=TSObqi3glpU9S9P6kSv8d488pt0Cc3xDgoUF6yHlx3g,1099
|
10
|
-
hafez-0.2.7.dist-info/METADATA,sha256=FLWEdX3A2Hd-uFEpqreDEKWYDcqIrVfGxBTUrbGpogM,4012
|
11
|
-
hafez-0.2.7.dist-info/WHEEL,sha256=1tXe9gY0PYatrMPMDd6jXqjfpz_B-Wqm32CPfRC58XU,91
|
12
|
-
hafez-0.2.7.dist-info/top_level.txt,sha256=zl_xBZxOuP1dJA41E3Cnxb-LZrnLFhpu7JCz3UW5uDs,6
|
13
|
-
hafez-0.2.7.dist-info/RECORD,,
|
File without changes
|
File without changes
|