-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranton_project_setup.py
More file actions
250 lines (193 loc) · 8.26 KB
/
Copy pathbranton_project_setup.py
File metadata and controls
250 lines (193 loc) · 8.26 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""
File: branton_project_setup.py
Purpose: Automate the creation of project folders
(and demonstrate basic Python coding skills).
Hint: See the Textbook, Skill Drills, and GUIDES for code snippets to help complete this module.
Author: Branton Dawson
"""
#####################################
# Import Modules at the Top
#####################################
# Import from the Python Standard library
import pathlib
import sys
import time # For time.sleep()
# Import packages from requirements.txt
import loguru
# Ensure project root is in sys.path for local imports
sys.path.append(str(pathlib.Path(__file__).resolve().parent))
# Import local modules
import utils_branton_advanced
#####################################
# Configure Logger and Verify
#####################################
logger = loguru.logger
logger.add("project.log", level="INFO", rotation="100 KB")
logger.info("Logger loaded.")
#####################################
# Declare global variables
#####################################
# Create a project path object for the root directory of the project.
ROOT_DIR = pathlib.Path.cwd()
REGIONS = [
"North America",
"South America",
"Europe",
"Asia",
"Africa",
"Oceania",
"Middle East"
]
#####################################
# Define Function 1. For item in Range:
# Create a function to generate folders for a given range (e.g., years).
# Pass in an int for the first year
# Pass in an int for the last year
#####################################
def create_folders_for_range(start_year: int, end_year: int) -> None:
'''
Create folders for a given range of years.
Arguments:
start_year -- The starting year of the range (inclusive).
end_year -- The ending year of the range (inclusive).
'''
# Log function name and parameters
logger.info("FUNCTION: create_folders_for_range()")
logger.info(f"PARAMETERS: start_year = {start_year}, end_year = {end_year}")
# Create folders for each year in the root directory
# Loop through the range of years
for year in range(start_year, end_year + 1):
year_path = ROOT_DIR / str(year)
year_path.mkdir(exist_ok=True)
logger.info(f"Created folder: {year_path}")
#####################################
# Define Function 2. For Item in List:
# Create folders from a list of names.
# Pass in a list of folder names
# After everything else is working,
# add options to force lowercase and remove spaces
#####################################
def create_folders_from_list(folder_list: list) -> None:
'''
Create folders based on a list of folder names.
Arguments:
folder_list -- A list of strings representing folder names.
'''
logger.info("FUNCTION: create_folders_from_list()")
logger.info(f"PARAMETER: folder_list = {folder_list}")
# Create folders for each year in the root directory
# Loop through the range of years
for folder in folder_list:
folder_path = ROOT_DIR / str(folder)
folder_path.mkdir(exist_ok=True)
logger.info(f"Created folder: {folder_path}")
#####################################
# Define Function 3. List Comprehension:
# Create a function to create prefixed folders by transforming a list of names
# and combining each with a prefix (e.g., "output-").
# Pass in a list of folder names
# Pass in a prefix (e.g. 'output-') to add to each
#####################################
def create_prefixed_folders_using_list_comprehension(folder_list: list, prefix: str) -> None:
'''
Create folders by adding a prefix to each item in a list
using a concise form of a for loop called a list comprehension.
Arguments:
folder_list -- A list of strings (e.g., ['csv', 'excel']).
prefix -- A string to prefix each name (e.g., 'output-').
'''
logger.info("FUNCTION: create_prefixed_folders()")
logger.info(f"PARAMETERS: folder_list = {folder_list}, prefix = {prefix}")
# Use a list comprehension to generate the folder names
prefixed_folders = [f"{prefix}{name}" for name in folder_list]
for folder in prefixed_folders:
folder_path = ROOT_DIR / folder
folder_path.mkdir(exist_ok=True)
logger.info(f"Created folder: {folder_path}")
logger.info(f"Total prefixed folders created: {len(prefixed_folders)}")
#####################################
# Define Function 4. While Loop:
# Write a function to create folders periodically
# (e.g., one folder every 5 seconds).
# Pass in the wait time in seconds
#####################################
def create_folders_periodically(duration_seconds: int) -> None:
'''
Create folders periodically over time using a while loop.
Arguments:
duration_seconds -- The number of seconds to wait between folder creations.
'''
logger.info("FUNCTION: create_folders_periodically()")
logger.info(f"PARAMETER: duration_seconds = {duration_seconds}")
num_folders = 5 # You can change this number as needed
created_folders = []
i = 1
while i <= num_folders:
folder_name = f"sleepfive_folder_{i}"
folder_path = ROOT_DIR / folder_name
folder_path.mkdir(exist_ok=True)
logger.info(f"Created folder: {folder_path}")
created_folders.append(folder_name)
if i < num_folders:
logger.info(f"Waiting {duration_seconds} seconds before creating the next folder...")
time.sleep(duration_seconds)
i += 1
logger.info(f"Total sleepfive folders created: {len(created_folders)}")
logger.info(f"List of sleepfive folders: {created_folders}")
#####################################
# Define Function 5. For Item in List:
# Create folders from a list of names.
# Pass in a list of folder names
# Add options to force lowercase AND remove spaces
#####################################
def create_standardized_folders(folder_list: list, to_lowercase: bool = False, remove_spaces: bool = False) -> None:
'''
Create folders from a list of names with options to standardize names.
Arguments:
folder_list -- A list of strings representing folder names.
to_lowercase -- If True, convert names to lowercase.
remove_spaces -- If True, remove spaces from names.
'''
logger.info("FUNCTION: create_standardized_folders()")
logger.info(f"PARAMETERS: folder_list = {folder_list}, to_lowercase = {to_lowercase}, remove_spaces = {remove_spaces}")
for name in folder_list:
folder_name = f"standard_folder_{name}"
if to_lowercase:
folder_name = folder_name.lower()
if remove_spaces:
folder_name = folder_name.replace(" ", "")
folder_path = ROOT_DIR / folder_name
folder_path.mkdir(exist_ok=True)
logger.info(f"Created standardized folder: {folder_path}")
#####################################
# Define a main() function for this module.
#####################################
def main() -> None:
''' Main function to demonstrate module capabilities. '''
logger.info("#####################################")
logger.info("# Starting execution of main()")
logger.info("#####################################\n")
# Module and get_byline() function
logger.info(f"Byline: {utils_branton_advanced.get_byline()}")
# Call function 1 to create folders for a range (e.g. years)
create_folders_for_range(start_year=2020, end_year=2025)
# Call function 2 to create folders given a list
folder_names = ['data-csv', 'data-excel', 'data-json']
create_folders_from_list(folder_names)
# Call function 3 to create folders using list comprehension
folder_names = ['csv', 'excel', 'json']
prefix = 'output-'
create_prefixed_folders_using_list_comprehension(folder_names, prefix)
# Call function 4 to create folders periodically using while
duration_secs:int = 5 # duration in seconds
create_folders_periodically(duration_secs)
# Call function 5 to create standardized folders, no spaces, lowercase
create_standardized_folders(REGIONS, to_lowercase=True, remove_spaces=True)
logger.info("\n#####################################")
logger.info("# Completed execution of main()")
logger.info("#####################################")
#####################################
# Conditional Execution
#####################################
if __name__ == '__main__':
main()