ZIP indir
0
main
1 dal
git@carbon4code.com:ahmet/Python-for-Data-Science.git
Ahmet Kerem Piscine Final
10 commit
new_student.py 1.2 KB
import random
import string
from dataclasses import dataclass, field


def generate_id() -> str:
    """Return a random identifier of 15 lowercase letters."""
    return "".join(random.choices(string.ascii_lowercase, k=15))


@dataclass
class Student:
    """Representing a student of the piscine.

    The login and the id are computed at creation time and cannot be
    given to the constructor.
    """

    name: str
    surname: str
    active: bool = True
    login: str = field(init=False)
    id: str = field(init=False, default_factory=generate_id)

    def __post_init__(self) -> None:
        """Build the login from the first name and the surname."""
        self.login = self.name[0] + self.surname


def main() -> None:
    """Test the creation of a student."""
    student = Student(name="Edward", surname="agle")
    print(student)
    print("---")
    try:
        print(Student(name="Edward", surname="agle", id="toto"))
    except TypeError as error:
        print(f"TypeError: {error}")
    print("---")
    try:
        print(Student(name="Edward", surname="agle", login="toto"))
    except TypeError as error:
        print(f"TypeError: {error}")


if __name__ == "__main__":
    main()