tqdm 0.0.1
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 +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +45 -0
- data/Rakefile +1 -0
- data/lib/core_ext/enumerable.rb +20 -0
- data/lib/tqdm.rb +94 -0
- data/lib/tqdm/sequel.rb +12 -0
- data/lib/tqdm/utils.rb +46 -0
- data/lib/tqdm/version.rb +3 -0
- data/tqdm.gemspec +23 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cd2cc2ae1f682e5a99a462698ec94a5363752ad2
|
4
|
+
data.tar.gz: ee799a36386c9a151fe555e0d856d56bf0650e66
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: be8161fd9956a94aa0bf195b7b1de88c3e063c4438b6ab88ba47019c2948ee1ccb31dfe1442930ac6023a8bdb64feaa9a15368f650c25a819fc39f9390631102
|
7
|
+
data.tar.gz: ae7f1c956f1f10121130909124f4d26794fea5fd94690f6ba8420db3f1b0ac060b7f4728123c4525cd0c1f809445b80d08c7256d078f0297cb9e96fc21945fcc
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Theodore Pak
|
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,45 @@
|
|
1
|
+
# tqdm-ruby
|
2
|
+
|
3
|
+
tqdm-ruby is a small utility to show a progress indicator while iterating through an Enumerable object.
|
4
|
+
|
5
|
+
It is a port of the excellent tdqm library for python: <a href="https://github.com/tqdm/tqdm" target="_blank">https://github.com/tqdm/tqdm</a>.
|
6
|
+
|
7
|
+
Call #tqdm on any `Enumerable`, which enhances the object so that iterating over it will produce an animated progress bar on `$stderr`.
|
8
|
+
|
9
|
+
require 'tqdm'
|
10
|
+
(0...1000).tqdm.each {|x| sleep 0.01 }
|
11
|
+
|
12
|
+
The default output looks like this:
|
13
|
+
|
14
|
+
|####------| 492/1000 49% [elapsed: 00:05 left: 00:05, 88.81 iters/sec]
|
15
|
+
|
16
|
+
It works equally well from within [pry](http://pryrepl.org/) and [Jupyter notebooks](https://jupyter.org/).
|
17
|
+
|
18
|
+
## Install
|
19
|
+
|
20
|
+
Add this line to your application's `Gemfile`:
|
21
|
+
|
22
|
+
gem 'tqdm'
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install tqdm
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
All `Enumerable` objects gain access to the `#tqdm` method, which returns an enhanced object wherein any iteration (by calling `#each` or any of its relatives, e.g., `#each_with_index`, `#each_with_object`, etc.) produces an animated progress bar on $stderr.
|
35
|
+
|
36
|
+
require 'tqdm'
|
37
|
+
(0...100).tqdm.each {|x| sleep 0.01 }
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "tqdm"
|
2
|
+
|
3
|
+
# This is where the real magic begins
|
4
|
+
# All enumerable objects (e.g.) Arrays will have these methods added to them
|
5
|
+
module Enumerable
|
6
|
+
|
7
|
+
# Upgrades an Enumerable so that any subsequent call to .each will spit out a progress bar
|
8
|
+
# opts is a hash that can include:
|
9
|
+
# desc: Short string, describing the progress, added to the beginning of the line
|
10
|
+
# total: Expected number of iterations, if not given, self.size is used
|
11
|
+
# file: A file-like object to output the progress message to, by default, $stderr
|
12
|
+
# leave: A boolean (default False) should the progress bar should stay on screen after it's done?
|
13
|
+
# min_interval: See below
|
14
|
+
# min_iters: If less than min_interval seconds or min_iters iterations have passed since
|
15
|
+
# the last progress meter update, it is not updated again.
|
16
|
+
def tqdm(opts = {})
|
17
|
+
Tqdm::TqdmDecorator.new(self, opts).enhance
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
data/lib/tqdm.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require "tqdm/version"
|
2
|
+
require "tqdm/utils"
|
3
|
+
require "core_ext/enumerable"
|
4
|
+
|
5
|
+
module Tqdm
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def enhance_sequel!
|
9
|
+
require "tqdm/sequel"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class StatusPrinter
|
14
|
+
def initialize(file)
|
15
|
+
@file = file
|
16
|
+
@last_printed_len = 0
|
17
|
+
end
|
18
|
+
|
19
|
+
def print_status(s)
|
20
|
+
@file.write("\r" + s + ' ' * [@last_printed_len - s.size, 0].max)
|
21
|
+
@file.flush
|
22
|
+
@last_printed_len = s.size
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
class TqdmDecorator
|
28
|
+
|
29
|
+
include Utils
|
30
|
+
|
31
|
+
def initialize(enumerable, opts={})
|
32
|
+
@enumerable = enumerable
|
33
|
+
@total = opts[:total] || @enumerable.size rescue nil
|
34
|
+
@prefix = opts[:desc] ? opts[:desc] + ': ' : ''
|
35
|
+
@file = opts[:file] || $stderr
|
36
|
+
@sp = StatusPrinter.new(@file)
|
37
|
+
@min_iters = opts[:min_iters] || 1
|
38
|
+
@min_interval = opts[:min_interval] || 0.5
|
39
|
+
@leave = opts[:leave] || false
|
40
|
+
end
|
41
|
+
|
42
|
+
def start!
|
43
|
+
@start_t = @last_print_t = Time.now
|
44
|
+
@last_print_n = 0
|
45
|
+
@n = 0
|
46
|
+
|
47
|
+
@sp.print_status(@prefix + format_meter(0, @total, 0))
|
48
|
+
end
|
49
|
+
|
50
|
+
def increment!
|
51
|
+
@n += 1
|
52
|
+
|
53
|
+
if @n - @last_print_n >= @min_iters
|
54
|
+
# We check the counter first, to reduce the overhead of time.time()
|
55
|
+
cur_t = Time.now
|
56
|
+
if cur_t - @last_print_t >= @min_interval
|
57
|
+
@sp.print_status(@prefix + format_meter(@n, @total, cur_t - @start_t))
|
58
|
+
@last_print_n = @n
|
59
|
+
@last_print_t = cur_t
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def finish!
|
65
|
+
if !@leave
|
66
|
+
@sp.print_status('')
|
67
|
+
@file.write("\r")
|
68
|
+
else
|
69
|
+
if @last_print_n < @n
|
70
|
+
@sp.print_status(@prefix + format_meter(@n, @total, Time.now - @start_t))
|
71
|
+
end
|
72
|
+
@file.write("\n")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def enhance
|
77
|
+
tqdm = self
|
78
|
+
|
79
|
+
enhanced = @enumerable.clone
|
80
|
+
enhanced.define_singleton_method(:each) do |*args, &block|
|
81
|
+
tqdm.start!
|
82
|
+
super(*args) do |item|
|
83
|
+
block.call item
|
84
|
+
tqdm.increment!
|
85
|
+
end
|
86
|
+
tqdm.finish!
|
87
|
+
end
|
88
|
+
|
89
|
+
enhanced
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
data/lib/tqdm/sequel.rb
ADDED
data/lib/tqdm/utils.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require "tqdm"
|
2
|
+
|
3
|
+
module Tqdm
|
4
|
+
|
5
|
+
N_BARS = 10
|
6
|
+
|
7
|
+
module Utils
|
8
|
+
|
9
|
+
# Formats a number of seconds into an hh:mm:ss string.
|
10
|
+
def format_interval(t)
|
11
|
+
mins, s = t.to_i.divmod(60)
|
12
|
+
h, m = mins.divmod(60)
|
13
|
+
if h > 0 then '%d:%02d:%02d' % [h, m, s]; else '%02d:%02d' % [m, s]; end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Formats a count (n) of total items processed + an elapsed time into a
|
17
|
+
# textual progress meter.
|
18
|
+
def format_meter(n, total, elapsed)
|
19
|
+
# n - number of finished iterations
|
20
|
+
# total - total number of iterations, or nil
|
21
|
+
# elapsed - number of seconds passed since start
|
22
|
+
total = (n > total ? nil : total) if total
|
23
|
+
|
24
|
+
elapsed_str = format_interval(elapsed)
|
25
|
+
rate = elapsed && elapsed > 0 ? ('%5.2f' % (n / elapsed)) : '?'
|
26
|
+
|
27
|
+
if total
|
28
|
+
frac = n.to_f / total
|
29
|
+
|
30
|
+
bar_length = (frac * N_BARS).to_i
|
31
|
+
bar = '#' * bar_length + '-' * (N_BARS - bar_length)
|
32
|
+
|
33
|
+
percentage = '%3d%%' % (frac * 100)
|
34
|
+
|
35
|
+
left_str = n > 0 ? (format_interval(elapsed / n * (total - n))) : '?'
|
36
|
+
|
37
|
+
'|%s| %d/%d %s [elapsed: %s left: %s, %s iters/sec]' % [bar, n, total,
|
38
|
+
percentage, elapsed_str, left_str, rate]
|
39
|
+
else
|
40
|
+
'%d [elapsed: %s, %s iters/sec]' % [n, elapsed_str, rate]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
data/lib/tqdm/version.rb
ADDED
data/tqdm.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tqdm/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tqdm"
|
8
|
+
spec.version = Tqdm::VERSION
|
9
|
+
spec.authors = ["Theodore Pak"]
|
10
|
+
spec.email = ["theodore.r.pak@gmail.com"]
|
11
|
+
spec.description = %q{Enhances Enumerables to show progress while iterating. (Port of tqdm for Python.)}
|
12
|
+
spec.summary = %q{Enhances Enumerables to show progress while iterating.}
|
13
|
+
spec.homepage = "https://github.com/powerpak/tqdm-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tqdm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Theodore Pak
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-20 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Enhances Enumerables to show progress while iterating. (Port of tqdm
|
42
|
+
for Python.)
|
43
|
+
email:
|
44
|
+
- theodore.r.pak@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/core_ext/enumerable.rb
|
55
|
+
- lib/tqdm.rb
|
56
|
+
- lib/tqdm/sequel.rb
|
57
|
+
- lib/tqdm/utils.rb
|
58
|
+
- lib/tqdm/version.rb
|
59
|
+
- tqdm.gemspec
|
60
|
+
homepage: https://github.com/powerpak/tqdm-ruby
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.2.5
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Enhances Enumerables to show progress while iterating.
|
84
|
+
test_files: []
|