librus_apix.student_information
This module provides functions for retrieving student information from the Librus system, parsing it, and formatting it into a structured representation.
Classes: - StudentInformation: Represents student information with attributes such as name, class name, student number, etc.
Functions: - get_student_information: Retrieves student information from Librus.
Usage:
from librus_apix.client import new_client
# Create a new client instance
client = new_client()
client.get_token(username, password)
# Fetch student information
student_info = get_student_information(client)
print(student_info.name)
print(student_info.class_name)
print(student_info.number)
print(student_info.tutor)
print(student_info.school)
print(student_info.lucky_number)
1""" 2This module provides functions for retrieving student information from the Librus system, parsing it, and formatting it into a structured representation. 3 4Classes: 5 - StudentInformation: Represents student information with attributes such as name, class name, student number, etc. 6 7Functions: 8 - get_student_information: Retrieves student information from Librus. 9 10Usage: 11 ```python 12 from librus_apix.client import new_client 13 14 # Create a new client instance 15 client = new_client() 16 client.get_token(username, password) 17 18 # Fetch student information 19 student_info = get_student_information(client) 20 print(student_info.name) 21 print(student_info.class_name) 22 print(student_info.number) 23 print(student_info.tutor) 24 print(student_info.school) 25 print(student_info.lucky_number) 26 ``` 27""" 28 29from typing import Union 30from bs4 import BeautifulSoup 31from dataclasses import dataclass 32from librus_apix.exceptions import ParseError 33from librus_apix.helpers import no_access_check 34from librus_apix.client import Client 35 36 37@dataclass 38class StudentInformation: 39 """ 40 Represents student information. 41 42 Attributes: 43 name (str): The name of the student. 44 class_name (str): The class name of the student. 45 number (int): The student number. 46 tutor (str): The tutor of the student. 47 school (str): The school of the student. 48 lucky_number (Union[int, str]): The lucky number of the student, if available. 49 """ 50 51 name: str 52 class_name: str 53 number: int 54 tutor: str 55 school: str 56 lucky_number: Union[int, str] 57 58 59def get_student_information(client: Client): 60 """ 61 Retrieves student information from librus. 62 63 Args: 64 client (Client): The client object for making HTTP requests. 65 66 Returns: 67 StudentInformation: An object containing the student's information. 68 69 Raises: 70 ParseError: If there is an error while parsing or retrieving student information. 71 """ 72 soup = no_access_check( 73 BeautifulSoup( 74 client.get(client.INFO_URL).text, 75 "lxml", 76 ) 77 ) 78 try: 79 lucky_number = soup.select_one("span.luckyNumber > b") 80 if lucky_number is None: 81 lucky_number = "?" 82 else: 83 lucky_number = int(lucky_number.text) 84 except ValueError: 85 lucky_number = "?" 86 87 table = soup.select_one("table.decorated.big.center > tbody") 88 if table is None: 89 raise ParseError("Error while parsing student information table") 90 lines = table.find_all("tr", attrs={"class": ["line0", "line1"]})[:5] 91 data = [val.select_one("td").text.strip() for val in lines] 92 if len(data) < 5: 93 raise ParseError("Error while retrieving student information") 94 name, class_name, number, tutor, school = data[:5] 95 return StudentInformation( 96 name, 97 class_name, 98 int(number), 99 tutor, 100 "\n".join([n.strip() for n in school.split("\n")]), 101 lucky_number, 102 )
38@dataclass 39class StudentInformation: 40 """ 41 Represents student information. 42 43 Attributes: 44 name (str): The name of the student. 45 class_name (str): The class name of the student. 46 number (int): The student number. 47 tutor (str): The tutor of the student. 48 school (str): The school of the student. 49 lucky_number (Union[int, str]): The lucky number of the student, if available. 50 """ 51 52 name: str 53 class_name: str 54 number: int 55 tutor: str 56 school: str 57 lucky_number: Union[int, str]
Represents student information.
Attributes: name (str): The name of the student. class_name (str): The class name of the student. number (int): The student number. tutor (str): The tutor of the student. school (str): The school of the student. lucky_number (Union[int, str]): The lucky number of the student, if available.
60def get_student_information(client: Client): 61 """ 62 Retrieves student information from librus. 63 64 Args: 65 client (Client): The client object for making HTTP requests. 66 67 Returns: 68 StudentInformation: An object containing the student's information. 69 70 Raises: 71 ParseError: If there is an error while parsing or retrieving student information. 72 """ 73 soup = no_access_check( 74 BeautifulSoup( 75 client.get(client.INFO_URL).text, 76 "lxml", 77 ) 78 ) 79 try: 80 lucky_number = soup.select_one("span.luckyNumber > b") 81 if lucky_number is None: 82 lucky_number = "?" 83 else: 84 lucky_number = int(lucky_number.text) 85 except ValueError: 86 lucky_number = "?" 87 88 table = soup.select_one("table.decorated.big.center > tbody") 89 if table is None: 90 raise ParseError("Error while parsing student information table") 91 lines = table.find_all("tr", attrs={"class": ["line0", "line1"]})[:5] 92 data = [val.select_one("td").text.strip() for val in lines] 93 if len(data) < 5: 94 raise ParseError("Error while retrieving student information") 95 name, class_name, number, tutor, school = data[:5] 96 return StudentInformation( 97 name, 98 class_name, 99 int(number), 100 tutor, 101 "\n".join([n.strip() for n in school.split("\n")]), 102 lucky_number, 103 )
Retrieves student information from librus.
Args: client (Client): The client object for making HTTP requests.
Returns: StudentInformation: An object containing the student's information.
Raises: ParseError: If there is an error while parsing or retrieving student information.