Skip to content

Commit 3562774

Browse files
author
David Noble
committed
Resolved test case failure: test_custom_search (test_examples.ExamplesTestCase)
Switched from float to Decimal computation in tophashtags.py unit test. This ensures uniform precision on all platforms. Without this fix precision is different on OS X 10.8 than it is on CentOS 6.4, OS X 10.9, and Windows Server 2008 R2.
1 parent d99641d commit 3562774

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

examples/twitted/twitted/bin/tophashtags.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import csv, sys, urllib, re
1818

19+
1920
# Tees output to a logfile for debugging
2021
class Logger:
2122
def __init__(self, filename, buf = None):
@@ -36,6 +37,7 @@ def write(self, message):
3637
self.buf.write(message)
3738
self.buf.flush()
3839

40+
3941
# Tees input as it is being read, also logging it to a file
4042
class Reader:
4143
def __init__(self, buf, filename = None):
@@ -65,6 +67,7 @@ def readline(self):
6567
# Return to the caller
6668
return line
6769

70+
6871
def output_results(results, mvdelim = '\n', output = sys.stdout):
6972
"""Given a list of dictionaries, each representing
7073
a single result, and an optional list of fields,
@@ -91,6 +94,7 @@ def output_results(results, mvdelim = '\n', output = sys.stdout):
9194
writer.writerow(dict(zip(fields, fields)))
9295
writer.writerows(results)
9396

97+
9498
def read_input(buf, has_header = True):
9599
"""Read the input from the given buffer (or stdin if no buffer)
96100
is supplied. An optional header may be present as well"""
@@ -132,6 +136,7 @@ def read_input(buf, has_header = True):
132136

133137
return buf, header
134138

139+
135140
def encode_mv(vals):
136141
"""For multivalues, values are wrapped in '$' and separated using ';'
137142
Literal '$' values are represented with '$$'"""
@@ -144,6 +149,7 @@ def encode_mv(vals):
144149

145150
return s
146151

152+
147153
def main(argv):
148154
stdin_wrapper = Reader(sys.stdin)
149155
buf, settings = read_input(stdin_wrapper, has_header = True)
@@ -167,12 +173,13 @@ def main(argv):
167173

168174
num_hashtags = sum(hashtags.values())
169175

176+
from decimal import Decimal
170177
results = []
171178
for k, v in hashtags.iteritems():
172179
results.append({
173180
"hashtag": k,
174181
"count": v,
175-
"percentage": (float(v) / float(num_hashtags))
182+
"percentage": (Decimal(v) / Decimal(num_hashtags))
176183
})
177184

178185
# And output it to the next stage of the pipeline
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
count,hashtag,percentage
2-
1,#2,0.333333333333
3-
1,#redsox,0.333333333333
4-
1,#mlb,0.333333333333
2+
1,#2,0.3333333333333333333333333333
3+
1,#redsox,0.3333333333333333333333333333
4+
1,#mlb,0.3333333333333333333333333333

0 commit comments

Comments
 (0)