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

bot.utils.validate.state_list()   A

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 16
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):
12
    """update_list
13
    ---
14
    Updates the school_list
15
    """
16
    if download:
17
        log.info("Downloading new list")
18
        try:
19
            os.replace("school_list.csv", "school_list.csv.bak")
20
        except OSError:
21
            log.debug("School list did not exist")
22
        csv_url = "https://raw.githubusercontent.com/Competitive-Cyber-Clubs/School-List/master/school_list.csv"  # noqa: E501 pylint: disable=line-too-long
23
        with open("school_list.csv", mode="w", encoding="utf-8") as new_list:
24
            async with aiohttp.ClientSession() as session, session.get(csv_url) as resp:
25
                new_list.write("\n".join((await resp.text(encoding="utf-8")).splitlines()))
26
    bot.list_updated = datetime.utcnow()
27
    bot.school_list = pd.read_csv("school_list.csv", encoding="utf-8")
28
29
30
async def school_check(school_list, name: str) -> bool:
31
    """school_check
32
    ---
33
    Asynchronous Function
34
35
    Checks to see if the name is in school_list.csv
36
37
    Arguments:
38
    ---
39
        name {str} -- Name of the school that the user wants to check against the list.
40
41
    Returns:
42
    ---
43
        bool -- Returns true if :ref:`name` is in :ref:`school_list.csv`
44
    """
45
    return name in school_list.Institution_Name.values
46
47
48
async def region_select(school_list, name: str) -> str:
49
    """region_select
50
    ---
51
    Asynchronous Function
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
    Arguments:
57
    ---
58
        name {str} -- Name of the school that needs a region.
59
60
    Returns:
61
    ---
62
        str -- The region which the school has been mapped to.
63
    """
64
    return school_list.Regions.values[school_list.Institution_Name == name][0]
65
66
67
async def school_search(school_list: pd.DataFrame, name: str) -> list:
68
    """school_search
69
    ---
70
    Asynchronous Function
71
72
    Searches for part of a school name in the 'Institution_Name' column in school_list.csv.
73
    Turns the 'Institution_Name' into a series then gets all names using pandas.Series.str.contains.
74
75
    Arguments:
76
    ---
77
        name {str} -- Part of the name the user wants to search for.
78
79
    Returns:
80
    ---
81
        list -- Schools which had :ref:`name` in them.
82
    """
83
    return school_list.Institution_Name.values[
84
        school_list["Institution_Name"].str.contains(name, case=False)
85
    ]
86
87
88
async def state_list(school_list: pd.DataFrame, state: str) -> list:
89
    """state_list
90
    ---
91
    Asynchronous Function
92
93
    Get a list of all schools in :ref:`state`.
94
95
    Arguments:
96
    ---
97
        state {str} -- The state to get all schools from.
98
99
    Returns:
100
    ---
101
        list -- List of all the schools in :ref:`state`.
102
    """
103
    return school_list.Institution_Name.values[school_list.States == state]
104