Skip to content

breaking: deprecate sagemaker.utils.to_str() #1621

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

Merged
merged 1 commit into from
Jun 24, 2020
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
12 changes: 5 additions & 7 deletions src/sagemaker/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
from __future__ import absolute_import
import json

from sagemaker.utils import to_str


class ParameterRange(object):
"""Base class for representing parameter ranges. This is used to define what
Expand Down Expand Up @@ -71,8 +69,8 @@ def as_tuning_range(self, name):
"""
return {
"Name": name,
"MinValue": to_str(self.min_value),
"MaxValue": to_str(self.max_value),
"MinValue": str(self.min_value),
"MaxValue": str(self.max_value),
"ScalingType": self.scaling_type,
}

Expand Down Expand Up @@ -111,9 +109,9 @@ def __init__(self, values): # pylint: disable=super-init-not-called
This input will be converted into a list of strings.
"""
if isinstance(values, list):
self.values = [to_str(v) for v in values]
self.values = [str(v) for v in values]
else:
self.values = [to_str(values)]
self.values = [str(values)]

def as_tuning_range(self, name):
"""Represent the parameter range as a dicionary suitable for a request
Expand Down Expand Up @@ -158,7 +156,7 @@ def cast_to_type(cls, value):
Args:
value:
"""
return to_str(value)
return str(value)


class IntegerParameter(ParameterRange):
Expand Down
6 changes: 2 additions & 4 deletions src/sagemaker/tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
)
from sagemaker.session import Session
from sagemaker.session import s3_input
from sagemaker.utils import base_name_from_image, name_from_base, to_str
from sagemaker.utils import base_name_from_image, name_from_base

AMAZON_ESTIMATOR_MODULE = "sagemaker"
AMAZON_ESTIMATOR_CLS_NAMES = {
Expand Down Expand Up @@ -345,9 +345,7 @@ def _prepare_static_hyperparameters(
):
"""Prepare static hyperparameters for one estimator before tuning"""
# Remove any hyperparameter that will be tuned
static_hyperparameters = {
to_str(k): to_str(v) for (k, v) in estimator.hyperparameters().items()
}
static_hyperparameters = {str(k): str(v) for (k, v) in estimator.hyperparameters().items()}
for hyperparameter_name in hyperparameter_ranges.keys():
static_hyperparameters.pop(hyperparameter_name, None)

Expand Down
20 changes: 0 additions & 20 deletions src/sagemaker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import random
import re
import shutil
import sys
import tarfile
import tempfile
import time
Expand Down Expand Up @@ -162,25 +161,6 @@ def get_short_version(framework_version):
return ".".join(framework_version.split(".")[:2])


def to_str(value):
"""Convert the input to a string, unless it is a unicode string in Python 2.

Unicode strings are supported as native strings in Python 3, but
``str()`` cannot be invoked on unicode strings in Python 2, so we need to
check for that case when converting user-specified values to strings.

Args:
value: The value to convert to a string.

Returns:
str or unicode: The string representation of the value or the unicode
string itself.
"""
if sys.version_info.major < 3 and isinstance(value, six.string_types):
return value
return str(value)


def extract_name_from_job_arn(arn):
"""Returns the name used in the API given a full ARN for a training job or
hyperparameter tuning job.
Expand Down
10 changes: 0 additions & 10 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,6 @@ def test_unique_name_from_base_truncated():
)


def test_to_str_with_native_string():
value = "some string"
assert sagemaker.utils.to_str(value) == value


def test_to_str_with_unicode_string():
value = u"åñøthér strîng"
assert sagemaker.utils.to_str(value) == value


def test_name_from_tuning_arn():
arn = "arn:aws:sagemaker:us-west-2:968277160000:hyper-parameter-tuning-job/resnet-sgd-tuningjob-11-07-34-11"
name = sagemaker.utils.extract_name_from_job_arn(arn)
Expand Down