コード例 #1
0
def testLibrary():
    lib = Library()
    b1 = Book("Live by Night", "Dennis Lehane")
    b2 = Book("A Time of Gifts", "Patrick Leigh Fermor")
    b3 = Book("Barchester Towers", "Anthony Trollope")
    for b in (b1, b2, b3):
        lib.addBook(b)
    p1 = Patron("Ken")
    p2 = Patron("Joshua")
    p3 = Patron("Sara")
    for p in (p1, p2, p3):
        lib.addPatron(p)
    print("\nThe first library:")
    print(lib)
    lib.save("books.dat", "patrons.dat")
    secondLib = Library("books.dat", "patrons.dat")
    print("\nThe second library (a copy):")
    print(secondLib)
    print("Expect Ken, 0 kooks out:", lib.getPatron("Ken"))
    print("Expect None:", lib.getPatron("Carolyn"))
    print("Expect Live by Night:", lib.getBook("Live by Night").getTitle())
    print("Expect None:", lib.getBook("Mystic River"))
    print("Expect None:", lib.removeBook("Live by Night"))
    print("\nLibrary:")
    print(lib)
    print("Expect Book's title is not in the library:",
          lib.removeBook("Mystic River"))
    print("Expect None:", lib.removePatron("Ken"))
    print("\nLibrary:")
    print(lib)
    print("Expect Patron's name is not in the library:",
          lib.removePatron("Ken"))
コード例 #2
0
 def test_return_book_unavailable(self):
     available = 2
     book = Book(title=TITLE, author=AUTHOR, copies=2, available=available)
     result = book.return_book()
     self.assertEqual(book.available, available)
     self.assertEqual(
         result, "This is impossible! All the copies are already there.")
コード例 #3
0
 def test_borrow(self):
     self.b = Book('Django', 'Elena Santasheva')
     # self.b.borrow()
     # self.assertEqual(self.b.borrow(), 'You are unable to borrow this book, sorry!')
     assert self.b.borrow() == 'You have borrowed "Django" by Elena Santasheva'
     assert self.b.is_available() is False  # after borrow -> False (self.available-=1)
     assert self.b.pretty_name() == '"Django" by Elena Santasheva'
コード例 #4
0
ファイル: main.py プロジェクト: mozanunal/hashcode2020
 def __init__(self, filename):
     print('file--', filename)
     f = open(filename)
     l = f.readline().split(' ')
     self.filename = filename
     self.nBooks, self.nLibs, self.nDays = int(l[0]), int(l[1]), int(l[2])
     self.books = [
         Book(i, int(score))
         for i, score in enumerate(f.readline().split(' '))
     ]
     sumb = 0
     for b in self.books:
         sumb += b.score
     print(sumb / 1000000)
     self.book2Score = {book.id: int(book.score) for book in self.books}
     self.libs = []
     for libId in range(self.nLibs):
         l = [int(i) for i in f.readline().split(' ')]
         nBooks, nSign, nScan = l[0], l[1], l[2]
         books = [
             Book(int(i), int(self.book2Score[int(i)]))
             for i in f.readline().split(' ')
         ]
         lib = Library(libId, nBooks, nSign, nScan, books)
         self.libs.append(lib)
     self.pri()
コード例 #5
0
class BookTest(unittest.TestCase):
    def test_init(self):
        self.b = Book('Django', 'Elena Santasheva')
        self.assertEqual(self.b.title, 'Django')
        self.assertEqual(self.b.copies, 1)

    def test_borrow(self):
        self.b = Book('Django', 'Elena Santasheva')
        # self.b.borrow()
        # self.assertEqual(self.b.borrow(), 'You are unable to borrow this book, sorry!')
        self.assertEqual(self.b.borrow(), 'You have borrowed "Django" by Elena Santasheva')
        self.assertEqual(self.b.is_available(), False)  # after borrow -> False (self.available-=1)
        self.assertEqual(self.b.pretty_name(), '"Django" by Elena Santasheva')

    def test_return_book(self):
        self.b = Book('Django', 'Elena Santasheva')
        self.assertEqual(self.b.return_book(), 'This is impossible! All the copies are already there.')
        self.assertEqual(self.b.is_available(), True)  # after return -> True (self.available += 1)
コード例 #6
0
 def generate_book_by_row(self, row):
     """
         Creates a book, which is in a certain row using the
         information from the table cells.
     """
     record = ""
     for i in range(6):
         data = self.table.item(row, i).text()
         record += ("+" + data)
     return Book.book_by_record(record[1:])
コード例 #7
0
class TestBook():
    @pytest.mark.BookTest
    def test_init(self):
        self.b = Book('Django', 'Elena Santasheva')
        assert self.b.title == 'Django'
        assert self.b.copies == 1

    @pytest.mark.BookTest
    def test_borrow(self):
        self.b = Book('Django', 'Elena Santasheva')
        # self.b.borrow()
        # self.assertEqual(self.b.borrow(), 'You are unable to borrow this book, sorry!')
        assert self.b.borrow() == 'You have borrowed "Django" by Elena Santasheva'
        assert self.b.is_available() is False  # after borrow -> False (self.available-=1)
        assert self.b.pretty_name() == '"Django" by Elena Santasheva'

    @pytest.mark.BookTest
    def test_return_book(self):
        self.b = Book('Django', 'Elena Santasheva')
        assert self.b.return_book() == 'This is impossible! All the copies are already there.'
        assert self.b.is_available() is True  # after return -> True (self.available += 1)
コード例 #8
0
def parseInput(inp):
    itr = (line for line in inp.split('\n'))
    numberOfDifferentBooks, numberOfLibraries, numberOfDays = nl(itr)
    books = {bookID: Book(bookID, bookScore)
             for bookID, bookScore in enumerate(nl(itr))}
    libraries = SortedList()
    for i in range(numberOfLibraries):
        numberOfBooks, signUpTime, capacityOfShipping = nl(itr)
        line = nl(itr)
        libraryBooks = SortedList(books[bookID]
                                  for bookID in line)
        libraries.add(Library(i, numberOfBooks, signUpTime,
                              capacityOfShipping, libraryBooks, numberOfDays))
    return argparse.Namespace(numberOfDifferentBooks=numberOfDifferentBooks, numberOfLibraries=numberOfLibraries, numberOfDays=numberOfDays, books=books, libraries=libraries)
コード例 #9
0
def read(file_name):
    with open(file_name, 'r') as f:
        line = f.readline()
        num_books, num_libs, num_days = [int(l) for l in line.split()]
        line = f.readline()
        books = {i: Book(i, int(v)) for i, v in enumerate(line.split())}
        libs = []
        for i in range(num_libs):
            line = f.readline()
            nb, su, bpd = [int(l) for l in line.split()]
            line = f.readline()
            book_ids = [int(l) for l in line.split()]
            lb = {j: books[j] for j in book_ids}
            for b in lb.values():
                b.register_copy()
            libs.append(Library(i, lb, su, bpd))

    return num_days, libs
コード例 #10
0
ファイル: cli.py プロジェクト: G-HenriquezV/library
def add_book(ctx: click.Context, title: str, author: str) -> None:
    """
    Adds a book to the library. Additional arguments can be added.
    \b
    Example: add-book "Fahrenheit 491" "Ray Bradbury" -year 1953

    \b
    :param title: Title of the book
    :param author: Author of the book
    :param ctx: Context of the click command line
    """
    kwargs = {
        ctx.args[i][1:]: ctx.args[i + 1]
        for i in range(0, len(ctx.args), 2)
    }
    _book = Book(title, author, **kwargs)
    with Library(get_workinglib()) as library:
        library.add_book(_book)
        print(f'{_book} has been added to the library with {_book.bid} as bid')
コード例 #11
0
ファイル: test.py プロジェクト: skorohodovn/sda
 def test_pretty_name(self):
     book = Book("Title 1", "basar")
     pretty_name = book.pretty_name()
     self.assertEqual(pretty_name, '"Title 1" by basar')
コード例 #12
0
 def test_init(self):
     self.b = Book('Django', 'Elena Santasheva')
     assert self.b.title == 'Django'
     assert self.b.copies == 1
コード例 #13
0
 def setup(self):
     self.books_by_title = {}
     assert self.books_by_title == {}
     self.b = Book('Django', 'Elena Santasheva')
     self.l = Library()
コード例 #14
0
 def test_publication_year_is_not_converted_if_missing(self):
     book = Book()
     book.publication_year = None
     csl = ReferenceToCSL.convert(book)
     self.assertEqual(csl['issued'], {'raw': None})
コード例 #15
0
 def test_publisher_is_converted(self):
     book = Book()
     book.publisher = 'Aschehoug'
     csl = ReferenceToCSL.convert(book)
     self.assertEqual(csl['publisher'], 'Aschehoug')
コード例 #16
0
 def test_borrow_unavailable(self):
     available = 0
     book = Book(title=TITLE, author=AUTHOR, copies=2, available=available)
     result = book.borrow()
     self.assertEqual(book.available, available)
     self.assertTrue(type(result) == str)
コード例 #17
0
 def test_pretty_name(self):
     book = Book(title=TITLE, author=AUTHOR, copies=2, available=2)
     expected = f'"{book.title}" by {book.author}'
     actual = book.pretty_name()
     self.assertEqual(expected, actual)
コード例 #18
0
ファイル: test2.py プロジェクト: skorohodovn/sda
 def test_unit_default(self):
     book = Book("Book", "Nikolai")
     self.assertEqual(book.title, "Book")
     self.assertEqual(book.author, "Nikolai")
     self.assertEqual(book.copies, 1)
     self.assertEqual(book.available, 1)
コード例 #19
0
ファイル: test2.py プロジェクト: skorohodovn/sda
 def test_pretty_name(self):
     book = Book("Book", "Nikolai")
     self.assertEqual(book.pretty_name(), '"Book" by Nikolai')
コード例 #20
0
ファイル: test2.py プロジェクト: skorohodovn/sda
 def test_not_available(self):
     book = Book("Book", "Nikolai", 5, 0)
     self.assertFalse(book.is_available())
コード例 #21
0
ファイル: test2.py プロジェクト: skorohodovn/sda
 def test_is_available(self):
     book = Book("Book", "Nikolai", 5, 5)
     self.assertTrue(book.is_available())
コード例 #22
0
def index():
    if 'username' in session:

        if (bool(shelf.library) == False):

            #shelf=Bookshelf()

            book1 = Book.Book('To Kill A Mockingbird', ['Harper Lee'], [
                'Novel', 'Bildungsroman', 'Southern Gothic', 'Thriller',
                'Domestic Fiction', 'Legal Story'
            ], '978-0446310789', 'B0000000001')
            book2 = Book.Book('The Talisman', ['Stephen King', 'Peter Straub'],
                              ['Fantasy', 'Thriller'], '978-1451697216',
                              'B0000000002')
            book3 = Book.Book('The Bad President', ['Donald Trump'],
                              ['Impeachment', 'Delusional'],
                              '978-1sdfasdfasdff', 'B0000000003')

            # Adds a Book into the shelf
            shelf.addBook(book1)
            shelf.addBook(book2)
            shelf.addBook(book3)
            # print(shelf.index[book.getISBN()])

            # Hard Coded Transactions for Book 1
            HCT1 = Transaction.Transaction('12345', 'User01', 'User02',
                                           '978-0446310789',
                                           'Brooklyn Bridge Park')
            HCT2 = Transaction.Transaction('76483', 'User02', 'User03',
                                           '978-0446310789',
                                           'Hunter College Lunchroom')

            # Hard Coded Transactions for Book 2
            HCT3 = Transaction.Transaction('0', 'User04', 'User06',
                                           '978-1451697216',
                                           'Central Park - North Corridor')
            # HCT4 = Transaction.Transaction('85274', 'User06', 'User03', '978-1451697216', 'Prospect Park')

            # Addition of Hard coded Block Transactions for Book 1
            GenBlock = Block.Block('0', HCT1)
            SecondBlock = Block.Block(GenBlock.getBlockHash(), HCT2)
            GenBlockBook2 = Block.Block('0', HCT3)

            book1.addValidBlocks(GenBlock)
            book1.addValidBlocks(SecondBlock)
            book2.addValidBlocks(GenBlockBook2)

        books = shelf.library

        writeMe = shelf.__str__()
        # open(<filename>, <mode>)
        saveFile = open('MasterLedger.txt', 'w')
        # writes the text contained in the variable writeMe to the file declared above
        saveFile.write(writeMe)
        # Always remember after an operation is completed to close it.
        saveFile.close()

        return render_template('index.html',
                               books=books,
                               username=session['username'])
    else:
        return redirect(url_for('login'))
コード例 #23
0
ファイル: test.py プロジェクト: skorohodovn/sda
 def test_return_positive(self):
     book = Book("Title 1", "basar", 5, 4)
     resp = book.borrow()
     self.assertEqual((resp, 'You have borrowed "Title 1" by basar'))
     self.assertEqual(book.available, 3)
     self.assertEqual(book.copies, 5)
コード例 #24
0
 def test_init_negative_value(self):
     with self.assertRaises(ValueError):
         book = Book(title=TITLE, author=AUTHOR, copies=0, available=-2)
コード例 #25
0
 def test_is_available(self):
     book = Book(title=TITLE, author=AUTHOR, copies=1, available=0)
     self.assertFalse(book.is_available())
     book.available = 1
     self.assertTrue(book.is_available())
コード例 #26
0
 def test_single_author_is_converted(self):
     book = Book()
     book.authors = [Author(given='Jo', family='Nesbø')]
     csl = ReferenceToCSL.convert(book)
     first_author = csl['author'][0]
     self.assertEqual(first_author, {'family': 'Nesbø', 'given': 'Jo'})
コード例 #27
0
 def test_borrow_available(self):
     available = 2
     book = Book(title=TITLE, author=AUTHOR, copies=2, available=available)
     result = book.borrow()
     self.assertEqual(book.available, available - 1)
     self.assertTrue(book.title in result and book.author in result)
コード例 #28
0
 def test_publisher_place_is_not_converted_if_missing(self):
     book = Book()
     book.publisher_place = None
     csl = ReferenceToCSL.convert(book)
     self.assertEqual(csl['publisher-place'], False)
コード例 #29
0
 def test_return_book_available(self):
     available = 0
     book = Book(title=TITLE, author=AUTHOR, copies=2, available=available)
     result = book.return_book()
     self.assertEqual(book.available, available + 1)
     self.assertEqual(result, "Thank you!")
コード例 #30
0
 def test_publisher_place_is_converted(self):
     book = Book()
     book.publisher_place = 'Oslo'
     csl = ReferenceToCSL.convert(book)
     self.assertEqual(csl['publisher-place'], 'Oslo')
コード例 #31
0
 def test_init(self):
     book = Book(title=TITLE, author=AUTHOR, copies=2, available=1)
     self.assertEqual(book.title, TITLE)
     self.assertEqual(book.author, AUTHOR)
     self.assertEqual(book.copies, 2)
     self.assertEqual(book.available, 1)
コード例 #32
0
 def test_init_default_params(self):
     book = Book(title=TITLE, author=AUTHOR)
     self.assertEqual(book.title, TITLE)
     self.assertEqual(book.author, AUTHOR)
     self.assertEqual(book.copies, 1)
     self.assertEqual(book.available, 1)
コード例 #33
0
 def test_return_book(self):
     self.b = Book('Django', 'Elena Santasheva')
     assert self.b.return_book() == 'This is impossible! All the copies are already there.'
     assert self.b.is_available() is True  # after return -> True (self.available += 1)
コード例 #34
0
 def test_init_wrong_type(self):
     with self.assertRaises(TypeError):
         book = Book(title=2, author=32, copies='2', available='3')
コード例 #35
0
 def test_publication_year_is_converted(self):
     book = Book()
     book.publication_year = 2009
     csl = ReferenceToCSL.convert(book)
     self.assertEqual(csl['issued'], {'raw': '2009'})
コード例 #36
0
 def test_init_wrong_value(self):
     with self.assertRaises(ValueError):
         book = Book(title=TITLE, author=AUTHOR, copies=1, available=2)
コード例 #37
0
def testBook():
    print("\nTesting a book")
    b = Book("Live by Night", "Dennis Lehane")
    print("Expect")
    print(
        "Title: Live by Night\nAuthor: Dennis Lehane\nNot checked out\nWait list:"
    )
    print(b)
    print("Expect Live by Night:", b.getTitle())
    print("Expect Dennis Lehane:", b.getAuthor())
    print("Expect None", b.getPatron())
    p = Patron("Ken")
    print("Expect The book is not checked out:", b.returnMe())
    print("Expect None:", b.borrowMe(p))
    print("Expect\nTitle: Live by Night\nAuthor: Dennis Lehane")
    print("Checked out to: Ken, 1 books out\nWait list:")
    print(b)
    print("Expect Ken, 1 books out:", b.getPatron())
    print("Expect This book is already checked out:", b.borrowMe(p))
    secondP = Patron("Carolyn")
    print("Expect This book is already checked out:", b.borrowMe(secondP))
    print("Expect\nTitle: Live by Night\nAuthor: Dennis Lehane")
    print("Checked out to: Ken, 1 books out\nWait list:\nCarolyn, 0 books out")
    print(b)
    print("Expect Book loaned to a waiting patron:", b.returnMe())
    print("Expect\nTitle: Live by Night\nAuthor: Dennis Lehane")
    print("Checked out to: Carolyn, 1 books out\nWait list:")
    print(b)
コード例 #38
0
 def test_author_with_family_name_only_works_fine(self):
     book = Book()
     book.authors = [Author(given='Jo', family=None)]
     csl = ReferenceToCSL.convert(book)
     first_author = csl['author'][0]
     self.assertEqual(first_author, {'family': None, 'given': 'Jo'})