cotcube-helpers 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 91263c1bbd453c5b1e6c8a3cfe7c0dee3542f5873e82d15caf8118b6b20ce603
4
- data.tar.gz: f886c9fe330f439b61c0aee1f3c9cc55b3df161511968c25bb92df5cb85c2f0c
3
+ metadata.gz: 32c7ba958cf4ee90e698fbc9b09332917a8fb085234612440f4fe5d013776767
4
+ data.tar.gz: ac8f2b8aac6e5b337f2cc2a3bc1d00c5573c67c8ddfe22fadd8dfeaee4be8a18
5
5
  SHA512:
6
- metadata.gz: '00591395a0289380b4063ffa6766073d7619d1f7cc0576afb722854cbdc97f03d5bd651feaaf6a5f49de61770f6d16982a3ae7de26a9fd030c40efec6198e9f5'
7
- data.tar.gz: c25f08c4677106ec7dc714653830bb6f9ef668f70df50343c443f0d0c79dccd110bf425fe7fa8cdc28b7ecf18db2d3aafd7cccd5f00b2bb5f3a8113fc17f65fa
6
+ metadata.gz: 066b9a5f15172a43f8fb7bba7384a26d930e3ae0cb936103cdae3865331e8670eeff26f2d7a80f84feebfd393c070fb5bc37738d7faa3b1556a6cc3c7b82cbee
7
+ data.tar.gz: ea7b4c464252c118e68cf7e220a60f36d2541e32f3e478a49b921dbbc03d84de34a5d8ca0a33c29e5c7351fec019e284f9fffc3ac133795a36da1d2c7ec2af60
data/CHANGELOG.md CHANGED
@@ -1,3 +1,20 @@
1
+ ## 0.1.8 (April 18, 2021)
2
+ - datetime_ext: Date.cw provides a range of (Date..Date) representing the according calendar week
3
+
4
+ ## 0.1.7.4 (March 13, 2021)
5
+ - hotfix on 0.1.7.3
6
+
7
+ ## 0.1.7.3 (March 13, 2021)
8
+ - array_ext: pairwise and triplewise now support saving result in latter members []=
9
+
10
+ ## 0.1.7.2 (February 01, 2021)
11
+ - adding #deep_freeze to Enumerable
12
+ - range_ext: added #mod to modify an (actually) immutable range
13
+ - simple_series_stats: minor fix
14
+
15
+ ## 0.1.7.1 (January 17, 2021)
16
+ - bugfix
17
+
1
18
  ## 0.1.7 (January 17, 2021)
2
19
  - added new method 'simple_series_stats'
3
20
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.7
1
+ 0.1.8
@@ -16,6 +16,7 @@ require_relative 'cotcube-helpers/datetime_ext'
16
16
  require_relative 'cotcube-helpers/subpattern'
17
17
  require_relative 'cotcube-helpers/parallelize'
18
18
  require_relative 'cotcube-helpers/simple_output'
19
+ require_relative 'cotcube-helpers/simple_series_stats'
19
20
  require_relative 'cotcube-helpers/input'
20
21
  require_relative 'cotcube-helpers/reduce'
21
22
 
@@ -24,6 +25,7 @@ module Cotcube
24
25
  module_function :sub,
25
26
  :parallelize,
26
27
  :reduce,
28
+ :simple_series_stats,
27
29
  :keystroke
28
30
 
29
31
  # please not that module_functions of source provided in private files must be published there
@@ -29,28 +29,32 @@ class Array
29
29
  # This method iterates over an Array by calling the given block on all 2 consecutive elements
30
30
  # it returns a Array of self.size - 1
31
31
  #
32
- def pairwise(&block)
32
+ def pairwise(ret=nil, &block)
33
33
  raise ArgumentError, 'Array.one_by_one needs an arity of 2 (i.e. |a, b|)' unless block.arity == 2
34
+ raise ArgumentError, 'Each element of Array should respond to []=, at least the last one fails.' unless self.last.respond_to?(:[]=)
34
35
  return [] if size <= 1
35
36
 
36
- each_with_index.map do |_, i|
37
+ each_index.map do |i|
37
38
  next if i.zero?
38
39
 
39
- block.call(self[i - 1], self[i])
40
+ r = block.call(self[i - 1], self[i])
41
+ ret.nil? ? r : (self[i][ret] = r)
40
42
  end.compact
41
43
  end
42
44
 
43
45
  alias one_by_one pairwise
44
46
 
45
47
  # same as pairwise, but with arity of three
46
- def triplewise(&block)
48
+ def triplewise(ret=nil, &block)
47
49
  raise ArgumentError, 'Array.triplewise needs an arity of 3 (i.e. |a, b, c|)' unless block.arity == 3
50
+ raise ArgumentError, 'Each element of Array should respond to []=, at least the last one fails.' unless self.last.respond_to?(:[]=)
48
51
  return [] if size <= 2
49
52
 
50
- each_with_index.map do |_, i|
53
+ each_index.map do |i|
51
54
  next if i < 2
52
55
 
53
- block.call(self[i - 2], self[i - 1], self[i])
56
+ r = block.call(self[i - 2], self[i - 1], self[i])
57
+ ret.nil? ? r : (self[i][ret] = r)
54
58
  end.compact
55
59
  end
56
60
 
@@ -61,37 +65,37 @@ class Array
61
65
  unless attr.nil? || first[attr]
62
66
  raise ArgumentError,
63
67
  "At least first element of Array '#{first}' does not contain attr '#{attr}'!"
64
- end
65
- raise ArgumentError, 'Ranges should be an Array or, more precisely, respond_to :map' unless ranges.respond_to? :map
66
- raise ArgumentError, 'Each range in :ranges should respond to .include!' unless ranges.map do |x|
67
- x.respond_to? :include?
68
- end.reduce(:&)
69
-
70
- select do |el|
71
- value = attr.nil? ? el : el[attr]
72
- ranges.map do |range|
73
- range.include?(block.nil? ? value : block.call(value))
74
- end.reduce(:|)
75
- end
76
68
  end
69
+ raise ArgumentError, 'Ranges should be an Array or, more precisely, respond_to :map' unless ranges.respond_to? :map
70
+ raise ArgumentError, 'Each range in :ranges should respond to .include!' unless ranges.map do |x|
71
+ x.respond_to? :include?
72
+ end.reduce(:&)
73
+
74
+ select do |el|
75
+ value = attr.nil? ? el : el[attr]
76
+ ranges.map do |range|
77
+ range.include?(block.nil? ? value : block.call(value))
78
+ end.reduce(:|)
79
+ end
80
+ end
77
81
 
78
- def select_right_by(inclusive: false, exclusive: false, initial: [], &block)
79
- # unless range.is_a? Range and
80
- # (range.begin.nil? or range.begin.is_a?(Integer)) and
81
- # (range.end.nil? or range.end.is_a?(Integer))
82
- # raise ArgumentError, ":range, if given, must be a range of ( nil|Integer..nil|Integer), got '#{range}'"
83
- # end
82
+ def select_right_by(inclusive: false, exclusive: false, initial: [], &block)
83
+ # unless range.is_a? Range and
84
+ # (range.begin.nil? or range.begin.is_a?(Integer)) and
85
+ # (range.end.nil? or range.end.is_a?(Integer))
86
+ # raise ArgumentError, ":range, if given, must be a range of ( nil|Integer..nil|Integer), got '#{range}'"
87
+ # end
84
88
 
85
- raise ArgumentError, 'No block given.' unless block.is_a? Proc
89
+ raise ArgumentError, 'No block given.' unless block.is_a? Proc
86
90
 
87
- inclusive = true unless exclusive
88
- if inclusive && exclusive
89
- raise ArgumentError,
90
- "Either :inclusive or :exclusive must remain falsey, got '#{inclusive}' and '#{exclusive}'"
91
- end
91
+ inclusive = true unless exclusive
92
+ if inclusive && exclusive
93
+ raise ArgumentError,
94
+ "Either :inclusive or :exclusive must remain falsey, got '#{inclusive}' and '#{exclusive}'"
95
+ end
92
96
 
93
- index = find_index { |obj| block.call(obj) }
97
+ index = find_index { |obj| block.call(obj) }
94
98
 
95
- self[((inclusive ? index : index + 1)..)]
96
- end
99
+ self[((inclusive ? index : index + 1)..)]
100
+ end
97
101
  end
@@ -10,3 +10,33 @@ class DateTime
10
10
 
11
11
  alias to_sssm to_seconds_since_sunday_morning
12
12
  end
13
+
14
+ class Date
15
+
16
+ # creates a range of 2 dates, of the given calendar week, Monday to Sunday
17
+ def self.cw( week: , year: Date.today.year )
18
+ form = '%Y %W %w'
19
+ build_range = lambda {|w|
20
+ begin
21
+ ( DateTime.strptime("#{year} #{w} 1", form).to_date..
22
+ DateTime.strptime("#{year} #{w} 0", form).to_date)
23
+ rescue
24
+ # beyond Dec 31st #strptime must be called with cw:0 to keep it working
25
+ ( DateTime.strptime("#{year} #{w} 1", form).to_date..
26
+ DateTime.strptime("#{year+1} 0 0", form).to_date)
27
+ end
28
+ }
29
+ case week
30
+ when :current
31
+ build_range.call(Date.today.cweek)
32
+ when :last
33
+ wday = Date.today.wday
34
+ build_range.call((Date.today - (wday.zero? ? 7 : wday)).cweek)
35
+ when Integer
36
+ raise ArgumentError, "'#{week}' is not supported as calendar week, choose from (1..53)" if week <= 0 or week > 53
37
+ build_range.call(week)
38
+ else
39
+ raise ArgumentError, "'#{week}' is not a supported format for calendar week"
40
+ end
41
+ end
42
+ end
@@ -11,3 +11,25 @@ class Enumerator
11
11
  ret
12
12
  end
13
13
  end
14
+
15
+ # Recursively freeze self if it's Enumerable
16
+ # Supports all Ruby versions 1.8.* to 2.2.*+
17
+ module Kernel
18
+ alias deep_freeze freeze
19
+ alias deep_frozen? frozen?
20
+ end
21
+
22
+ # Adding deep_freeze and deep_frozen?
23
+ module Enumerable
24
+ def deep_freeze
25
+ unless @deep_frozen
26
+ each(&:deep_freeze)
27
+ @deep_frozen = true
28
+ end
29
+ freeze
30
+ end
31
+
32
+ def deep_frozen?
33
+ !!@deep_frozen
34
+ end
35
+ end
@@ -2,6 +2,10 @@
2
2
 
3
3
  # Monkey patching the Ruby Core class Range
4
4
  class Range
5
+ def mod(first, last = 0)
6
+ exclude_end? ? ((self.begin + first)...(self.end + last)) : ((self.begin + first)..(self.begin + last))
7
+ end
8
+
5
9
  def to_time_intervals(step:,
6
10
  timezone: Time.find_zone('America/Chicago'),
7
11
  # ranges: nil,
@@ -5,7 +5,7 @@ module Cotcube
5
5
  module Helpers
6
6
  # if given a block, :ind of base is set by block.call()
7
7
  # dim reduces the sample size by top n% and least n%, so dim of 0.5 would remove 100% of the sample
8
- def simple_series_stats(base:, ind: nil, dim: 0, format: '%5.2f', print: true, &block)
8
+ def simple_series_stats(base:, ind: nil, dim: 0, format: '% 6.2f', prefix: '', print: true, &block)
9
9
  raise ArgumentError, 'Need :ind of type integer' if base.first.is_a?(Array) and not ind.is_a?(Integer)
10
10
  raise ArgumentError, 'Need :ind to evaluate base' if base.first.is_a?(Hash) and ind.nil?
11
11
 
@@ -23,9 +23,7 @@ module Cotcube
23
23
  sort
24
24
  unless dim.zero?
25
25
  reductor = (base.size * dim).round
26
- puts reductor
27
26
  worker = worker[reductor..base.size - reductor]
28
- puts worker.size
29
27
  end
30
28
  result = {}
31
29
 
@@ -37,15 +35,16 @@ module Cotcube
37
35
  result[:upper] = worker[ (result[:size] * 3 / 4).round ]
38
36
  result[:max] = worker.last
39
37
 
40
- result[:output] = result.
38
+ output = result.
41
39
  reject{|k,_| k == :size}.
42
40
  map{|k,v| { type: k, value: v, output: "#{k}: #{format(format, v)}".colorize(k==:avg ? :light_yellow : :white) } }.
43
41
  sort_by{|x| x[:value]}.
44
42
  map{|x| x[:output]}
45
- output = "[" +
46
- " size: #{format '%6d', result[:size]} | ".light_white +
47
- output.join(' | ') +
48
- " ]"
43
+ result[:output] = "#{format '%20s',(prefix.empty? ? '' : (prefix + ': '))}" +
44
+ "[" +
45
+ " size: #{format '%6d', result[:size]} | ".light_white +
46
+ output.join(' | ') +
47
+ " ]"
49
48
 
50
49
  puts result[:output] if print
51
50
  result
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cotcube-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin L. Tischendorf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-17 00:00:00.000000000 Z
11
+ date: 2021-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport