-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_challenge.py
More file actions
75 lines (60 loc) · 1.9 KB
/
format_challenge.py
File metadata and controls
75 lines (60 loc) · 1.9 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
###################### Bad format ###################
# # (c) Maximilian Schwarzmüller / Academind GmbH
#
# # *********
# # Imports
# # *********
# from os import path, makedirs
# from pathlib import Path
#
# # *********
# # Main
# # *********
# # A class which allows us to create DiskStorage instances
#
#
# class DiskStorage:
# def __init__(self, directory_name):
# self.storage_directory = directory_name
#
# def get_directory_path(self):
# return Path(self.storage_directory)
#
# # This must be called before a file is inserted
# def create_directory(self):
# if (not path.exists(self.get_directory_path())):
# makedirs(self.storage_directory)
#
# # Warning: Directory must exist in advance
# def insert_file(self, file_name, content):
# file = open(self.get_directory_path() / file_name, 'w')
# file.write(content)
# file.close()
# # Todo: Add proper error handling
#
#
# log_storage = DiskStorage('logs')
#
# log_storage.insert_file('test.txt', 'Test')
##################### Better format ####################
from os import path, makedirs
from pathlib import Path
# A class which allows us to create DiskStorage instances
class DiskStorage:
def __init__(self, directory_name):
self.storage_directory = directory_name
def get_directory_path(self):
return Path(self.storage_directory)
def create_directory(self):
if (not path.exists(self.get_directory_path())):
makedirs(self.storage_directory)
# Warning: Directory must exist in advance
def insert_file(self, file_name, content):
file_path = self.get_directory_path() / file_name
file = open(file_path, 'w')
file.write(content)
file.close()
# Todo: Add proper error handling
log_storage = DiskStorage('logs')
log_storage.create_directory()
log_storage.insert_file('test.txt', 'Test')