hafez 0.2.6__py3-none-any.whl → 0.2.8__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/entry.py CHANGED
@@ -1,9 +1,9 @@
1
1
  import random
2
- from typing import Tuple, Optional
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
- id = str(id)
15
- conn = get_connection()
16
- alt_explanation = ""
17
-
18
- if id is None:
19
- sql_query = "SELECT * FROM poems"
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
- conn = get_connection()
34
- if query is None or len(query) == 0:
35
- sql_query = "SELECT * FROM poems"
36
- else:
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
- sql_query = f"SELECT * FROM poems WHERE"
39
- i = 0
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() -> Connection:
52
- """Put the connection in cache to reuse if path does not change between Streamlit reruns.
53
- NB : https://stackoverflow.com/questions/48218065/programmingerror-sqlite-objects-created-in-a-thread-can-only-be-used-in-that-sa
54
- """
55
- db_con = sqlite3.connect(URI_SQLITE_DB, check_same_thread=False)
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
 
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: hafez
3
- Version: 0.2.6
3
+ Version: 0.2.8
4
4
  Summary: Hafez Poems
5
5
  Author-email: Kaveh Bakhtiyari <kbakhtiyari@yahoo.com>
6
6
  Project-URL: Homepage, https://github.com/kavehbc/hafez
@@ -11,6 +11,7 @@ Classifier: Operating System :: OS Independent
11
11
  Requires-Python: >=3.9
12
12
  Description-Content-Type: text/markdown
13
13
  License-File: LICENSE
14
+ Dynamic: license-file
14
15
 
15
16
  # Divan Hafez Poems & Omen
16
17
 
@@ -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=K7bDXuMFxfwAD3p2L1tXgqH41aFZaeksO7Y-pUmvkdU,2573404
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.8.dist-info/licenses/LICENSE,sha256=TSObqi3glpU9S9P6kSv8d488pt0Cc3xDgoUF6yHlx3g,1099
9
+ hafez-0.2.8.dist-info/METADATA,sha256=OhkJ6Z_ux-4sMAH1jYAnKJUNvr88g1qhKqyS5nObmJg,4012
10
+ hafez-0.2.8.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
11
+ hafez-0.2.8.dist-info/top_level.txt,sha256=zl_xBZxOuP1dJA41E3Cnxb-LZrnLFhpu7JCz3UW5uDs,6
12
+ hafez-0.2.8.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.43.0)
2
+ Generator: setuptools (78.1.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
hafez/data/hafez.db DELETED
Binary file
@@ -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=aCntb5yFQMaNnoIwehFTHKUE3s6FYtb3fauiH4l08xc,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.6.dist-info/LICENSE,sha256=TSObqi3glpU9S9P6kSv8d488pt0Cc3xDgoUF6yHlx3g,1099
10
- hafez-0.2.6.dist-info/METADATA,sha256=O1-FlpWwZgB07xrAodCg8Pz9K51qA_8lkSNUI9XKGkA,3989
11
- hafez-0.2.6.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
12
- hafez-0.2.6.dist-info/top_level.txt,sha256=zl_xBZxOuP1dJA41E3Cnxb-LZrnLFhpu7JCz3UW5uDs,6
13
- hafez-0.2.6.dist-info/RECORD,,