python包中的循环依赖

问题描述 投票:-1回答:1

我有一个任务来制作一个名为“ project”的python包。在内部,我必须创建2个具有我事先指定的名称的单独文件。在每一个内部,我必须放置1个带有其参数和方法的类。到目前为止一切都很好。但这是问题所在-每个类都引用另一个类以使其正常工作(在任务描述中,为我提供了两个类的方法的名称,其中一些必须引用另一个类的对象,所以我当然使用了project.xxxx导入Xxxx)。

我有使用方法的类库-

add_user(用户:用户)

我具有方法的类User

get_book(作者:str,书名:str,days_to_return:int,库:Library)

[这里我了解了循环依赖问题。我为一些文章提供了解决方法-将这些内容合并到一个文件中或制作更多文件并重组代码。那是不行的-名为“ project”的程序包必须有2个带有提供名称的文件,每个文件中都有1个类,因为我必须将带有该程序包的zip文件提交到在线自动化系统进行测试。

我遇到的另一件事是将import语句放在一个文件的末尾。我在两个文件上都尝试了1个文件,但仍然出现ImportError。

将非常感谢您的帮助。

这里是头等舱:

from project.user import User

class Library:
    def __init__(self):
        self.user_records = []
        self.books_available = {}
        self.rented_books = {}

    def add_user(self, user: User):
        if user.username in [u.username for u in self.user_records]:
            return f'User with id = {user.id} already registered in the library!'
        self.user_records.append(user)

    def remove_user(self, user: User):
        if user.username not in [u.username for u in self.user_records]:
            return 'We could not find such user to remove!'
        for i, o in enumerate(self.user_records):
            if o.username == user.username:
                del self.user_records[i]

    def change_username(self, user_id: int, new_username: str):
        user = [u for u in self.user_records if u.id == user_id][0]
        if not user:
            return f'There is no user with id = {user_id}!'
        if user.username == new_username:
            return f'Please check again the provided username - it should be different than the username used so far!'
        user.username = new_username
        return f'Username successfully changed to: {new_username} for userid: {user_id}'

这是第二堂课:

from project.library import Library

class User:
    def __init__(self, user_id: int, username: str):
        self.books = []
        self.id = user_id
        self.username = username

    def get_book(self, author: str, book_name: str, days_to_return: int, library: Library):
        rented_books = [x for x in library.rented_books.values()]
        if rented_books:
            for x in rented_books:
                if book_name in x:
                    days = x[book_name]
                    return f'he book "{book_name}" is already rented and will be available in' \
                           f' {days} days!'
        if book_name in library.books_available[author]:
            if self.username not in library.rented_books:
                library.rented_books[self.username] = {}
            library.rented_books[self.username].update({book_name: days_to_return})
            library.books_available[author].remove(book_name)
            self.books.append(book_name)
            return f'{book_name} successfully rented for the next {days_to_return} days!'

    def return_book(self, author:str, book_name:str, library: Library):
        if book_name in self.books:
            library.books_available[author].append(book_name)
            library.rented_books[self.username].pop(book_name)
            self.books.remove(book_name)
        else:
            return f"{self.username} doesn't have this book in his/her records!"

    def info(self):
        books = sorted(self.books, key=lambda x: x)
        return ', '.join(books)
python circular-dependency
1个回答
0
投票

很好,您开始编写自己的代码并将其发布。

快速的答案是,您无需在Library类中添加类型提示。代替:

def add_user(self, user: User):

您可以写:

def add_user(self, user):

并删除导入语句(from project.user import User

只需更改一下即可运行您的代码。我尝试这样做,例如:

library_of_congress = Library()
tom = User(user_id=1, username="tom")
library_of_congress.add_user(tom)

注意:您仍然需要执行一些其他步骤才能使其他功能正常工作:(但这超出了您的“循环依赖问题”的范围)

library_of_congress.books_available = { <put the book info here> }
tom.get_book( <put the correct arguments> )

希望这对您的剩余工作有帮助,祝您好运!您可以在此处阅读有关python中类型提示的更多信息:

https://realpython.com/python-type-checking/

https://docs.python.org/3/library/typing.html

© www.soinside.com 2019 - 2024. All rights reserved.