dimsum 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +31 -0
- data/Rakefile +2 -0
- data/bin/dimsum +39 -0
- data/dimsum.gemspec +19 -0
- data/lib/dimsum.rb +5 -0
- data/lib/dimsum/version.rb +3 -0
- metadata +66 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Aaron Olson, Steven Noble, Camilo Lopez
|
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,31 @@
|
|
1
|
+
# Dimsum
|
2
|
+
|
3
|
+
Dimsum takes a file and a number of lines and prints a reservoir sample of the
|
4
|
+
file to stadard output
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'dimsum'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install dimsum
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
```sh
|
22
|
+
sample.rb filename
|
23
|
+
-n, --number [Fixnum]
|
24
|
+
```
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/dimsum
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
options = {}
|
6
|
+
|
7
|
+
OptionParser.new do |opts|
|
8
|
+
opts.banner = "Usage: sample.rb [options]"
|
9
|
+
opts.on("-n [Fixnum]", "--number [Fixnum]", "") do |n|
|
10
|
+
options[:lines] = n
|
11
|
+
end
|
12
|
+
end.parse!
|
13
|
+
|
14
|
+
filename = ARGV[0]
|
15
|
+
lines = options[:lines].to_i
|
16
|
+
raise ArgumentError, "The filename is required" unless filename
|
17
|
+
|
18
|
+
file_size = `wc -l #{filename}`.strip.to_i
|
19
|
+
|
20
|
+
last_2 = `tail -c2 #{filename}`
|
21
|
+
if last_2 == "\n\n"
|
22
|
+
file_size -= 1
|
23
|
+
end
|
24
|
+
|
25
|
+
random = Random.new
|
26
|
+
|
27
|
+
File.open(filename, "r").lines do |line|
|
28
|
+
if file_size == 0
|
29
|
+
next
|
30
|
+
end
|
31
|
+
r = random.rand(0 .. file_size - 1)
|
32
|
+
if r < lines
|
33
|
+
STDOUT.print line
|
34
|
+
STDOUT.flush
|
35
|
+
lines -= 1
|
36
|
+
end
|
37
|
+
file_size -= 1
|
38
|
+
end
|
39
|
+
|
data/dimsum.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/dimsum/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Aaron Olson", "Steven Noble", "Camilo Lopez"]
|
6
|
+
gem.email = ["aaron.olson@shopify.com", "steven.noble@shopify.com", "camilo.lopez@shopify.com"]
|
7
|
+
gem.description = %q{dimsum is a very simple ruby script that performs reservoir sampling on the input file.}
|
8
|
+
gem.summary = %q{reservoir sampling}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "dimsum"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.bindir = 'bin'
|
17
|
+
gem.executables << 'dimsum'
|
18
|
+
gem.version = Dimsum::VERSION
|
19
|
+
end
|
data/lib/dimsum.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dimsum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Aaron Olson
|
9
|
+
- Steven Noble
|
10
|
+
- Camilo Lopez
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2012-06-30 00:00:00.000000000 Z
|
15
|
+
dependencies: []
|
16
|
+
description: dimsum is a very simple ruby script that performs reservoir sampling
|
17
|
+
on the input file.
|
18
|
+
email:
|
19
|
+
- aaron.olson@shopify.com
|
20
|
+
- steven.noble@shopify.com
|
21
|
+
- camilo.lopez@shopify.com
|
22
|
+
executables:
|
23
|
+
- dimsum
|
24
|
+
extensions: []
|
25
|
+
extra_rdoc_files: []
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- Gemfile
|
29
|
+
- LICENSE
|
30
|
+
- README.md
|
31
|
+
- Rakefile
|
32
|
+
- bin/dimsum
|
33
|
+
- dimsum.gemspec
|
34
|
+
- lib/dimsum.rb
|
35
|
+
- lib/dimsum/version.rb
|
36
|
+
homepage: ''
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
hash: -1291815841305852171
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
hash: -1291815841305852171
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.8.11
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: reservoir sampling
|
66
|
+
test_files: []
|