-
Notifications
You must be signed in to change notification settings - Fork 1
Code review #1
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
base: master
Are you sure you want to change the base?
Code review #1
Changes from 2 commits
e155871
8e43594
04bb477
694330d
48a7ecf
9d03605
31fdb56
34e6cad
173afd2
2c863d7
d948deb
704250c
b58e70d
f7e3782
042b951
6289aa9
5a17c59
d628218
caebad0
8517949
4075fc5
f7893a4
244dcb2
aae9185
f1e5c12
39616c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,15 @@ | ||
| import hashlib | ||
| import io | ||
| import time | ||
| import os | ||
| import requests | ||
| from bs4 import BeautifulSoup | ||
| import pandas as pd | ||
| import numpy as np | ||
| import shutil | ||
| from tqdm import tqdm | ||
| from selenium import webdriver | ||
| from PIL import Image | ||
| import signal | ||
| import platform | ||
| import threading | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using threading.timer to provide timeout mechanism for windows |
||
|
|
||
| driver_path = '/home/iheb/chromedriver' | ||
| output_path = 'data/images/robbery_images' | ||
|
|
@@ -33,22 +32,40 @@ | |
| "full body person portrait", | ||
| "person smiling"] | ||
| # search_terms = ["armed masked thief"] | ||
|
|
||
| class TimeoutException(Exception): | ||
| pass | ||
|
|
||
| class timeout: | ||
|
|
||
| def __init__(self, seconds= 1, error_message="Timeout"): | ||
| self.seconds = seconds | ||
| self.error_message = error_message | ||
|
|
||
| self.os_is_windows = platform.system().lower == 'windows' | ||
|
|
||
| def handle_timeout(self, signum, frame): | ||
| raise TimeoutError(self.error_message) | ||
|
|
||
| def __enter__(self): | ||
| signal.signal(signal.SIGALRM, self.handle_timeout) | ||
| signal.alarm(self.seconds) | ||
| if self.os_is_windows: | ||
| # For better portability a timeout class for windows is needed | ||
| self.timer = threading.Timer(self.seconds, self._raise_timeout) | ||
| self.timer.start | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. threading.timer won't interrupt any main thread in case it's in blocking operation (i,e I/O or sleep) it waits for it to complete and raises an exception |
||
| else: | ||
| # Use signal for Unix-based systems | ||
| signal.signal(signal.SIGALRM, self.handle_timeout) | ||
| signal.alarm(self.seconds) | ||
|
|
||
| def __exit__(self, type, value, traceback): | ||
| signal.alarm(0) | ||
|
|
||
| if self.os_is_windows: | ||
| # Cancel timer for windows | ||
| self.timer.cancel() | ||
| else: | ||
| # Disable Unix alarm | ||
| signal.alarm(0) | ||
|
|
||
| def _raise_timeout(self): | ||
| raise TimeoutException(self.error_message) | ||
|
|
||
| def fetch_image_urls(query: str, | ||
| max_links_to_fetch: int, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused imports