Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Latest commit

 

History

History
39 lines (28 loc) · 1.38 KB

custom_class.rst

File metadata and controls

39 lines (28 loc) · 1.38 KB

Расширение функционала

Можно расширить базовый функционал библиотеки собственными методами и классами.

Пример:

from requests import session
from cinemate import Cinemate, Movie
from cinemate.utils import BaseCinemate


class MovieWithLinks(Movie):
    """ Добавление дополнительного метода к основному классу. """
    def links(self):
        return self.cinemate.link.for_movie(self.id)


class Link(BaseCinemate):
    """ Собственный класс для работы с ссылками. """
    @classmethod
    def for_movie(cls, movie_id):
        """ your code here """


class CinemateExtra(Cinemate):
    """ Переопределение главного класса. """
    def __init__(self, *args, **kwargs):
        super(CinemateExtra, self).__init__(*args, **kwargs)
        self.session = session()
        # переопределение класса movie
        self.movie = type('movie', (MovieWithLinks,), {'cinemate': self})
        # добавление класса link
        self.link = type('link', (Link,), {'cinemate': self})

    def login(self):
        """ your code here """