Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/build/sage_bootstrap/util.py
4052 views
1
import time
2
3
4
def is_url(url):
5
"""
6
Test whether argument is url
7
"""
8
url = url.rstrip()
9
if len(url.splitlines()) > 1:
10
return False
11
if url.find(' ') >= 0:
12
return False
13
return (
14
url.startswith('http://') or
15
url.startswith('https://') or
16
url.startswith('ftp://')
17
)
18
19
20
def retry(func, exc=Exception, tries=3, delay=1):
21
"""
22
Call ``func()`` up to ``tries`` times, exiting only if the function
23
returns without an exception. If the function raises an exception on
24
the final try that exception is raised.
25
26
If given, ``exc`` can be either an `Exception` or a tuple of `Exception`s
27
in which only those exceptions result in a retry, and all other exceptions
28
are raised. ``delay`` is the time in seconds between each retry, and
29
doubles after each retry.
30
"""
31
32
while True:
33
try:
34
return func()
35
except exc:
36
tries -= 1
37
if tries == 0:
38
raise
39
40
time.sleep(delay)
41
delay *= 2
42
43