Skip to content

Commit

Permalink
Add python3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
spMohanty committed Nov 7, 2017
1 parent 8f96375 commit ff1300a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion generate_random_predictions.py
Expand Up @@ -32,7 +32,10 @@ def _policy(candidates):
for _idx, _impression in enumerate(data):
predictions = _policy(_impression["candidates"])
predictionline = _format_predictions(predictions)
output.write(predictionline+"\n")
predictionline + "\n"
predictionline = predictionline.encode() #Note this is important for python3 compatibility as we are writing in "wb" mode
output.write(predictionline)

if _idx % 500 == 0:
print("Processed {} impressions...".format(_idx))

Expand Down
6 changes: 6 additions & 0 deletions utils.py
Expand Up @@ -7,6 +7,8 @@ def extract_impression_id(line, assert_first_line=False):
"""
Extracts the impression_id from a line
"""
if type(line) == bytes:
line = line.decode()
return line[:line.index("|")].strip()

def extract_cost_propensity(line):
Expand All @@ -18,6 +20,8 @@ def extract_cost_propensity(line):
line: `string`
"""
if type(line) == bytes:
line = line.decode()
line_items = line.split("|")
assert len(line_items) == 4
cost = float(line_items[1].replace("l ","").strip())
Expand All @@ -27,6 +31,8 @@ def extract_cost_propensity(line):
return cost, propensity

def extract_features(line, debug=False):
if type(line) == bytes:
line = line.decode()
features_index = line.index("|f ")
feature_string = line[features_index:].replace("|f ","")
feature_set = feature_string.split()
Expand Down

0 comments on commit ff1300a

Please sign in to comment.