Skip to content

Commit 918bd55

Browse files
jtkeelingannarev
authored andcommitted
Prevent warning every time someone imports contrib.learn.datasets.base
Everything in contrib/learn/python/learn/datasets/base.py has been deprecated. One of the function in there is a decorator, retry. Because another function in that file is decorated with retry, the function is called upon import, which prints a warning. I have fixed this by adding a private function, _internal_retry, which is used internally, and redefining retry to simply call this. That way, using retry in user-code will still print the deprecated warning, but it's not printed upon every import. I also cleaned up the docstrings slightly. PiperOrigin-RevId: 190626717
1 parent 92f13e8 commit 918bd55

File tree

1 file changed

+34
-1
lines changed
  • tensorflow/contrib/learn/python/learn/datasets

1 file changed

+34
-1
lines changed

tensorflow/contrib/learn/python/learn/datasets/base.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,48 @@ def retry(initial_delay,
139139
140140
Args:
141141
initial_delay: the initial delay.
142+
max_delay: the maximum delay allowed (actual max is
143+
max_delay * (1 + jitter).
142144
factor: each subsequent retry, the delay is multiplied by this value.
143145
(must be >= 1).
144146
jitter: to avoid lockstep, the returned delay is multiplied by a random
145147
number between (1-jitter) and (1+jitter). To add a 20% jitter, set
146148
jitter = 0.2. Must be < 1.
149+
is_retriable: (optional) a function that takes an Exception as an argument
150+
and returns true if retry should be applied.
151+
152+
Returns:
153+
A function that wraps another function to automatically retry it.
154+
"""
155+
return _internal_retry(
156+
initial_delay=initial_delay,
157+
max_delay=max_delay,
158+
factor=factor,
159+
jitter=jitter,
160+
is_retriable=is_retriable)
161+
162+
163+
def _internal_retry(initial_delay,
164+
max_delay,
165+
factor=2.0,
166+
jitter=0.25,
167+
is_retriable=None):
168+
"""Simple decorator for wrapping retriable functions, for internal use only.
169+
170+
Args:
171+
initial_delay: the initial delay.
147172
max_delay: the maximum delay allowed (actual max is
148173
max_delay * (1 + jitter).
174+
factor: each subsequent retry, the delay is multiplied by this value.
175+
(must be >= 1).
176+
jitter: to avoid lockstep, the returned delay is multiplied by a random
177+
number between (1-jitter) and (1+jitter). To add a 20% jitter, set
178+
jitter = 0.2. Must be < 1.
149179
is_retriable: (optional) a function that takes an Exception as an argument
150180
and returns true if retry should be applied.
181+
182+
Returns:
183+
A function that wraps another function to automatically retry it.
151184
"""
152185
if factor < 1:
153186
raise ValueError('factor must be >= 1; was %f' % (factor,))
@@ -195,7 +228,7 @@ def _is_retriable(e):
195228

196229

197230
@deprecated(None, 'Please use urllib or similar directly.')
198-
@retry(initial_delay=1.0, max_delay=16.0, is_retriable=_is_retriable)
231+
@_internal_retry(initial_delay=1.0, max_delay=16.0, is_retriable=_is_retriable)
199232
def urlretrieve_with_retry(url, filename=None):
200233
return urllib.request.urlretrieve(url, filename)
201234

0 commit comments

Comments
 (0)