Skip to content
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

add python3 support #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 10 additions & 11 deletions data_hacks/bar_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,20 @@ def run(input_stream, options):
else:
data[row] += 1
total += 1

if not data:
print "Error: no data"
print("Error: no data")
sys.exit(1)

max_length = max([len(key) for key in data.keys()])
max_length = min(max_length, 50)
value_characters = 80 - max_length
max_value = max(data.values())
scale = int(math.ceil(float(max_value) / value_characters))
scale = max(1, scale)
print "# each " + options.dot + " represents a count of %d. total %d" % (scale, total)

print("# each " + options.dot + " represents a count of %d. total %d" % (scale, total))

if options.sort_values:
data = [[value, key] for key, value in data.items()]
data.sort(key=lambda x: x[0], reverse=options.reverse_sort)
Expand All @@ -79,13 +79,13 @@ def run(input_stream, options):
data.sort(key=lambda x: (Decimal(x[1])), reverse=options.reverse_sort)
else:
data.sort(key=lambda x: x[1], reverse=options.reverse_sort)

str_format = "%" + str(max_length) + "s [%6d] %s%s"
percentage = ""
for value, key in data:
if options.percentage:
percentage = " (%0.2f%%)" % (100 * Decimal(value) / Decimal(total))
print str_format % (key[:max_length], value, (value / scale) * options.dot, percentage)
print(str_format % (key[:max_length], value, (value // scale) * options.dot, percentage))

if __name__ == "__main__":
parser = OptionParser()
Expand All @@ -107,10 +107,9 @@ def run(input_stream, options):
parser.add_option("--dot", dest="dot", default='∎', help="Dot representation")

(options, args) = parser.parse_args()

if sys.stdin.isatty():
parser.print_usage()
print "for more help use --help"
print("for more help use --help")
sys.exit(1)
run(load_stream(sys.stdin), options)

17 changes: 9 additions & 8 deletions data_hacks/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
https://github.com/bitly/data_hacks
"""

from __future__ import print_function
import sys
from decimal import Decimal
import logging
Expand Down Expand Up @@ -97,17 +98,17 @@ def load_stream(input_stream, agg_value_key, agg_key_value):
yield DataPoint(Decimal(clean_line), 1)
except:
logging.exception('failed %r', line)
print >>sys.stderr, "invalid line %r" % line
print("invalid line %r" % line, file=sys.stderr)


def median(values, key=None):
if not key:
key = None # map and sort accept None as identity
length = len(values)
if length % 2:
median_indeces = [length/2]
median_indeces = [length // 2]
else:
median_indeces = [length/2-1, length/2]
median_indeces = [length // 2 - 1, length // 2]

values = sorted(values, key=key)
return sum(map(key,
Expand Down Expand Up @@ -241,7 +242,7 @@ def log_steps(k, n):
print("# Mean = %f; Variance = %f; SD = %f; Median %f" %
(mvsd.mean(), mvsd.var(), mvsd.sd(),
median(accepted_data, key=lambda x: x.value)))
print "# each " + options.dot + " represents a count of %d" % bucket_scale
print("# each " + options.dot + " represents a count of %d" % bucket_scale)
bucket_min = min_v
bucket_max = min_v
percentage = ""
Expand All @@ -252,12 +253,12 @@ def log_steps(k, n):
bucket_count = bucket_counts[bucket]
star_count = 0
if bucket_count:
star_count = bucket_count / bucket_scale
star_count = bucket_count // bucket_scale
if options.percentage:
percentage = " (%0.2f%%)" % (100 * Decimal(bucket_count) /
Decimal(samples))
print format_string % (bucket_min, bucket_max, bucket_count, options.dot *
star_count, percentage)
print(format_string % (bucket_min, bucket_max, bucket_count, options.dot *
star_count, percentage))


if __name__ == "__main__":
Expand Down Expand Up @@ -294,7 +295,7 @@ def log_steps(k, n):
if sys.stdin.isatty():
# if isatty() that means it's run without anything piped into it
parser.print_usage()
print "for more help use --help"
print("for more help use --help")
sys.exit(1)
histogram(load_stream(sys.stdin, options.agg_value_key,
options.agg_key_value), options)
13 changes: 7 additions & 6 deletions data_hacks/ninety_five_percent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
#
#
# Copyright 2010 Bitly
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand All @@ -20,6 +20,7 @@
https://github.com/bitly/data_hacks
"""

from __future__ import print_function
import sys
import os
from decimal import Decimal
Expand All @@ -35,16 +36,16 @@ def run():
try:
t = Decimal(line)
except:
print >>sys.stderr, "invalid line %r" % line
print("invalid line %r" % line, file=sys.stderr)
count +=1
data[t] = data.get(t, 0) + 1
print calc_95(data, count)
print(calc_95(data, count))

def calc_95(data, count):
# find the time it took for x entry, where x is the threshold
threshold = Decimal(count) * Decimal('.95')
start = Decimal(0)
times = data.keys()
times = list(data.keys())
times.sort()
for t in times:
# increment our count by the # of items in this time bucket
Expand All @@ -54,6 +55,6 @@ def calc_95(data, count):

if __name__ == "__main__":
if sys.stdin.isatty() or '--help' in sys.argv or '-h' in sys.argv:
print "Usage: cat data | %s" % os.path.basename(sys.argv[0])
print("Usage: cat data | %s" % os.path.basename(sys.argv[0]))
sys.exit(1)
run()
13 changes: 7 additions & 6 deletions data_hacks/run_for.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
#
#
# Copyright 2010 Bitly
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand All @@ -20,6 +20,7 @@
https://github.com/bitly/data_hacks
"""

from __future__ import print_function
import time
import sys
import os
Expand All @@ -38,7 +39,7 @@ def getruntime(arg):
elif suffix == "d":
return base * 60 * 60 * 24
else:
print >>sys.stderr, "invalid time suffix %r. must be one of s,m,h,d" % arg
print("invalid time suffix %r. must be one of s,m,h,d" % arg, file=sys.stderr)

def run(runtime):
end = time.time() + runtime
Expand All @@ -49,14 +50,14 @@ def run(runtime):

if __name__ == "__main__":
usage = "Usage: tail -f access.log | %s [time] | ..." % os.path.basename(sys.argv[0])
help = "time can be in the format 10s, 10m, 10h, etc"
help_str = "time can be in the format 10s, 10m, 10h, etc"
if sys.stdin.isatty():
print usage
print help
print(usage)
print(help_str)
sys.exit(1)

runtime = getruntime(sys.argv[-1])
if not runtime:
print usage
print(usage)
sys.exit(1)
run(runtime)
13 changes: 7 additions & 6 deletions data_hacks/sample.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
#
#
# Copyright 2010 Bitly
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand All @@ -20,6 +20,7 @@
https://github.com/bitly/data_hacks
"""

from __future__ import print_function
import sys
import random
from optparse import OptionParser
Expand Down Expand Up @@ -49,17 +50,17 @@ def get_sample_rate(rate_string):
parser = OptionParser(usage="cat data | %prog [options] [sample_rate]")
parser.add_option("--verbose", dest="verbose", default=False, action="store_true")
(options, args) = parser.parse_args()

if not args or sys.stdin.isatty():
parser.print_usage()
sys.exit(1)

try:
sample_rate = get_sample_rate(sys.argv[-1])
except ValueError, e:
print >>sys.stderr, e
except ValueError as e:
print(e, file=sys.stderr)
parser.print_usage()
sys.exit(1)
if options.verbose:
print >>sys.stderr, "Sample rate is %d%%" % sample_rate
print("Sample rate is %d%%" % sample_rate, file=sys.stderr)
run(sample_rate)