IsaacKerson commited on
Commit
2610a06
1 Parent(s): ebdbeee

move functions to utils and update import

Browse files
Files changed (2) hide show
  1. pages/quiz_new.py +10 -12
  2. pages/utils.py +13 -0
pages/quiz_new.py CHANGED
@@ -3,10 +3,9 @@ import os.path
3
  import sqlite3
4
  import random
5
  import datetime
6
- import re
7
 
8
  # Custom imports
9
- from pages.utils import add_blanks, chunker, random_session_id, check_answer, db_connect, chk_conn
10
 
11
  def app():
12
 
@@ -55,17 +54,17 @@ def app():
55
  tag_string = st.session_state.tags
56
  num_q = st.session_state.num_q
57
 
58
- def clean_string(string):
59
- return re.sub('[^0-9a-zA-Z\s,]+', '', string)
60
 
61
- def split_string(string, split_on = ","):
62
- return [x.strip().upper() for x in string.split(split_on)]
63
 
64
- def make_subquery(terms, column = 'tags', operator = 'AND'):
65
- return f' {operator} '.join([f"{column} LIKE '%{x}%'" for x in terms if len(x) > 0])
66
 
67
- def make_query(subquery, limit = 10):
68
- return f"""SELECT * FROM vocab WHERE {subquery} ORDER BY RANDOM() LIMIT {str(limit)}"""
69
 
70
  clean_tags = clean_string(tag_string)
71
  terms = split_string(clean_tags)
@@ -74,8 +73,6 @@ def app():
74
 
75
  if tag_string:
76
 
77
- st.markdown(f"## QUIZ: {' '.join(terms)}")
78
-
79
  questions = []
80
  word_bank = []
81
 
@@ -92,6 +89,7 @@ def app():
92
  elif len(questions) < num_q:
93
  st.warning(f"There are only {len(questions)} with that tag.")
94
  else:
 
95
  st.markdown("### Word Bank")
96
  random.shuffle(word_bank)
97
  st.table(chunker(word_bank, 5))
 
3
  import sqlite3
4
  import random
5
  import datetime
 
6
 
7
  # Custom imports
8
+ from pages.utils import *
9
 
10
  def app():
11
 
 
54
  tag_string = st.session_state.tags
55
  num_q = st.session_state.num_q
56
 
57
+ # def clean_string(string):
58
+ # return re.sub('[^0-9a-zA-Z\s,]+', '', string)
59
 
60
+ # def split_string(string, split_on = ","):
61
+ # return [x.strip().upper() for x in string.split(split_on)]
62
 
63
+ # def make_subquery(terms, column = 'tags', operator = 'AND'):
64
+ # return f' {operator} '.join([f"{column} LIKE '%{x}%'" for x in terms if len(x) > 0])
65
 
66
+ # def make_query(subquery, limit = 10):
67
+ # return f"""SELECT * FROM vocab WHERE {subquery} ORDER BY RANDOM() LIMIT {str(limit)}"""
68
 
69
  clean_tags = clean_string(tag_string)
70
  terms = split_string(clean_tags)
 
73
 
74
  if tag_string:
75
 
 
 
76
  questions = []
77
  word_bank = []
78
 
 
89
  elif len(questions) < num_q:
90
  st.warning(f"There are only {len(questions)} with that tag.")
91
  else:
92
+ st.markdown(f"## QUIZ: {' '.join(terms)}")
93
  st.markdown("### Word Bank")
94
  random.shuffle(word_bank)
95
  st.table(chunker(word_bank, 5))
pages/utils.py CHANGED
@@ -3,6 +3,7 @@ import random
3
  import datetime
4
  import string
5
  import sqlite3
 
6
 
7
  def add_blanks(word, sentence, blank = "__"):
8
  return re.sub(word, blank, sentence, flags=re.IGNORECASE)
@@ -17,6 +18,18 @@ def random_session_id():
17
  def check_answer(item, answer):
18
  return item == answer
19
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def db_connect(database):
21
  conn = sqlite3.connect(database)
22
  c = conn.cursor()
 
3
  import datetime
4
  import string
5
  import sqlite3
6
+ import re
7
 
8
  def add_blanks(word, sentence, blank = "__"):
9
  return re.sub(word, blank, sentence, flags=re.IGNORECASE)
 
18
  def check_answer(item, answer):
19
  return item == answer
20
 
21
+ def clean_string(string):
22
+ return re.sub('[^0-9a-zA-Z\s,]+', '', string)
23
+
24
+ def split_string(string, split_on = ","):
25
+ return [x.strip().upper() for x in string.split(split_on)]
26
+
27
+ def make_subquery(terms, column = 'tags', operator = 'AND'):
28
+ return f' {operator} '.join([f"{column} LIKE '%{x}%'" for x in terms if len(x) > 0])
29
+
30
+ def make_query(subquery, limit = 10):
31
+ return f"""SELECT * FROM vocab WHERE {subquery} ORDER BY RANDOM() LIMIT {str(limit)}"""
32
+
33
  def db_connect(database):
34
  conn = sqlite3.connect(database)
35
  c = conn.cursor()