Alejandro Cremades commited on
Commit
14f90e3
1 Parent(s): 2714786

Move functions to separate file

Browse files
Middle_School_Card_Search.py CHANGED
@@ -1,37 +1,7 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import urllib.parse
4
  import streamlit_common.footer
5
-
6
-
7
- def compose_scryfall_url(x: str) -> str:
8
- return f"https://scryfall.com/search?q=prefer%3Aoldest%20!%22{urllib.parse.quote_plus(x)}%22"
9
-
10
-
11
- def row_to_link(x: pd.DataFrame) -> None:
12
- cardname = x["name"]
13
- if x.name_ja is not "":
14
- cardname = f"{cardname} / {x.name_ja}"
15
- st.markdown(f"- [{cardname}]({x.link})")
16
-
17
-
18
- def get_legal_cardnames(cardname: str, mslist_df: pd.DataFrame) -> list:
19
- english_match = mslist_df[mslist_df["name"].str.lower() == cardname.lower()]
20
- cardname_en_list = None
21
- if english_match.shape[0] > 0:
22
- cardname_en_list = english_match["name"].to_list()
23
- cardname_ja_list = english_match["name_ja"].to_list()
24
- japanese_match = mslist_df[mslist_df["name_ja"] == cardname]
25
- if japanese_match.shape[0] > 0:
26
- cardname_en_list = japanese_match["name"].to_list()
27
- cardname_ja_list = japanese_match["name_ja"].to_list()
28
- if cardname_en_list is not None and len(cardname_en_list) > 0:
29
- return [
30
- cardname_en_list[0] or None,
31
- cardname_ja_list[0] or None,
32
- ]
33
- return None
34
-
35
 
36
  mslist_path = "output/middleschool.csv"
37
  number_shown_results = 20
@@ -54,7 +24,7 @@ mslist_df.fillna("", inplace=True)
54
  st.write(mslist_df.shape[0], "cards are legal")
55
 
56
  name_input = st.text_input(f"Search by card name").strip()
57
- exact_match = get_legal_cardnames(name_input, mslist_df)
58
  results_en_df = mslist_df[
59
  mslist_df["name"].str.contains(name_input.lower(), case=False)
60
  ]
@@ -68,12 +38,12 @@ if name_input:
68
  if exact_match[1] is not None:
69
  cardname = f"{cardname} / {exact_match[1]}"
70
  st.write(
71
- f"✅ [{cardname}]({compose_scryfall_url(exact_match[0])}) is an exact match"
72
  )
73
  st.write(results_df.shape[0], f'cards found by "{name_input}"')
74
  if results_df.shape[0] > number_shown_results:
75
  st.write(f"Top {number_shown_results} results:")
76
- results_df["link"] = results_df["name"].apply(compose_scryfall_url)
77
- results_df[:number_shown_results].transpose().apply(row_to_link)
78
 
79
  streamlit_common.footer.write_footer()
 
1
  import streamlit as st
2
  import pandas as pd
 
3
  import streamlit_common.footer
4
+ import streamlit_common.lib as lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  mslist_path = "output/middleschool.csv"
7
  number_shown_results = 20
 
24
  st.write(mslist_df.shape[0], "cards are legal")
25
 
26
  name_input = st.text_input(f"Search by card name").strip()
27
+ exact_match = lib.get_legal_cardnames(name_input, mslist_df)
28
  results_en_df = mslist_df[
29
  mslist_df["name"].str.contains(name_input.lower(), case=False)
30
  ]
 
38
  if exact_match[1] is not None:
39
  cardname = f"{cardname} / {exact_match[1]}"
40
  st.write(
41
+ f"✅ [{cardname}]({lib.compose_scryfall_url(exact_match[0])}) is an exact match"
42
  )
43
  st.write(results_df.shape[0], f'cards found by "{name_input}"')
44
  if results_df.shape[0] > number_shown_results:
45
  st.write(f"Top {number_shown_results} results:")
46
+ results_df["link"] = results_df["name"].apply(lib.compose_scryfall_url)
47
+ results_df[:number_shown_results].transpose().apply(lib.row_to_link)
48
 
49
  streamlit_common.footer.write_footer()
pages/1_Check_Card_List.py CHANGED
@@ -1,23 +1,7 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import re
4
  import streamlit_common.footer
5
-
6
-
7
- def remove_number_of_copies(line: str) -> str:
8
- if len(line.strip()) < 1:
9
- return None
10
- pattern = re.compile("^([0-9]+) +")
11
- return pattern.sub("", line)
12
-
13
-
14
- def is_cardname_legal(cardname: str, mslist_df: pd.DataFrame) -> bool:
15
- if mslist_df[mslist_df["name"].str.lower() == cardname.lower()].shape[0] > 0:
16
- return True
17
- if mslist_df[mslist_df["name_ja"] == cardname].shape[0] > 0:
18
- return True
19
- return False
20
-
21
 
22
  mslist_path = "output/middleschool.csv"
23
 
@@ -46,13 +30,13 @@ input_list = col1.text_area(
46
  cardnames = []
47
 
48
  for line in input_list.split("\n"):
49
- cardname = remove_number_of_copies(line)
50
  if cardname is not None:
51
- cardnames.append(remove_number_of_copies(cardname))
52
 
53
  input_cards = pd.DataFrame(cardnames, columns=["cardname"])
54
  input_cards["legal"] = input_cards["cardname"].apply(
55
- is_cardname_legal, args=[mslist_df]
56
  )
57
 
58
  col2.write("##### Middle School legality")
 
1
  import streamlit as st
2
  import pandas as pd
 
3
  import streamlit_common.footer
4
+ import streamlit_common.lib as lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  mslist_path = "output/middleschool.csv"
7
 
 
30
  cardnames = []
31
 
32
  for line in input_list.split("\n"):
33
+ cardname = lib.remove_number_of_copies(line)
34
  if cardname is not None:
35
+ cardnames.append(lib.remove_number_of_copies(cardname))
36
 
37
  input_cards = pd.DataFrame(cardnames, columns=["cardname"])
38
  input_cards["legal"] = input_cards["cardname"].apply(
39
+ lib.is_cardname_legal, args=[mslist_df]
40
  )
41
 
42
  col2.write("##### Middle School legality")
streamlit_common/lib.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import re
4
+ import urllib.parse
5
+
6
+
7
+ def compose_scryfall_url(x: str) -> str:
8
+ return f"https://scryfall.com/search?q=prefer%3Aoldest%20!%22{urllib.parse.quote_plus(x)}%22"
9
+
10
+
11
+ def row_to_link(x: pd.DataFrame) -> None:
12
+ cardname = x["name"]
13
+ if x.name_ja is not "":
14
+ cardname = f"{cardname} / {x.name_ja}"
15
+ st.markdown(f"- [{cardname}]({x.link})")
16
+
17
+
18
+ def get_legal_cardnames(cardname: str, mslist_df: pd.DataFrame) -> list:
19
+ english_match = mslist_df[mslist_df["name"].str.lower() == cardname.lower()]
20
+ cardname_en_list = None
21
+ if english_match.shape[0] > 0:
22
+ cardname_en_list = english_match["name"].to_list()
23
+ cardname_ja_list = english_match["name_ja"].to_list()
24
+ japanese_match = mslist_df[mslist_df["name_ja"] == cardname]
25
+ if japanese_match.shape[0] > 0:
26
+ cardname_en_list = japanese_match["name"].to_list()
27
+ cardname_ja_list = japanese_match["name_ja"].to_list()
28
+ if cardname_en_list is not None and len(cardname_en_list) > 0:
29
+ return [
30
+ cardname_en_list[0] or None,
31
+ cardname_ja_list[0] or None,
32
+ ]
33
+ return None
34
+
35
+
36
+ def remove_number_of_copies(line: str) -> str:
37
+ if len(line.strip()) < 1:
38
+ return None
39
+ pattern = re.compile("^([0-9]+) +")
40
+ return pattern.sub("", line)
41
+
42
+
43
+ def is_cardname_legal(cardname: str, mslist_df: pd.DataFrame) -> bool:
44
+ if mslist_df[mslist_df["name"].str.lower() == cardname.lower()].shape[0] > 0:
45
+ return True
46
+ if mslist_df[mslist_df["name_ja"] == cardname].shape[0] > 0:
47
+ return True
48
+ return False