8000 Fix py2-3 compat issue in sending bytestring (#878) · comcomhanb/python-docs-samples@559c791 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 559c791

Browse files
elibixbydpebot
authored andcommitted
Fix py2-3 compat issue in sending bytestring (GoogleCloudPlatform#878)
* Fix py2-3 compat issue in sending bytestring * unmark test_predict_examples as slow * Move data to resources * Make test_census_example_bytes non-trivial * Fix linter * b64 encoded string must be decoded to utf-8
1 parent 2fac8d9 commit 559c791

File tree

4 files changed

+15
-21
lines changed

4 files changed

+15
-21
lines changed

ml_engine/online_prediction/predict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def predict_examples(project,
8989
response = service.projects().predict(
9090
name=name,
9191
body={'instances': [
92-
{'b64': base64.b64encode(example_bytes)}
92+
{'b64': base64.b64encode(example_bytes).decode('utf-8')}
9393
for example_bytes in example_bytes_list
9494
]}
9595
).execute()

ml_engine/online_prediction/predict_test.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""Tests for predict.py ."""
1616

17-
import base64
17+
import json
1818

1919
import pytest
2020

@@ -25,27 +25,20 @@
2525
JSON_VERSION = 'v1json'
2626
EXAMPLES_VERSION = 'v1example'
2727
PROJECT =< 10000 /span> 'python-docs-samples-tests'
28-
JSON = {
29-
'age': 25,
30-
'workclass': ' Private',
31-
'education': ' 11th',
32-
'education_num': 7,
33-
'marital_status': ' Never-married',
34-
'occupation': ' Machine-op-inspct',
35-
'relationship': ' Own-child',
36-
'race': ' Black',
37-
'gender': ' Male',
38-
'capital_gain': 0,
39-
'capital_loss': 0,
40-
'hours_per_week': 40,
41-
'native_country': ' United-States'
42-
}
4328
EXPECTED_OUTPUT = {
4429
u'confidence': 0.7760371565818787,
4530
u'predictions': u' <=50K'
4631
}
4732

4833

34+
with open('resources/census_test_data.json') as f:
35+
JSON = json.load(f)
36+
37+
38+
with open('resources/census_example_bytes.pb', 'rb') as f:
39+
BYTESTRING = f.read()
40+
41+
4942
def test_predict_json():
5043
result = predict.predict_json(
5144
PROJECT, MODEL, [JSON, JSON], version=JSON_VERSION)
@@ -60,13 +53,13 @@ def test_predict_json_error():
6053

6154
@pytest.mark.slow
6255
def test_census_example_to_bytes():
56+
import tensorflow as tf
6357
b = predict.census_to_example_bytes(JSON)
64-
assert base64.b64encode(b) is not None
58+
assert tf.train.Example.FromString(b) == tf.train.Example.FromString(
59+
BYTESTRING)
6560

6661

67-
@pytest.mark.slow
6862
def test_predict_examples():
69-
b = predict.census_to_example_bytes(JSON)
7063
result = predict.predict_examples(
71-
PROJECT, MODEL, [b, b], version=EXAMPLES_VERSION)
64+
PROJECT, MODEL, [BYTESTRING, BYTESTRING], version=EXAMPLES_VERSION)
7265
assert [EXPECTED_OUTPUT, EXPECTED_OUTPUT] == result
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"hours_per_week": 40, "native_country": " United-States", "relationship": " Own-child", "capital_loss": 0, "education": " 11th", "capital_gain": 0, "occupation": " Machine-op-inspct", "workclass": " Private", "gender": " Male", "age": 25, "marital_status": " Never-married", "race": " Black", "education_num": 7}

0 commit comments

Comments
 (0)
0