epitools 0.5.103 → 0.5.105
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.rdoc +1 -1
- data/Rakefile +2 -2
- data/TODO +1 -1
- data/VERSION +1 -1
- data/lib/epitools.rb +1 -1
- data/lib/epitools/colored.rb +25 -25
- data/lib/epitools/core_ext/enumerable.rb +1 -1
- data/lib/epitools/core_ext/file.rb +6 -2
- data/lib/epitools/core_ext/hash.rb +43 -29
- data/lib/epitools/core_ext/misc.rb +21 -1
- data/lib/epitools/core_ext/object.rb +19 -19
- data/lib/epitools/core_ext/truthiness.rb +5 -5
- data/lib/epitools/daemonize.rb +1 -1
- data/lib/epitools/hexdump.rb +1 -1
- data/lib/epitools/iter.rb +26 -26
- data/lib/epitools/lcs.rb +3 -3
- data/lib/epitools/mimemagic.rb +7 -7
- data/lib/epitools/niceprint.rb +18 -18
- data/lib/epitools/numwords.rb +34 -34
- data/lib/epitools/path.rb +7 -0
- data/lib/epitools/permutations.rb +9 -9
- data/lib/epitools/pretty_backtrace.rb +11 -11
- data/lib/epitools/progressbar.rb +7 -7
- data/lib/epitools/rails.rb +2 -2
- data/lib/epitools/rash.rb +16 -16
- data/lib/epitools/ratio.rb +1 -1
- data/lib/epitools/sys.rb +47 -47
- data/lib/epitools/trie.rb +4 -4
- data/lib/epitools/typed_struct.rb +5 -5
- data/lib/epitools/wm.rb +6 -6
- data/lib/epitools/zopen.rb +2 -2
- data/spec/autoreq_spec.rb +5 -5
- data/spec/browser_spec.rb +1 -1
- data/spec/colored_spec.rb +5 -5
- data/spec/core_ext_spec.rb +1 -1
- data/spec/histogram_spec.rb +3 -3
- data/spec/iter_spec.rb +16 -16
- data/spec/lcs_spec.rb +5 -5
- data/spec/numwords_spec.rb +8 -8
- data/spec/permutations_spec.rb +9 -9
- data/spec/rash_spec.rb +7 -7
- data/spec/ratio_spec.rb +5 -5
- data/spec/sys_spec.rb +6 -6
- data/spec/term_spec.rb +7 -7
- data/spec/typed_struct_spec.rb +5 -5
- data/spec/wm_spec.rb +4 -4
- data/spec/zopen_spec.rb +11 -11
- metadata +2 -2
data/lib/epitools/niceprint.rb
CHANGED
@@ -1,61 +1,61 @@
|
|
1
1
|
def niceprint(o, level=0, indent_first_line=true)
|
2
2
|
maxstring = 50
|
3
3
|
maxarray = 20
|
4
|
-
|
4
|
+
|
5
5
|
result = ""
|
6
|
-
|
6
|
+
|
7
7
|
dent = " "
|
8
|
-
indent = dent * level
|
9
|
-
|
8
|
+
indent = dent * level
|
9
|
+
|
10
10
|
result << indent if indent_first_line
|
11
|
-
|
11
|
+
|
12
12
|
case o
|
13
|
-
|
13
|
+
|
14
14
|
when Hash
|
15
15
|
#puts "Hash!"
|
16
16
|
result << "{\n"
|
17
|
-
|
17
|
+
|
18
18
|
o.each_with_index do |(k,v),i|
|
19
19
|
result << "#{indent+dent}#{k.inspect} => #{niceprint(v,level+1,false)}"
|
20
20
|
result << "," unless i == o.size
|
21
21
|
result << "\n"
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
result << "#{indent}}"
|
25
|
-
|
25
|
+
|
26
26
|
when Array
|
27
27
|
#puts "Array!"
|
28
28
|
indent_first = o.any? { |e| e.instance_of? Hash }
|
29
|
-
|
29
|
+
|
30
30
|
if indent_first
|
31
31
|
result << "[\n"
|
32
32
|
else
|
33
33
|
result << "["
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
o = o[0..maxarray] if o.size > maxarray
|
37
37
|
o.each do |e|
|
38
38
|
result << niceprint(e,level+1,indent_first)
|
39
|
-
result << ", "
|
39
|
+
result << ", "
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
result << "]"
|
43
|
-
|
43
|
+
|
44
44
|
when String
|
45
45
|
#puts "String!"
|
46
46
|
o = o[0..maxstring] + "..." if o.size > maxstring
|
47
47
|
result << o.inspect
|
48
|
-
|
48
|
+
|
49
49
|
else
|
50
50
|
result << o.inspect
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
if level == 0
|
54
54
|
print result
|
55
55
|
else
|
56
56
|
result
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
end
|
60
60
|
|
61
61
|
if $0 == __FILE__
|
@@ -68,6 +68,6 @@ if $0 == __FILE__
|
|
68
68
|
},
|
69
69
|
:d => "asdf"*1000,
|
70
70
|
}
|
71
|
-
|
71
|
+
|
72
72
|
puts niceprint(t)
|
73
73
|
end
|
data/lib/epitools/numwords.rb
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
# Works on numbers up to a googol!
|
17
17
|
#
|
18
18
|
class Numeric
|
19
|
-
|
19
|
+
|
20
20
|
# < 20
|
21
21
|
NAMES_SMALL = [
|
22
22
|
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
|
@@ -27,11 +27,11 @@ class Numeric
|
|
27
27
|
"twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"
|
28
28
|
]
|
29
29
|
|
30
|
-
# >= 100
|
30
|
+
# >= 100
|
31
31
|
NAMES_LARGE = [
|
32
32
|
"thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "quattuordecillion", "quindecillion", "sexdecillion", "septendecillion", "octodecillion", "novemdecillion", "vigintillion", "unvigintillion", "duovigintillion", "trevigintillion", "quattuorvigintillion", "quinvigintillion", "sexvigintillion", "septenvigintillion", "octovigintillion", "novemvigintillion", "trigintillion", "untrigintillion", "duotrigintillion"
|
33
33
|
]
|
34
|
-
|
34
|
+
|
35
35
|
#
|
36
36
|
# Convert this number to words (eg: 69 => 'sixty-nine').
|
37
37
|
# Works with numbers up to a googol (10^100).
|
@@ -42,38 +42,38 @@ class Numeric
|
|
42
42
|
else
|
43
43
|
num = self.to_i
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
if (n = num.to_s.size) > 102
|
47
47
|
return "more than a googol! (#{n} digits)"
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
whole_thing = []
|
51
|
-
|
51
|
+
|
52
52
|
triplets = num.commatize.split(',')
|
53
53
|
num_triplets = triplets.size
|
54
|
-
|
54
|
+
|
55
55
|
triplets.each_with_index do |triplet, i|
|
56
56
|
next if triplet.to_i == 0
|
57
|
-
|
57
|
+
|
58
58
|
result = []
|
59
|
-
|
59
|
+
|
60
60
|
tens, hunds = nil, nil
|
61
|
-
|
61
|
+
|
62
62
|
digits = triplet.chars.to_a
|
63
|
-
|
64
|
-
raise "Error: Not a triplet: #{triplet}" if digits.size > 3 or digits.size < 1
|
65
|
-
|
63
|
+
|
64
|
+
raise "Error: Not a triplet: #{triplet}" if digits.size > 3 or digits.size < 1
|
65
|
+
|
66
66
|
if digits.size == 3
|
67
|
-
digit = digits.shift.to_i
|
68
|
-
hunds = NAMES_SMALL[digit] + "-hundred" if digit > 0
|
67
|
+
digit = digits.shift.to_i
|
68
|
+
hunds = NAMES_SMALL[digit] + "-hundred" if digit > 0
|
69
69
|
digits.shift if digits.first == '0'
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
if digits.size == 2
|
73
73
|
n = digits.join('').to_i
|
74
|
-
|
75
|
-
if n > 0 and n < 20
|
76
|
-
tens = NAMES_SMALL[n]
|
74
|
+
|
75
|
+
if n > 0 and n < 20
|
76
|
+
tens = NAMES_SMALL[n]
|
77
77
|
elsif n > 0
|
78
78
|
tens = NAMES_MEDIUM[digits.shift.to_i - 2]
|
79
79
|
if digits.first != '0'
|
@@ -83,47 +83,47 @@ class Numeric
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
if digits.size == 1
|
88
|
-
n = digits.join('').to_i
|
89
|
-
tens = NAMES_SMALL[n] if n > 0
|
88
|
+
n = digits.join('').to_i
|
89
|
+
tens = NAMES_SMALL[n] if n > 0
|
90
90
|
end
|
91
91
|
|
92
|
-
if hunds
|
93
|
-
if tens
|
94
|
-
result << "#{hunds} and #{tens}"
|
92
|
+
if hunds
|
93
|
+
if tens
|
94
|
+
result << "#{hunds} and #{tens}"
|
95
95
|
else
|
96
|
-
result << hunds
|
96
|
+
result << hunds
|
97
97
|
end
|
98
|
-
else
|
98
|
+
else
|
99
99
|
result << tens if tens
|
100
100
|
end
|
101
101
|
|
102
102
|
magnitude = (num_triplets - i)
|
103
103
|
result << NAMES_LARGE[magnitude-2] if magnitude > 1
|
104
|
-
|
104
|
+
|
105
105
|
whole_thing << result.join(' ') if result.any?
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
whole_thing.join ', '
|
109
109
|
end
|
110
|
-
|
110
|
+
|
111
111
|
#
|
112
|
-
# Define the .million, .thousand, .hundred, etc. methods.
|
112
|
+
# Define the .million, .thousand, .hundred, etc. methods.
|
113
113
|
#
|
114
114
|
NAMES_LARGE.each_with_index do |name, magnitude|
|
115
115
|
define_method name do
|
116
116
|
pow = (magnitude+1) * 3
|
117
117
|
factor = 10**pow
|
118
|
-
|
119
|
-
if is_a?(Float)
|
118
|
+
|
119
|
+
if is_a?(Float)
|
120
120
|
(BigDecimal.new(to_s) * factor).to_i
|
121
121
|
else
|
122
122
|
self * factor
|
123
123
|
end
|
124
124
|
end
|
125
125
|
end
|
126
|
-
|
126
|
+
|
127
127
|
end
|
128
128
|
|
129
129
|
|
data/lib/epitools/path.rb
CHANGED
@@ -770,6 +770,13 @@ class Path
|
|
770
770
|
end
|
771
771
|
end
|
772
772
|
|
773
|
+
#
|
774
|
+
# Treat each line of the file as a json object, and parse them all, returning an array of hashes
|
775
|
+
#
|
776
|
+
def parse_lines
|
777
|
+
each_line.map { |line| JSON.parse line }
|
778
|
+
end
|
779
|
+
|
773
780
|
# Parse the file as JSON
|
774
781
|
def read_json
|
775
782
|
JSON.load(io)
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'epitools'
|
2
2
|
|
3
3
|
class Array
|
4
|
-
|
4
|
+
|
5
5
|
alias_method :mult, :"*"
|
6
|
-
|
6
|
+
|
7
7
|
#
|
8
8
|
# Overloaded * operator.
|
9
9
|
#
|
@@ -26,14 +26,14 @@ class Array
|
|
26
26
|
send(:mult, other)
|
27
27
|
end
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
#
|
31
31
|
# Multiply the array by itself 'exponent'-times.
|
32
32
|
#
|
33
33
|
def **(exponent)
|
34
34
|
([self] * exponent).foldl(:*)
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
def all_pairs(reflexive=false)
|
38
38
|
(0...size).each do |a|
|
39
39
|
start = reflexive ? a : a+1
|
@@ -42,9 +42,9 @@ class Array
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
enumerable :all_pairs
|
47
|
-
|
47
|
+
|
48
48
|
end
|
49
49
|
|
50
50
|
|
@@ -55,7 +55,7 @@ end
|
|
55
55
|
# permute is passed in as a block. For example:
|
56
56
|
#
|
57
57
|
# >> perms(1) { [1,2,3,4] }
|
58
|
-
# => [[1], [2], [3], [4]]
|
58
|
+
# => [[1], [2], [3], [4]]
|
59
59
|
# >> perms(2) { [1,2,3,4] }
|
60
60
|
# => [[1, 1], [1, 2], [1, 3], [1, 4], [2, 1], [2, 2], [2, 3], [2, 4],
|
61
61
|
# [3, 1], [3, 2], [3, 3], [3, 4], [4, 1], [4, 2], [4, 3], [4, 4]]
|
@@ -68,14 +68,14 @@ def perms(size, n=0, stack=[], &block)
|
|
68
68
|
results = []
|
69
69
|
if n >= size
|
70
70
|
results << stack
|
71
|
-
else
|
71
|
+
else
|
72
72
|
ps.each do |p|
|
73
73
|
results += perms(size, n+1, stack + [p], &block)
|
74
74
|
end
|
75
75
|
end
|
76
76
|
results
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
|
80
80
|
if $0 == __FILE__
|
81
81
|
puts "-------------- foldl ---"
|
@@ -8,30 +8,30 @@ class Line # :nodoc:
|
|
8
8
|
attr_accessor :path, :num, :meth, :dir, :filename
|
9
9
|
|
10
10
|
def initialize(path, num, meth)
|
11
|
-
|
11
|
+
|
12
12
|
@path = path
|
13
13
|
@num = num
|
14
14
|
@meth = meth
|
15
15
|
@gem = false
|
16
|
-
|
16
|
+
|
17
17
|
@dir, @filename = File.split(path)
|
18
|
-
|
18
|
+
|
19
19
|
if @dir =~ %r{^/usr/lib/ruby/gems/1.8/gems/(.+)}
|
20
20
|
@dir = "[gem] #{$1}"
|
21
21
|
@gem = true
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
end
|
25
25
|
|
26
26
|
def gem?
|
27
|
-
@gem
|
27
|
+
@gem
|
28
28
|
end
|
29
29
|
|
30
30
|
def codeline
|
31
31
|
l = @num.to_i - 1
|
32
32
|
open(path).readlines[l]
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
end
|
36
36
|
|
37
37
|
|
@@ -76,7 +76,7 @@ def color_backtrace_2(lines, options={})
|
|
76
76
|
groups.map! { |group| group.first.gem? ? [] : group }
|
77
77
|
end
|
78
78
|
|
79
|
-
|
79
|
+
|
80
80
|
for group in groups
|
81
81
|
if group.empty?
|
82
82
|
puts " ... ignored ... "
|
@@ -92,7 +92,7 @@ def color_backtrace_2(lines, options={})
|
|
92
92
|
|
93
93
|
#puts "#{firstline.filename.green} (#{firstline.dir.light_white})"
|
94
94
|
puts "#{firstline.filename.underline} (#{firstline.dir.light_white})"
|
95
|
-
|
95
|
+
|
96
96
|
max_methsize = group.map { |line| line.meth.size}.max
|
97
97
|
group.each do |line|
|
98
98
|
pad = " " * (max_methsize - line.meth.size)
|
@@ -101,7 +101,7 @@ def color_backtrace_2(lines, options={})
|
|
101
101
|
end
|
102
102
|
puts
|
103
103
|
end
|
104
|
-
|
104
|
+
|
105
105
|
end
|
106
106
|
|
107
107
|
|
@@ -252,13 +252,13 @@ if $0 == __FILE__
|
|
252
252
|
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
|
253
253
|
script/server:3
|
254
254
|
}.lines
|
255
|
-
|
255
|
+
|
256
256
|
begin
|
257
257
|
String.new nil
|
258
258
|
rescue => e
|
259
259
|
backtrace = e.backtrace
|
260
260
|
end
|
261
|
-
|
261
|
+
|
262
262
|
lines = parse_lines(backtrace)
|
263
263
|
#debug_backtrace(lines)
|
264
264
|
#color_backtrace_1(lines)
|
data/lib/epitools/progressbar.rb
CHANGED
@@ -36,7 +36,7 @@ class ProgressBar
|
|
36
36
|
clear
|
37
37
|
show
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
attr_reader :title
|
41
41
|
attr_reader :current
|
42
42
|
attr_reader :total
|
@@ -45,8 +45,8 @@ class ProgressBar
|
|
45
45
|
private
|
46
46
|
def fmt_bar
|
47
47
|
bar_width = do_percentage * @terminal_width / 100
|
48
|
-
sprintf("|%s%s|",
|
49
|
-
@bar_mark * bar_width,
|
48
|
+
sprintf("|%s%s|",
|
49
|
+
@bar_mark * bar_width,
|
50
50
|
" " * (@terminal_width - bar_width))
|
51
51
|
end
|
52
52
|
|
@@ -61,14 +61,14 @@ class ProgressBar
|
|
61
61
|
def fmt_stat_for_file_transfer
|
62
62
|
if !@total or @finished_p then
|
63
63
|
sprintf("%s %s %s", bytes, transfer_rate, elapsed)
|
64
|
-
else
|
64
|
+
else
|
65
65
|
sprintf("%s %s %s", bytes, transfer_rate, eta)
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
69
|
def fmt_title
|
70
70
|
title = (@title[0,(@title_width - 1)] + ":")
|
71
|
-
@total ? "%-#{@title_width}s" % title : title
|
71
|
+
@total ? "%-#{@title_width}s" % title : title
|
72
72
|
end
|
73
73
|
|
74
74
|
def convert_bytes(bytes)
|
@@ -115,7 +115,7 @@ class ProgressBar
|
|
115
115
|
elapsed = Time.now - @start_time
|
116
116
|
sprintf("Time: %s", format_time(elapsed))
|
117
117
|
end
|
118
|
-
|
118
|
+
|
119
119
|
def eol
|
120
120
|
if @finished_p then "\n" else "\r" end
|
121
121
|
end
|
@@ -165,7 +165,7 @@ class ProgressBar
|
|
165
165
|
# end
|
166
166
|
@out.print(line + eol)
|
167
167
|
@out.flush
|
168
|
-
|
168
|
+
|
169
169
|
@previous_time = Time.now
|
170
170
|
end
|
171
171
|
|