Passed
Pull Request — master (#50)
by Cyb3r
01:15
created

bot.utils.validate.region_select()   A

Complexity

Conditions 1

Size

Total Lines 14
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
"""School name validation"""
2
import os
3
import logging
4
from datetime import datetime
5
import aiohttp
6
import pandas as pd
7
8
log = logging.getLogger("bot")
9
10
11
async def update_list(bot, download: bool = False) -> None:
12
    """
13
    Update list
14
15
    Update the school list
16
    :param bot: The bot class to update the dataframe for
17
    :param download: Download the list
18
    :return: None
19
    """
20
    if download:
21
        log.info("Downloading new list")
22
        try:
23
            os.replace("school_list.csv", "school_list.csv.bak")
24
        except OSError:
25
            log.debug("School list did not exist")
26
        csv_url = "https://raw.githubusercontent.com/Competitive-Cyber-Clubs/School-List/master/school_list.csv"  # noqa: E501 pylint: disable=line-too-long
27
        with open("school_list.csv", mode="w", encoding="utf-8") as new_list:
28
            async with aiohttp.ClientSession() as session, session.get(csv_url) as resp:
29
                new_list.write("\n".join((await resp.text(encoding="utf-8")).splitlines()))
30
    bot.list_updated = datetime.utcnow()
31
    bot.school_list = pd.read_csv("school_list.csv", encoding="utf-8")
32
33
34
async def school_check(school_list: pd.DataFrame, name: str) -> bool:
35
    """
36
    School check
37
38
    Checks to see if the name is in school_list.csv
39
40
    :param school_list: Dataframe of lists
41
    :type school_list: pandas.DataFrame
42
    :param name: Name of the school that the member wants to check against the list.
43
    :type name: str
44
    :return: Name is in school_list
45
    """
46
    return name in school_list.Institution_Name.values
47
48
49
async def region_select(school_list: pd.DataFrame, name: str) -> str:
50
    """
51
    Region select
52
53
    Maps a regions for a school, :refs:`name`. Looks school name in the 'Institution_Name' column,
54
        if it finds a match then pulls from 'Regions' column.
55
56
    :param school_list: Dataframe of schools
57
    :type school_list: pandas.DataFrame
58
    :param name: Name of the school that needs a region.
59
    :type name: str
60
    :return:
61
    """
62
    return school_list.Regions.values[school_list.Institution_Name == name][0]
63
64
65
async def school_search(school_list: pd.DataFrame, name: str) -> list:
66
    """
67
    School Search
68
69
    Searches for part of a school name in the 'Institution_Name' column in school_list.
70
    Turns the 'Institution_Name' into a series then gets all names using pandas.Series.str.contains.
71
72
    :param school_list: Dataframe of schools
73
    :type school_list: pandas.DataFrame
74
    :param name: Part of the name the member wants to search for.
75
    :type name: str
76
    :return: All possible matches
77
    :rtype: list
78
    """
79
    return school_list.Institution_Name.values[
80
        school_list["Institution_Name"].str.contains(name, case=False)
81
    ]
82
83
84
async def state_list(school_list: pd.DataFrame, state: str) -> list:
85
    """
86
    State List
87
    Get a list of all schools in state.
88
    :param school_list: Dataframe of schools
89
    :type school_list: pandas.DataFrame
90
    :param state: State to get all schools from.
91
    :return: Schools in the state
92
    :rtype: list
93
    """
94
    return school_list.Institution_Name.values[school_list.States == state]
95