Passed
Push — master ( e17e00...ecdb37 )
by Cyb3r
01:27
created

utils.validate   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 7

5 Functions

Rating   Name   Duplication   Size   Complexity  
A school_search() 0 18 1
A state_list() 0 16 1
A school_check() 0 16 1
A region_select() 0 17 1
A update_list() 0 15 3
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