Skip to content

Commit

Permalink
[BEAM-2085] Fixups for Python resource hints. (#14605)
Browse files Browse the repository at this point in the history
  • Loading branch information
tvalentyn authored Apr 22, 2021
1 parent 28020ef commit 2e9ee8c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions sdks/python/apache_beam/options/pipeline_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ def _add_argparse_args(cls, parser):

parser.add_argument(
'--resource_hint',
'--resource_hints',
dest='resource_hints',
action='append',
default=[],
Expand Down
10 changes: 6 additions & 4 deletions sdks/python/apache_beam/transforms/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ def register_resource_hint(
@staticmethod
def _parse_str(value):
if not isinstance(value, str):
raise ValueError()
raise ValueError("Input must be a string.")
return value.encode('ascii')

@staticmethod
def _parse_int(value):
if isinstance(value, str):
value = int(value)
if not isinstance(value, int):
raise ValueError()
raise ValueError("Input must be an integer.")
return str(value).encode('ascii')

@staticmethod
Expand All @@ -113,7 +113,7 @@ def _parse_storage_size_str(value):
return ResourceHint._parse_int(value)

if not isinstance(value, str):
raise ValueError()
raise ValueError("Input must be a string or integer.")

value = value.strip().replace(" ", "")
units = {
Expand All @@ -131,9 +131,11 @@ def _parse_storage_size_str(value):
}
match = re.match(r'.*?(\D+)$', value)
if not match:
raise ValueError()
raise ValueError("Unrecognized value pattern.")

suffix = match.group(1)
if suffix not in units:
raise ValueError("Unrecognized unit.")
multiplier = units[suffix]
value = value[:-len(suffix)]

Expand Down

0 comments on commit 2e9ee8c

Please sign in to comment.