cotcube-helpers 0.1.5.4 → 0.1.7.3

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: 97c255de9c0dac3cad83de38b0b8e1eb21af6d27bdf751d58dcc013b9044a7bc
4
- data.tar.gz: 01cb8859f054baf5c87d55d7b718d8261b48428a59e0e728acbb759855526804
3
+ metadata.gz: 93dafb52f8ac98f974e664aa75c8ffec500d8e97424f4d8018632ebf2593b591
4
+ data.tar.gz: 4ebb3c134a1d4fcf498fbc1514649e072283f8f30071a8c668db7a483801e649
5
5
  SHA512:
6
- metadata.gz: 6358bd447301dd4a001b3e4f3ab6e80cd8d2e3073eef1ef9f05fa5c907bf7fd3a19245bd4ea4667bfaebade359e1dc6ea5b24a8f24cf2a8f43bfbb070b6e2567
7
- data.tar.gz: a8876e8e07211f0648b2e646b721e52912bcf117f5b6d811f744368a86ab5c030a2f7ad6542464486fd6502ce9f7c8c51ff9cc5e04e401f45c9289a69e8eb1f5
6
+ metadata.gz: 4b89d9ba9ace8dea6e88cbefc5caf8a56ea923d2130850eee74e5fca2c888b35994841225ccf96f64bcf73eb650febefb15508704ba01f704afceb49f7efdc15
7
+ data.tar.gz: 0722b12dff36977b75f53988a1563310438ff9ed445b8d563b76fe522e5116f55b291e4bf46924611bf0b5886a71a82f53b14e915b34c0d071e3f0e6112bc06e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ ## 0.1.7.3 (March 13, 2021)
2
+ - array_ext: pairwise and triplewise now support saving result in latter members []=
3
+
4
+ ## 0.1.7.2 (February 01, 2021)
5
+ - adding #deep_freeze to Enumerable
6
+ - range_ext: added #mod to modify an (actually) immutable range
7
+ - simple_series_stats: minor fix
8
+
9
+ ## 0.1.7.1 (January 17, 2021)
10
+ - bugfix
11
+
12
+ ## 0.1.7 (January 17, 2021)
13
+ - added new method 'simple_series_stats'
14
+
15
+ ## 0.1.6 (January 15, 2021)
16
+ - removing :ranges from Range#select_within
17
+ - Added Array#select_right_by
18
+
1
19
  ## 0.1.5.4 (January 02, 2021)
2
20
 
3
21
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5.4
1
+ 0.1.7.3
@@ -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
 
@@ -74,4 +78,24 @@ class Array
74
78
  end.reduce(:|)
75
79
  end
76
80
  end
81
+
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
88
+
89
+ raise ArgumentError, 'No block given.' unless block.is_a? Proc
90
+
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
96
+
97
+ index = find_index { |obj| block.call(obj) }
98
+
99
+ self[((inclusive ? index : index + 1)..)]
100
+ end
77
101
  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,12 +2,21 @@
2
2
 
3
3
  # Monkey patching the Ruby Core class Range
4
4
  class Range
5
- def to_time_intervals(step:, timezone: Time.find_zone('America/Chicago'), ranges: nil)
5
+ def mod(first, last = 0)
6
+ exclude_end? ? ((self.begin + first)...(self.end + last)) : ((self.begin + first)..(self.begin + last))
7
+ end
8
+
9
+ def to_time_intervals(step:,
10
+ timezone: Time.find_zone('America/Chicago'),
11
+ # ranges: nil,
12
+ debug: false)
6
13
  unless step.is_a? ActiveSupport::Duration
7
14
  raise ArgumentError,
8
15
  ":step must be a 'ActiveSupport::Duration', like '15.minutes', but '#{step}' is a '#{step.class}'"
9
16
  end
10
- raise ArgumentError, "Sorry, currently supporting only 15.minutes, 1.hour, 1.day as :step" unless [15.minutes, 60.minutes, 1.hour, 1.day].include? step
17
+ unless [15.minutes, 60.minutes, 1.hour, 1.day].include? step
18
+ raise ArgumentError, 'Sorry, currently supporting only 15.minutes, 1.hour, 1.day as :step'
19
+ end
11
20
 
12
21
  valid_classes = [ActiveSupport::TimeWithZone, Time, Date, DateTime]
13
22
  unless timezone.is_a? ActiveSupport::TimeZone
@@ -30,21 +39,22 @@ class Range
30
39
  # here sub-day and super-day need to be distinguished, as they react differently to daylight time
31
40
  # for super-day, just return an array containing all calendar days
32
41
  if step.to_i >= 1.day
33
- return (starting.to_date..ending.to_date).to_a.map{|x| x.to_datetime}
42
+ (starting.to_date..ending.to_date).to_a.map(&:to_datetime)
34
43
  else
35
44
 
45
+ # sub-day is checked for DST
46
+ # noinspection RubyNilAnalysis
47
+ actual_starting = starting.to_time.to_i
48
+ actual_ending = ending.to_time.to_i
49
+ actual_ending -= 3600 if starting.dst? && (not ending.dst?)
50
+ actual_ending += 3600 if ending.dst? && (not starting.dst?)
51
+
36
52
  ##### The following is the actual big magic line, as it creates the raw target array:
37
53
  #
38
- result = (starting.to_time.to_i..ending.to_time.to_i).step(step).to_a.map { |x| timezone.at(x) }
54
+ result = (actual_starting..actual_ending).step(step).to_a.map { |x| timezone.at(x) }
39
55
  #
40
56
  # ###################<3##
41
57
 
42
-
43
- # sub-day is checked for DST and filtered along provided ranges
44
- # noinspection RubyNilAnalysis
45
- starting_with_dst = result.first.dst?
46
-
47
- # The following lambda is completely misplaces here.
48
58
  # It should probably relocated to Cotcube::Bardata
49
59
  # NOTE: In this current version 12 belongs to it succeeding hour
50
60
  # i.e. 12am is right before 1am and 12pm right before 1pm
@@ -62,26 +72,29 @@ class Range
62
72
  end
63
73
  convert_to_sec_since.call('9:00a.m - 5:00p.m.')
64
74
 
65
- ranges ||= [
66
- 61_200...144_000, # Sun 5pm .. Mon 4pm
67
- 147_600...230_400, # Mon 5pm .. Tue 4pm
68
- 234_000...316_800, # ...
69
- 320_400...403_200,
70
- 406_800...489_600
71
- ]
75
+ # ranges ||= [
76
+ # 61_200...144_000, # Sun 5pm .. Mon 4pm
77
+ # 147_600...230_400, # Mon 5pm .. Tue 4pm
78
+ # 234_000...316_800, # ...
79
+ # 320_400...403_200,
80
+ # 406_800...489_600
81
+ # ]
72
82
 
73
83
  # if there was a change towards daylight saving time, subtract 1 hour, otherwise add 1 hour
74
84
  result.map! do |time|
75
- if (not starting_with_dst) && time.dst?
76
- time - 3600
77
- elsif starting_with_dst && (not time.dst?)
78
- time + 3600
79
- else
80
- time
85
+ print "#{time}\t" if debug
86
+ if (not starting.dst?) && time.dst?
87
+ time -= 3600
88
+ print "Time reduced (not starting_DST, but current\t" if debug
89
+ elsif starting.dst? && (not time.dst?)
90
+ time += 3600
91
+ print "Time extended (starting DST, but not current\t" if debug
81
92
  end
93
+ puts "#{time} " if debug
94
+ time
82
95
  end
83
96
 
84
- result.select_within(ranges: ranges) { |x| x.to_datetime.to_seconds_since_monday_morning }
97
+ result # .select_within(ranges: ranges) { |x| x.to_datetime.to_seconds_since_sunday_morning }
85
98
  end
86
99
  end
87
100
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cotcube
4
+ # Missing top level documentation
5
+ module Helpers
6
+ # if given a block, :ind of base is set by block.call()
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: '% 6.2f', prefix: '', print: true, &block)
9
+ raise ArgumentError, 'Need :ind of type integer' if base.first.is_a?(Array) and not ind.is_a?(Integer)
10
+ raise ArgumentError, 'Need :ind to evaluate base' if base.first.is_a?(Hash) and ind.nil?
11
+
12
+ dim = dim.to_f if dim.is_a? Numeric
13
+ dim = 0 if dim==false
14
+
15
+ raise ArgumentError, 'Expecting 0 <= :dim < 0.5' unless dim.is_a?(Float) and dim >= 0 and dim < 0.5
16
+ raise ArgumentError, 'Expecting arity of one if block given' if block_given? and not block.arity==1
17
+
18
+ precision = format[-1] == 'f' ? format[..-2].split('.').last.to_i : 0
19
+ worker = base.
20
+ tap {|b| b.map{|x| x[ind] = block.call(x) } if block.is_a? Proc }.
21
+ map {|x| ind.nil? ? x : x[ind] }.
22
+ compact.
23
+ sort
24
+ unless dim.zero?
25
+ reductor = (base.size * dim).round
26
+ worker = worker[reductor..base.size - reductor]
27
+ end
28
+ result = {}
29
+
30
+ result[:size] = worker.size
31
+ result[:min] = worker.first
32
+ result[:avg] = (worker.reduce(:+) / result[:size]).round(precision+1)
33
+ result[:lower] = worker[ (result[:size] * 1 / 4).round ]
34
+ result[:median] = worker[ (result[:size] * 2 / 4).round ]
35
+ result[:upper] = worker[ (result[:size] * 3 / 4).round ]
36
+ result[:max] = worker.last
37
+
38
+ output = result.
39
+ reject{|k,_| k == :size}.
40
+ map{|k,v| { type: k, value: v, output: "#{k}: #{format(format, v)}".colorize(k==:avg ? :light_yellow : :white) } }.
41
+ sort_by{|x| x[:value]}.
42
+ map{|x| x[:output]}
43
+ result[:output] = "#{format '%20s',(prefix.empty? ? '' : (prefix + ': '))}" +
44
+ "[" +
45
+ " size: #{format '%6d', result[:size]} | ".light_white +
46
+ output.join(' | ') +
47
+ " ]"
48
+
49
+ puts result[:output] if print
50
+ result
51
+ end
52
+ end
53
+ end
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.5.4
4
+ version: 0.1.7.3
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-02 00:00:00.000000000 Z
11
+ date: 2021-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -90,6 +90,7 @@ files:
90
90
  - lib/cotcube-helpers/range_ext.rb
91
91
  - lib/cotcube-helpers/reduce.rb
92
92
  - lib/cotcube-helpers/simple_output.rb
93
+ - lib/cotcube-helpers/simple_series_stats.rb
93
94
  - lib/cotcube-helpers/string_ext.rb
94
95
  - lib/cotcube-helpers/subpattern.rb
95
96
  - lib/cotcube-helpers/swig/date.rb