cadence 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.
- data/README.textile +47 -0
- data/Rakefile +5 -0
- data/lib/cadence/computer/counter.rb +25 -0
- data/lib/cadence/computer.rb +69 -0
- data/lib/cadence.rb +5 -0
- data/test/cadence_test.rb +32 -0
- metadata +72 -0
data/README.textile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
h1. Cadence
|
2
|
+
|
3
|
+
Track counts and compute rate of iteration. Set up callbacks for various
|
4
|
+
intervals such as @every@ n increments or every n @ticks@.
|
5
|
+
|
6
|
+
h2. Sample
|
7
|
+
|
8
|
+
<pre>
|
9
|
+
require 'lib/cadence'
|
10
|
+
|
11
|
+
data = (1..100).to_a
|
12
|
+
|
13
|
+
computer = Cadence::Computer.new do |c|
|
14
|
+
c.every 5 do
|
15
|
+
p [:every, 5, n]
|
16
|
+
end
|
17
|
+
|
18
|
+
c.for(:tens).every do
|
19
|
+
p [:tens, :every, n]
|
20
|
+
end
|
21
|
+
|
22
|
+
c.twenties.every 2 do
|
23
|
+
p [:twenties, :every, 2, n]
|
24
|
+
end
|
25
|
+
|
26
|
+
c.ticks 1 do
|
27
|
+
p [:tock, n, timestamp]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
computer.start do |c|
|
32
|
+
data.each do |n|
|
33
|
+
c.next
|
34
|
+
c.tens.next if n % 10 == 0
|
35
|
+
p [:n, n]
|
36
|
+
sleep 0.02
|
37
|
+
end
|
38
|
+
end
|
39
|
+
</pre>
|
40
|
+
|
41
|
+
h2. License
|
42
|
+
|
43
|
+
MIT
|
44
|
+
|
45
|
+
h2. Copyright
|
46
|
+
|
47
|
+
Copyright (C) 2010 Matt Todd.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
class Cadence::Computer::Counter
|
2
|
+
|
3
|
+
attr_accessor :count, :triggers
|
4
|
+
|
5
|
+
def initialize(count = 0)
|
6
|
+
@count = 0
|
7
|
+
@triggers = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def every(n = 1, &block)
|
11
|
+
@triggers[n] = block
|
12
|
+
end
|
13
|
+
|
14
|
+
def next
|
15
|
+
@count += 1
|
16
|
+
@triggers.each do |(n, trigger)|
|
17
|
+
instance_eval(&trigger) if @count % n == 0
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def n
|
22
|
+
@count
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
class Cadence::Computer
|
2
|
+
|
3
|
+
attr_accessor :started_at, :finished_at, :duration,
|
4
|
+
:counter, :counters, :total
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@counter = Counter.new
|
8
|
+
@counters = {}
|
9
|
+
@ticks = {}
|
10
|
+
|
11
|
+
yield self
|
12
|
+
end
|
13
|
+
|
14
|
+
### Keys
|
15
|
+
|
16
|
+
def for(key)
|
17
|
+
self.counters[key] ||= Counter.new
|
18
|
+
end
|
19
|
+
def method_missing(method_name, *args, &block)
|
20
|
+
self.for(method_name, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
### Overall total and count tracking
|
24
|
+
|
25
|
+
def every(n = 1, &block)
|
26
|
+
@counter.every(n, &block)
|
27
|
+
end
|
28
|
+
def next
|
29
|
+
@counter.next
|
30
|
+
@ticks.each do |(n, (last, tick))|
|
31
|
+
@ticks[n] = [self.duration.to_i, tick]
|
32
|
+
instance_eval(&tick) if self.duration.to_i > last.to_i && self.duration.to_i % n == 0
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
### Timing
|
37
|
+
|
38
|
+
def ticks(n = 1, &block)
|
39
|
+
@ticks[n] ||= [nil, block]
|
40
|
+
end
|
41
|
+
|
42
|
+
### Runtime
|
43
|
+
|
44
|
+
def start
|
45
|
+
@started_at = self.timestamp
|
46
|
+
yield self
|
47
|
+
@finished_at = self.timestamp
|
48
|
+
|
49
|
+
@duration = @finished_at - @started_at
|
50
|
+
end
|
51
|
+
|
52
|
+
def timestamp
|
53
|
+
Time.now
|
54
|
+
end
|
55
|
+
|
56
|
+
def duration
|
57
|
+
self.timestamp.to_f - self.started_at.to_f
|
58
|
+
end
|
59
|
+
|
60
|
+
def rate
|
61
|
+
# self.duration / self.counter.count
|
62
|
+
self.counter.count / self.duration
|
63
|
+
end
|
64
|
+
|
65
|
+
def n
|
66
|
+
self.counter.count
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
data/lib/cadence.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..'))
|
2
|
+
require 'lib/cadence'
|
3
|
+
|
4
|
+
data = (1..100).to_a
|
5
|
+
|
6
|
+
computer = Cadence::Computer.new do |c|
|
7
|
+
c.every 5 do
|
8
|
+
p [:every, 5, n]
|
9
|
+
end
|
10
|
+
|
11
|
+
c.for(:tens).every do
|
12
|
+
p [:tens, :every, n]
|
13
|
+
end
|
14
|
+
|
15
|
+
c.twenties.every 2 do
|
16
|
+
p [:twenties, :every, 2, n]
|
17
|
+
end
|
18
|
+
|
19
|
+
c.ticks 1 do
|
20
|
+
p [:tock, n, timestamp]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
computer.start do |c|
|
25
|
+
data.each do |n|
|
26
|
+
c.next
|
27
|
+
c.tens.next if n % 10 == 0
|
28
|
+
c.twenties.next if n % 20 == 0
|
29
|
+
p [:n, n]
|
30
|
+
sleep 0.02
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cadence
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matt Todd
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-01 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: " Track counts and compute rate of iteration. Set up callbacks for various\n intervals such as every n increments or every n ticks.\n \n computer = Cadence::Computer.new do |c|\n c.every 5 do\n p [:completed_processing, n]\n end\n end\n \n computer.start do |c|\n 1.upto(100) do |n|\n c.next\n # do magic here\n end\n end\n \n Mostly intended for providing intermitent feedback of the progress of tasks\n that will run for lengthy periods of time.\n \n Rudimentary support for time-based callbacks is possible through #ticks.\n"
|
23
|
+
email: mtodd@highgroove.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- README.textile
|
32
|
+
- Rakefile
|
33
|
+
- lib/cadence/computer/counter.rb
|
34
|
+
- lib/cadence/computer.rb
|
35
|
+
- lib/cadence.rb
|
36
|
+
- test/cadence_test.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: http://github.com/mtodd/cadence
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
hash: 3
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.7
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Interval/period callback library
|
71
|
+
test_files: []
|
72
|
+
|