1
|
|
|
"""School name validation"""
|
2
|
|
|
import os
|
3
|
|
|
from datetime import datetime
|
4
|
|
|
import aiohttp
|
5
|
|
|
import pandas as pd
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
async def update_list(bot):
|
9
|
|
|
"""update_list
|
10
|
|
|
---
|
11
|
|
|
Updates the school_list
|
12
|
|
|
"""
|
13
|
|
|
os.replace("school_list.csv", "school_list.csv.bak")
|
14
|
|
|
csv_url = "https://raw.githubusercontent.com/Competitive-Cyber-Clubs/School-List/master/school_list.csv" # noqa: E501 pylint: disable=line-too-long
|
15
|
|
|
new_list = open("school_list.csv", mode="w", encoding="utf-8")
|
16
|
|
|
async with aiohttp.ClientSession() as session:
|
17
|
|
|
async with session.get(csv_url) as resp:
|
18
|
|
|
new_file = await resp.text(encoding="utf-8")
|
19
|
|
|
new_list.write(new_file)
|
20
|
|
|
new_list.close()
|
21
|
|
|
bot.list_updated = datetime.utcnow()
|
22
|
|
|
bot.school_list = pd.read_csv("school_list.csv", encoding="utf-8")
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
async def school_check(school_list, name: str) -> bool:
|
26
|
|
|
"""school_check
|
27
|
|
|
---
|
28
|
|
|
Asynchronous Function
|
29
|
|
|
|
30
|
|
|
Checks to see if the name is in school_list.csv
|
31
|
|
|
|
32
|
|
|
Arguments:
|
33
|
|
|
---
|
34
|
|
|
name {str} -- Name of the school that the user wants to check against the list.
|
35
|
|
|
|
36
|
|
|
Returns:
|
37
|
|
|
---
|
38
|
|
|
bool -- Returns true if :ref:`name` is in :ref:`school_list.csv`
|
39
|
|
|
"""
|
40
|
|
|
return name in school_list.Institution_Name.values
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
async def region_select(school_list, name: str) -> str:
|
44
|
|
|
"""region_select
|
45
|
|
|
---
|
46
|
|
|
Asynchronous Function
|
47
|
|
|
|
48
|
|
|
Maps a regions for a school, :refs:`name`. Looks school name in the 'Institution_Name' column,
|
49
|
|
|
if it finds a match then pulls from 'Regions' column.
|
50
|
|
|
|
51
|
|
|
Arguments:
|
52
|
|
|
---
|
53
|
|
|
name {str} -- Name of the school that needs a region.
|
54
|
|
|
|
55
|
|
|
Returns:
|
56
|
|
|
---
|
57
|
|
|
str -- The region which the school has been mapped to.
|
58
|
|
|
"""
|
59
|
|
|
return school_list.Regions.values[school_list.Institution_Name == name][0]
|
60
|
|
|
|
61
|
|
|
|
62
|
|
|
async def school_search(school_list: pd.DataFrame, name: str) -> list:
|
63
|
|
|
"""school_search
|
64
|
|
|
---
|
65
|
|
|
Asynchronous Function
|
66
|
|
|
|
67
|
|
|
Searches for part of a school name in the 'Institution_Name' column in school_list.csv.
|
68
|
|
|
Turns the 'Institution_Name' into a series then gets all names using pandas.Series.str.contains.
|
69
|
|
|
|
70
|
|
|
Arguments:
|
71
|
|
|
---
|
72
|
|
|
name {str} -- Part of the name the user wants to search for.
|
73
|
|
|
|
74
|
|
|
Returns:
|
75
|
|
|
---
|
76
|
|
|
list -- Schools which had :ref:`name` in them.
|
77
|
|
|
"""
|
78
|
|
|
return school_list.Institution_Name.values[
|
79
|
|
|
school_list["Institution_Name"].str.contains(name, case=False)
|
80
|
|
|
]
|
81
|
|
|
|
82
|
|
|
|
83
|
|
|
async def state_list(school_list, state: str) -> list:
|
84
|
|
|
"""state_list
|
85
|
|
|
---
|
86
|
|
|
Asynchronous Function
|
87
|
|
|
|
88
|
|
|
Get a list of all schools in :ref:`state`.
|
89
|
|
|
|
90
|
|
|
Arguments:
|
91
|
|
|
---
|
92
|
|
|
state {str} -- The state to get all schools from.
|
93
|
|
|
|
94
|
|
|
Returns:
|
95
|
|
|
---
|
96
|
|
|
list -- List of all the schools in :ref:`state`.
|
97
|
|
|
"""
|
98
|
|
|
return school_list.Institution_Name.values[school_list.States == state]
|
99
|
|
|
|