epitools 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/epitools.rb +11 -0
- data/lib/epitools/basetypes.rb +429 -0
- data/lib/epitools/hexdump.rb +66 -0
- data/lib/epitools/http.rb +82 -0
- data/lib/epitools/lcs.rb +39 -0
- data/lib/epitools/metaclass.rb +28 -0
- data/lib/epitools/niceprint.rb +73 -0
- data/lib/epitools/permutations.rb +53 -0
- data/lib/epitools/pretty_backtrace.rb +289 -0
- data/lib/epitools/rails.rb +11 -0
- data/lib/epitools/rash.rb +92 -0
- data/spec/basetypes_spec.rb +163 -0
- data/spec/lcs_spec.rb +23 -0
- data/spec/metaclass_spec.rb +12 -0
- data/spec/permutations_spec.rb +14 -0
- data/spec/rash_spec.rb +34 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +9 -0
- metadata +103 -0
@@ -0,0 +1,92 @@
|
|
1
|
+
#
|
2
|
+
# A Regex-queryable Hash
|
3
|
+
#
|
4
|
+
class Rash
|
5
|
+
|
6
|
+
attr_accessor :optimize_every
|
7
|
+
|
8
|
+
def initialize(initial={})
|
9
|
+
@hash = {}
|
10
|
+
@regexes = []
|
11
|
+
@regex_counts = Hash.new(0)
|
12
|
+
@optimize_every = 500
|
13
|
+
@lookups = 0
|
14
|
+
|
15
|
+
update(initial)
|
16
|
+
end
|
17
|
+
|
18
|
+
def []=(key, value)
|
19
|
+
if key.is_a? Regexp
|
20
|
+
key = normalize_regex(key)
|
21
|
+
@regexes << key
|
22
|
+
end
|
23
|
+
@hash[key] = value
|
24
|
+
end
|
25
|
+
|
26
|
+
def search_regexes(string)
|
27
|
+
@regexes.select { |r| string =~ r }.map { |r| @regex_counts[regex] += 1; @hash[r] }
|
28
|
+
end
|
29
|
+
|
30
|
+
def search_strings(regex)
|
31
|
+
keys.select { |key| key =~ regex if key.is_a? String }.map{ |key| @hash[key] }
|
32
|
+
end
|
33
|
+
|
34
|
+
def [](key)
|
35
|
+
return @hash[key] if @hash.include? key
|
36
|
+
|
37
|
+
case key
|
38
|
+
|
39
|
+
when String
|
40
|
+
optimize_if_necessary!
|
41
|
+
|
42
|
+
regexes = @regexes.select { |r| r =~ key }
|
43
|
+
|
44
|
+
if regexes.any?
|
45
|
+
return regexes.map do |regex|
|
46
|
+
@regex_counts[regex] += 1
|
47
|
+
@hash[regex]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
when Regexp
|
52
|
+
|
53
|
+
matches = search_strings(key)
|
54
|
+
|
55
|
+
if matches.any?
|
56
|
+
return matches
|
57
|
+
end
|
58
|
+
|
59
|
+
else
|
60
|
+
return @hash[key]
|
61
|
+
end
|
62
|
+
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
|
66
|
+
def update(other)
|
67
|
+
for key, value in other
|
68
|
+
self[key] = value
|
69
|
+
end
|
70
|
+
self
|
71
|
+
end
|
72
|
+
|
73
|
+
def method_missing(*args, &block)
|
74
|
+
@hash.send(*args, &block)
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def optimize_if_necessary!
|
80
|
+
if (@lookups += 1) >= @optimize_every
|
81
|
+
@regexes = @regex_counts.sort_by { |regex,count| -count }.map { |regex,count| regex }
|
82
|
+
@lookups = 0
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def normalize_regex(regex)
|
87
|
+
#/^#{regex}$/
|
88
|
+
regex
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
@@ -0,0 +1,163 @@
|
|
1
|
+
require 'epitools/basetypes'
|
2
|
+
|
3
|
+
|
4
|
+
describe Object do
|
5
|
+
|
6
|
+
it "in?" do
|
7
|
+
5.in?([1,2,3,4,5,6]).should == true
|
8
|
+
5.in?(1..10).should == true
|
9
|
+
5.in?(20..30).should == false
|
10
|
+
"butt".in?("butts!!!").should == true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "benches" do
|
14
|
+
lambda {
|
15
|
+
bench("benchmark test") { x = 10 }
|
16
|
+
}.should_not raise_error
|
17
|
+
|
18
|
+
lambda {
|
19
|
+
bench("benchmark test") { raise "ERROR" }
|
20
|
+
}.should raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
it "trys" do
|
24
|
+
s = Struct.new(:a,:b).new
|
25
|
+
s.a = 5
|
26
|
+
s.b = 10
|
27
|
+
|
28
|
+
s.try(:a).should == 5
|
29
|
+
s.try(:b).should == 10
|
30
|
+
s.try(:c).should == nil
|
31
|
+
|
32
|
+
lambda { s.try(:c) }.should_not raise_error
|
33
|
+
lambda { s.c }.should raise_error
|
34
|
+
|
35
|
+
def s.test(a); a; end
|
36
|
+
|
37
|
+
s.test(1).should == 1
|
38
|
+
s.try(:test, 1).should == 1
|
39
|
+
|
40
|
+
lambda { s.test }.should raise_error
|
41
|
+
lambda { s.try(:test) }.should raise_error
|
42
|
+
|
43
|
+
def s.blocky; yield; end
|
44
|
+
|
45
|
+
s.blocky{ 1 }.should == 1
|
46
|
+
s.try(:blocky){ 1 }.should == 1
|
47
|
+
s.try(:nonexistant){ 1 }.should == nil
|
48
|
+
end
|
49
|
+
|
50
|
+
it "nots" do
|
51
|
+
10.even?.should == true
|
52
|
+
10.not.even?.should == false
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
describe Integer do
|
59
|
+
|
60
|
+
it "integer?" do
|
61
|
+
|
62
|
+
{
|
63
|
+
nil => false,
|
64
|
+
"123" => true,
|
65
|
+
"000" => true,
|
66
|
+
123 => true,
|
67
|
+
123.45 => true,
|
68
|
+
"123asdf" => false,
|
69
|
+
"asdfasdf" => false,
|
70
|
+
Object.new => false,
|
71
|
+
|
72
|
+
}.each do |object, expected_result|
|
73
|
+
object.integer?.should == expected_result
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
it "has bits" do
|
79
|
+
1.to_bits.should == [1]
|
80
|
+
2.to_bits.should == [0,1]
|
81
|
+
3.to_bits.should == [1,1]
|
82
|
+
42.to_bits.should == [0,1,0,1,0,1]
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
describe Array do
|
89
|
+
|
90
|
+
it "squashes" do
|
91
|
+
[1,2,[3,4,[5],[],[nil,nil],[6]]].squash.should == [1,2,3,4,5,6]
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
describe Enumerable do
|
98
|
+
|
99
|
+
it "splits" do
|
100
|
+
[1,2,3,4,5].split_at{|e| e == 3}.should == [[1,2],[4,5]]
|
101
|
+
[1,2,3,4,5].split_after{|e| e == 3}.should == [[1,2,3],[4,5]]
|
102
|
+
[1,2,3,4,5].split_before{|e| e == 3}.should == [[1,2],[3,4,5]]
|
103
|
+
|
104
|
+
"a\nb\n---\nc\nd\n".split_at(/---/).map_recursive(&:strip).should == [ %w[a b], %w[c d] ]
|
105
|
+
end
|
106
|
+
|
107
|
+
it "sums" do
|
108
|
+
[1,2,3,4,5].sum.should == 15
|
109
|
+
end
|
110
|
+
|
111
|
+
it "averages" do
|
112
|
+
[1,3].average.should == 2.0
|
113
|
+
[1,1,3,3].average.should == 2.0
|
114
|
+
end
|
115
|
+
|
116
|
+
it "recursively maps" do
|
117
|
+
[[1,2],[3,4]].recursive_map {|e| e ** 2}.should == [[1,4],[9,16]]
|
118
|
+
[1,2,3,4].recursive_map {|e| e ** 2}.should == [1,4,9,16]
|
119
|
+
[[],[],1,2,3,4].recursive_map {|e| e ** 2}.should == [[], [], 1, 4, 9, 16]
|
120
|
+
end
|
121
|
+
|
122
|
+
it "foldl's" do
|
123
|
+
a = [1,2,3,4]
|
124
|
+
a.foldl(:+).should == a.sum
|
125
|
+
%w[hi there].foldl(:+).should == "hithere"
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
describe Hash do
|
131
|
+
|
132
|
+
before :each do
|
133
|
+
@h = {"key1"=>"val1", "key2"=>"val2"}
|
134
|
+
end
|
135
|
+
|
136
|
+
it "maps keys" do
|
137
|
+
h = @h.map_keys{|k| k.upcase}
|
138
|
+
h.keys.should == @h.keys.map{|k| k.upcase}
|
139
|
+
h.values.should == @h.values
|
140
|
+
|
141
|
+
h.map_keys! { 1 }
|
142
|
+
h.keys.should == [1]
|
143
|
+
end
|
144
|
+
|
145
|
+
it "maps values" do
|
146
|
+
h = @h.map_values{|v| v.upcase}
|
147
|
+
h.values.should == @h.values.map{|v| v.upcase}
|
148
|
+
h.keys.should == @h.keys
|
149
|
+
h.map_values!{ 1 }
|
150
|
+
h.values.should == [1,1]
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
describe BlankSlate do
|
157
|
+
|
158
|
+
it "is blank!" do
|
159
|
+
BlankSlate.methods(false).should == []
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
|
data/spec/lcs_spec.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'epitools/lcs'
|
2
|
+
|
3
|
+
describe "Longest common subsequence" do
|
4
|
+
|
5
|
+
it "works!" do
|
6
|
+
prefix_strings = [
|
7
|
+
"shenanigans, gentlemen!",
|
8
|
+
"shenanigans has been called",
|
9
|
+
"shenanigans: a great restaurant."
|
10
|
+
]
|
11
|
+
|
12
|
+
subsequence_strings = [
|
13
|
+
"i call shenanigans on you!",
|
14
|
+
"shenanigans is a great restaurant.",
|
15
|
+
"you like this? shenanigans!"
|
16
|
+
]
|
17
|
+
|
18
|
+
longest_common_prefix(prefix_strings).should == "shenanigans"
|
19
|
+
longest_common_subsequence(*subsequence_strings[0..1]).should == 12
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'epitools/basetypes'
|
2
|
+
require 'epitools/permutations'
|
3
|
+
|
4
|
+
describe "Permutations" do
|
5
|
+
|
6
|
+
it "a*b" do
|
7
|
+
([1,2] * [3,4]).should == [ [1,3], [1,4], [2,3], [2,4] ]
|
8
|
+
end
|
9
|
+
|
10
|
+
it "a**2" do
|
11
|
+
([1,2] ** 2).should == [ [1,1], [1,2], [2,1], [2,2] ]
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
data/spec/rash_spec.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'epitools/rash'
|
2
|
+
|
3
|
+
describe Rash do
|
4
|
+
|
5
|
+
attr_accessor :r
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
@r = Rash.new(
|
9
|
+
/hello/ => "hello",
|
10
|
+
/world/ => "world",
|
11
|
+
"other" => "whee",
|
12
|
+
true => false,
|
13
|
+
1 => "awesome"
|
14
|
+
# /.+/ => "EVERYTHING"
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "string lookups" do
|
19
|
+
r["other"].should == "whee"
|
20
|
+
r["well hello there"].should == ["hello"]
|
21
|
+
r["the world is round"].should == ["world"]
|
22
|
+
r["hello world"].sort.should == ["hello", "world"]
|
23
|
+
end
|
24
|
+
|
25
|
+
it "regex lookups" do
|
26
|
+
r[/other/].should == ["whee"]
|
27
|
+
end
|
28
|
+
|
29
|
+
it "other objects" do
|
30
|
+
r[true].should == false
|
31
|
+
r[1].should == "awesome"
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: epitools
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- epitron
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-15 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Miscellaneous utility libraries to make my life easier.
|
35
|
+
email: chris@ill-logic.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
43
|
+
files:
|
44
|
+
- .document
|
45
|
+
- .gitignore
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- Rakefile
|
49
|
+
- VERSION
|
50
|
+
- lib/epitools.rb
|
51
|
+
- lib/epitools/basetypes.rb
|
52
|
+
- lib/epitools/hexdump.rb
|
53
|
+
- lib/epitools/http.rb
|
54
|
+
- lib/epitools/lcs.rb
|
55
|
+
- lib/epitools/metaclass.rb
|
56
|
+
- lib/epitools/niceprint.rb
|
57
|
+
- lib/epitools/permutations.rb
|
58
|
+
- lib/epitools/pretty_backtrace.rb
|
59
|
+
- lib/epitools/rails.rb
|
60
|
+
- lib/epitools/rash.rb
|
61
|
+
- spec/basetypes_spec.rb
|
62
|
+
- spec/lcs_spec.rb
|
63
|
+
- spec/metaclass_spec.rb
|
64
|
+
- spec/permutations_spec.rb
|
65
|
+
- spec/spec.opts
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
has_rdoc: true
|
68
|
+
homepage: http://github.com/epitron/epitools
|
69
|
+
licenses: []
|
70
|
+
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options:
|
73
|
+
- --charset=UTF-8
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.3.6
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: NOT UTILS... METILS!
|
97
|
+
test_files:
|
98
|
+
- spec/lcs_spec.rb
|
99
|
+
- spec/rash_spec.rb
|
100
|
+
- spec/metaclass_spec.rb
|
101
|
+
- spec/permutations_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
- spec/basetypes_spec.rb
|