forked from CodeMouse92/DeadSimplePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.py
More file actions
30 lines (23 loc) · 780 Bytes
/
book.py
File metadata and controls
30 lines (23 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import struct
class Book:
packer = struct.Struct(">64sx64sx2h")
def __init__(self, title="", author="", pages=0, pages_read=0):
self.title = title
self.author = author
self.pages = pages
self.pages_read = pages_read
def update_progress(self, pages_read):
self.pages_read = min(pages_read, self.pages)
def serialize(self):
return self.packer.pack(
self.title.encode(),
self.author.encode(),
self.pages,
self.pages_read
)
@classmethod
def deserialize(cls, bits):
title, author, pages, pages_read = cls.packer.unpack(bits)
title = title.decode()
author = author.decode()
return cls(title, author, pages, pages_read)