-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
59 lines (48 loc) · 1.57 KB
/
Copy pathhelpers.py
File metadata and controls
59 lines (48 loc) · 1.57 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
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from contextlib import contextmanager
import os, fileinput, time, re, signal, traceback
@contextmanager
def selenium_driver(driver_path, debug=True):
try:
service = Service(driver_path)
service.start()
driver = webdriver.Remote(service.service_url)
yield driver
except Exception as ex:
if debug:
print(traceback.format_exc())
input()
else:
raise ex
finally:
driver.quit()
def loc(locator):
if isinstance(locator, str):
locator = (By.CSS_SELECTOR, locator)
assert isinstance(locator, tuple)
return locator
def wait_until(condition, timeout=10, poll_frequency=0.5):
signal.signal(signal.SIGALRM, lambda _, __: throw(TimeoutException("Wait timeout exceed")))
signal.alarm(timeout)
while not condition():
time.sleep(poll_frequency)
signal.alarm(0)
def file_to_list(file_path):
with open(file_path, "r") as file:
lines = [line.rstrip() for line in file]
return lines
def read_ignore_comments(file_path):
lines = file_to_list(file_path)
return [line for line in lines if line and not line.startswith("#")]
def parse_float(string):
string = string.replace(u"\u2009", " ") \
.replace(" ", "") \
.replace(",", ".")
return float(re.search("[\d]+(.\d+)*", string).group(0))
def flatmap(func, *iterable):
return itertools.chain.from_iterable(map(func, *iterable))
def throw(error):
raise error