sample_by_rate 0.9.0
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 +7 -0
- data/.gitignore +14 -0
- data/.travis.yml +16 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +2 -0
- data/lib/sample_by_rate.rb +11 -0
- data/lib/sample_by_rate/core_ext.rb +5 -0
- data/lib/sample_by_rate/version.rb +3 -0
- data/sample_by_rate.gemspec +24 -0
- data/test/sample_by_rate_test.rb +60 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 45013c262bfab1a28ebbf9be87f45b504eca9913
|
4
|
+
data.tar.gz: 6b7c13006c4053668cf146c48843da89f0e76590
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 32b8b47f60af4d7ad73bbec17bc088e23a5684ec8e8b67e721864919726ce98cf6dc27a380d1e397927abf68f013df57bb471dc7d5eeb4976e2ed656bd6d9ecc
|
7
|
+
data.tar.gz: cbfb862eb3d66986d612d5a85cc06966fe47c81a3dae236e285979dd6c1e1583364793aa10d99270c335e71cb89cd4839c5515ece305389cb10a7aa9e4e03dd9
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Yuya Takeyama
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# SampleByRate
|
2
|
+
|
3
|
+
Method to do sampling for `Enumerable` objects in specified rate.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# Prints 10% of numbers in (1..100000)
|
7
|
+
(1..100000).sample_by_rate(0.1).each do |n|
|
8
|
+
puts n
|
9
|
+
end
|
10
|
+
```
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'sample_by_rate'
|
18
|
+
```
|
19
|
+
|
20
|
+
Or if you want to import `sample_by_rate` method into `Enumerable` module by default, Add this line.
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'sample_by_rate', require: 'sample_by_rate/core_ext'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install sample_by_rate
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
### Importing sample_by_rate method into some Enumerable class
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class SomeEnumerableClass
|
40
|
+
include SampleByRate
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
### Importing sample_by_rate method into Enumerable module by default
|
45
|
+
|
46
|
+
Recommended way is to specify in `Gemfile` which explained in `Installation` section above.
|
47
|
+
|
48
|
+
Or do like below manually.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
require 'sample_by_rate/core_ext'
|
52
|
+
```
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it ( https://github.com/yuya-takeyama/sample_by_rate/fork )
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "sample_by_rate/version"
|
2
|
+
|
3
|
+
module SampleByRate
|
4
|
+
def sample_by_rate(rate)
|
5
|
+
raise 'rate must greater than or equal to 0 and less than or equal to 1' unless rate >= 0 and rate <= 1
|
6
|
+
|
7
|
+
return enum_for(__method__, rate) unless block_given?
|
8
|
+
|
9
|
+
each {|n| yield n if rand < rate }
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sample_by_rate/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sample_by_rate"
|
8
|
+
spec.version = SampleByRate::VERSION
|
9
|
+
spec.authors = ["Yuya Takeyama"]
|
10
|
+
spec.email = ["sign.of.the.wolf.pentagram@gmail.com"]
|
11
|
+
spec.summary = %q{Method to do sampling for Enumerable objects in specified rate.}
|
12
|
+
spec.description = %q{Method to do sampling for Enumerable objects in specified rate.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "test-unit", "~> 3.0.1"
|
24
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
lib = File.expand_path('../../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sample_by_rate'
|
5
|
+
|
6
|
+
class TestSampleByRate < Test::Unit::TestCase
|
7
|
+
sub_test_case '#sample_by_rate' do
|
8
|
+
sub_test_case 'without block' do
|
9
|
+
setup do
|
10
|
+
@range = (1..100000)
|
11
|
+
@range.extend SampleByRate
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'do sampling correctly' do
|
15
|
+
assert_maybe_sampled_correctly @range.sample_by_rate(0.1), 10000
|
16
|
+
assert_maybe_sampled_correctly @range.sample_by_rate(0.5), 50000
|
17
|
+
assert_maybe_sampled_correctly @range.sample_by_rate(0.9), 90000
|
18
|
+
assert_maybe_sampled_correctly @range.sample_by_rate(Rational(1, 3)), 33333
|
19
|
+
end
|
20
|
+
|
21
|
+
test 'with argument 0' do
|
22
|
+
assert_equal 0, size_of_enumerable(@range.sample_by_rate(0))
|
23
|
+
end
|
24
|
+
|
25
|
+
test 'with argument 1' do
|
26
|
+
assert_equal 100000, size_of_enumerable(@range.sample_by_rate(1))
|
27
|
+
end
|
28
|
+
|
29
|
+
def assert_maybe_sampled_correctly(sampled, expected_size)
|
30
|
+
size = size_of_enumerable(sampled)
|
31
|
+
|
32
|
+
assert size >= (expected_size * 0.95)
|
33
|
+
assert size <= (expected_size * 1.05)
|
34
|
+
end
|
35
|
+
|
36
|
+
def size_of_enumerable(enumerable)
|
37
|
+
enumerable.reduce(0) {|acc, n| acc + 1 }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
sub_test_case 'invalid argument' do
|
42
|
+
setup do
|
43
|
+
@range = (1..10)
|
44
|
+
@range.extend SampleByRate
|
45
|
+
end
|
46
|
+
|
47
|
+
test '-1 is invalid argument' do
|
48
|
+
assert_raise do
|
49
|
+
@range.sample_by_rate -1
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
test '1.1 is invalid argument' do
|
54
|
+
assert_raise do
|
55
|
+
@range.sample_by_rate 1.1
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sample_by_rate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuya Takeyama
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: test-unit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.0.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.0.1
|
55
|
+
description: Method to do sampling for Enumerable objects in specified rate.
|
56
|
+
email:
|
57
|
+
- sign.of.the.wolf.pentagram@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/sample_by_rate.rb
|
69
|
+
- lib/sample_by_rate/core_ext.rb
|
70
|
+
- lib/sample_by_rate/version.rb
|
71
|
+
- sample_by_rate.gemspec
|
72
|
+
- test/sample_by_rate_test.rb
|
73
|
+
homepage: ''
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.2.2
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Method to do sampling for Enumerable objects in specified rate.
|
97
|
+
test_files:
|
98
|
+
- test/sample_by_rate_test.rb
|