Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise 1 completed #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.idea*
*.DS_Store*
*postgres-data*
*postgres-data*
*/**/venv
56 changes: 54 additions & 2 deletions Exercises/Exercise-1/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import os,shutil
import glob
import zipfile
import requests
import io
import time
from concurrent.futures import ThreadPoolExecutor

download_uris = [
"https://divvy-tripdata.s3.amazonaws.com/Divvy_Trips_2018_Q4.zip",
Expand All @@ -11,10 +17,56 @@
]


t1 = time.perf_counter()

def download_file(url):
downloads = r'./downloads'
if not os.path.exists(downloads):
os.makedirs(downloads)

r = requests.get(url)
fileName = url.split("/")[-1]
with open(fileName, "wb") as file:
# create a folder downloads
zip = zipfile.ZipFile(io.BytesIO(r.content))
zip.extractall(downloads) # extract to folder downloads
os.remove(fileName) # remove zip file

def remove_zip_files():
# remove zip files
for f in glob.glob("*.zip"):
os.remove(f)


def remove_csv_files():
# remove csv files
try:
shutil.rmtree('./downloads/')
except FileNotFoundError or OSError:
pass


def main():
# your code here
pass
# create a folder downloads
try:
remove_csv_files()
remove_zip_files()
for url in download_uris:
download_file(url)
pass

except zipfile.BadZipFile or FileNotFoundError:
pass


main()
t2 = time.perf_counter()
print(f'Finished Single threading in {t2-t1} seconds')

if __name__ == "__main__":
main()
t1 = time.perf_counter()
with ThreadPoolExecutor(max_workers=7) as executor:
executor.map(download_file, download_uris)
t2 = time.perf_counter()
print(f'Finished Multi threading in {t2-t1} seconds')