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

Simplify with from six.moves import input #9216

Merged
merged 10 commits into from
Feb 2, 2018
Merged
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
14 changes: 6 additions & 8 deletions keras/utils/io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from __future__ import print_function

import numpy as np
import sys
from collections import defaultdict

import six
try:
import h5py
except ImportError:
Expand Down Expand Up @@ -141,13 +141,11 @@ def ask_to_proceed_with_overwrite(filepath):
# Returns
True if we can proceed with overwrite, False otherwise.
"""
get_input = input
if sys.version_info[:2] <= (2, 7):
get_input = raw_input
overwrite = get_input('[WARNING] %s already exists - overwrite? '
'[y/n]' % (filepath))
while overwrite not in ['y', 'n']:
overwrite = get_input('Enter "y" (overwrite) or "n" (cancel).')
overwrite = six.moves.input('[WARNING] %s already exists - overwrite? '
'[y/n]' % (filepath)).strip().lower()
while overwrite not in ('y', 'n'):
overwrite = six.moves.input('Enter "y" (overwrite) or "n" '
'(cancel).').strip().lower()
if overwrite == 'n':
return False
print('[TIP] Next time specify overwrite=True!')
Expand Down
21 changes: 7 additions & 14 deletions tests/keras/utils/io_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from keras.utils.io_utils import HDF5Matrix
from keras.utils.io_utils import ask_to_proceed_with_overwrite
import numpy as np
import six
import warnings
import h5py
try:
Expand Down Expand Up @@ -106,20 +107,12 @@ def test_io_utils(in_tmpdir):


def test_ask_to_proceed_with_overwrite():
if sys.version_info[:2] <= (2, 7):
with patch('__builtin__.raw_input') as mock:
mock.return_value = 'y'
assert ask_to_proceed_with_overwrite('/tmp/not_exists')

mock.return_value = 'n'
assert not ask_to_proceed_with_overwrite('/tmp/not_exists')
else:
with patch('builtins.input') as mock:
mock.return_value = 'y'
assert ask_to_proceed_with_overwrite('/tmp/not_exists')

mock.return_value = 'n'
assert not ask_to_proceed_with_overwrite('/tmp/not_exists')
with patch('six.moves.input') as mock:
mock.return_value = 'y'
assert ask_to_proceed_with_overwrite('/tmp/not_exists')

mock.return_value = 'n'
assert not ask_to_proceed_with_overwrite('/tmp/not_exists')


if __name__ == '__main__':
Expand Down