|
4 | 4 | # Bad example |
5 | 5 | # class FileManager: |
6 | 6 | # def __init__(self, filename): |
7 | | -# self.filename = Path(filename) |
| 7 | +# self.path = Path(filename) |
8 | 8 |
|
9 | | -# def read(self): |
10 | | -# with self.filename.open(mode="r") as file: |
11 | | -# data = file.read() |
12 | | -# return data |
| 9 | +# def read(self, encoding="utf-8"): |
| 10 | +# return self.path.read_text(encoding) |
13 | 11 |
|
14 | | -# def write(self, data): |
15 | | -# with self.filename.open(mode="w") as file: |
16 | | -# file.write(data) |
| 12 | +# def write(self, data, encoding="utf-8"): |
| 13 | +# self.path.write_text(data, encoding) |
17 | 14 |
|
18 | 15 | # def compress(self): |
19 | | -# with ZipFile(self.filename.stem + ".zip", mode="w") as archive: |
20 | | -# archive.write(self.filename) |
| 16 | +# with ZipFile(self.path.with_suffix(".zip"), mode="w") as archive: |
| 17 | +# archive.write(self.path) |
21 | 18 |
|
22 | | -# def decompress(self, archive_name): |
23 | | -# with ZipFile(archive_name, mode="r") as archive: |
| 19 | +# def decompress(self): |
| 20 | +# with ZipFile(self.path.with_suffix(".zip"), mode="r") as archive: |
24 | 21 | # archive.extractall() |
25 | 22 |
|
26 | 23 |
|
27 | 24 | # Good example |
28 | 25 | class FileManager: |
29 | 26 | def __init__(self, filename): |
30 | | - self.filename = Path(filename) |
| 27 | + self.path = Path(filename) |
31 | 28 |
|
32 | | - def read(self): |
33 | | - with self.filename.open(mode="r") as file: |
34 | | - data = file.read() |
35 | | - return data |
| 29 | + def read(self, encoding="utf-8"): |
| 30 | + return self.path.read_text(encoding) |
36 | 31 |
|
37 | | - def write(self, data): |
38 | | - with self.filename.open(mode="w") as file: |
39 | | - file.write(data) |
| 32 | + def write(self, data, encoding="utf-8"): |
| 33 | + self.path.write_text(data, encoding) |
40 | 34 |
|
41 | 35 |
|
42 | 36 | class ZipFileManager: |
43 | 37 | def __init__(self, filename): |
44 | | - self.filename = Path(filename) |
| 38 | + self.path = Path(filename) |
45 | 39 |
|
46 | 40 | def compress(self): |
47 | | - with ZipFile(self.filename.stem + ".zip", mode="w") as archive: |
48 | | - archive.write(self.filename) |
| 41 | + with ZipFile(self.path.with_suffix(".zip"), mode="w") as archive: |
| 42 | + archive.write(self.path) |
49 | 43 |
|
50 | | - def decompress(self, archive_name): |
51 | | - with ZipFile(archive_name, mode="r") as archive: |
| 44 | + def decompress(self): |
| 45 | + with ZipFile(self.path.with_suffix(".zip"), mode="r") as archive: |
52 | 46 | archive.extractall() |
0 commit comments