cotcube-helpers 0.1.5.1 → 0.1.7.2

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: e3d088e43fd870ff67a238501fd680f1c5688c7108bc90ace92e01cd8c33627b
4
- data.tar.gz: 72b07a1f0e502a29e9cb818d62a8d398fe5e094efade358300eab4578b64ceae
3
+ metadata.gz: 273a697c87680d0c695c1617caa490bb89f7712688c60a772e109ccda91fc622
4
+ data.tar.gz: c3f91ba72a275068527f0fdc9dc59026ecffacd63de1466df9a6df9b39c1104c
5
5
  SHA512:
6
- metadata.gz: 7b92ec6b178328bd6944f61828848d1d1c4aa164e9a3680054345189503d237a809ea082441eeab476ff693eeebdf5bf27bec300b3606af589598da946ce1c0a
7
- data.tar.gz: 00d63e82c5ed3cfcc9986f4ec3222e2282a692ed0ad54602cb4e212b9285d8c51182d9c87a4e8b9672d2f305409b6f01d27437b73aa40df6872a35e4ea962b8e
6
+ metadata.gz: 6017fee6976dbb5c9306fb783df42174bf0d88a6df33d7d4a3647f744d484358257380297970faf2c14897c506aa6b9e6dd383399f21a623e993572e50c76bf0
7
+ data.tar.gz: 1f39b5c5ac1002e1596f6d9ca79c73524fce4ba574d4fec594912f4f37f14ec4e0bb53ef57d60b2b648e95c1c8d7193fa0e3b47a6262c11d882eea228c7a3509
data/CHANGELOG.md CHANGED
@@ -1,3 +1,27 @@
1
+ ## 0.1.7.2 (February 01, 2021)
2
+ - adding #deep_freeze to Enumerable
3
+ - range_ext: added #mod to modify an (actually) immutable range
4
+ - simple_series_stats: minor fix
5
+
6
+ ## 0.1.7.1 (January 17, 2021)
7
+ - bugfix
8
+
9
+ ## 0.1.7 (January 17, 2021)
10
+ - added new method 'simple_series_stats'
11
+
12
+ ## 0.1.6 (January 15, 2021)
13
+ - removing :ranges from Range#select_within
14
+ - Added Array#select_right_by
15
+
16
+ ## 0.1.5.4 (January 02, 2021)
17
+
18
+
19
+ ## 1.5.1.3 (January 02, 2021)
20
+ - hotfixing the hotfix (hello CI tools, c ya coming)
21
+
22
+ ## 1.5.1.2 (January 02, 2021)
23
+ - hotfix problem in Range.to_time_intervals
24
+
1
25
  ## 0.1.5.1 (January 02, 2021)
2
26
  - Hotfixing parallelize
3
27
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5.1
1
+ 0.1.7.2
@@ -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
@@ -74,4 +74,24 @@ class Array
74
74
  end.reduce(:|)
75
75
  end
76
76
  end
77
+
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
84
+
85
+ raise ArgumentError, 'No block given.' unless block.is_a? Proc
86
+
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
92
+
93
+ index = find_index { |obj| block.call(obj) }
94
+
95
+ self[((inclusive ? index : index + 1)..)]
96
+ end
77
97
  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,11 +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
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
10
20
 
11
21
  valid_classes = [ActiveSupport::TimeWithZone, Time, Date, DateTime]
12
22
  unless timezone.is_a? ActiveSupport::TimeZone
@@ -26,63 +36,65 @@ class Range
26
36
  ":self.end seems not to be proper time value: #{ending} is a #{ending.class}"
27
37
  end
28
38
 
29
- ##### The following is the actual big magic line, as it creates the raw target array:
30
- #
31
- result = (starting.to_time.to_i..ending.to_time.to_i).step(step).to_a.map { |x| timezone.at(x) }
32
- #
33
- # ###################<3##
39
+ # here sub-day and super-day need to be distinguished, as they react differently to daylight time
40
+ # for super-day, just return an array containing all calendar days
41
+ if step.to_i >= 1.day
42
+ (starting.to_date..ending.to_date).to_a.map(&:to_datetime)
43
+ else
34
44
 
35
- # with step.to_i >= 86400 we are risking stuff like 25.hours to return bogus
36
- # also notice: When using this with swaps, you will loose 1 hour (#f**k_it)
37
- #
38
- # eventually, for dailies and above, return M-F default, return S-S when forced by empty ranges
39
- if step.to_i >= 86_400
40
- return result.select do |x|
41
- (not ranges.nil?) && ranges.empty? ? true : (not [6, 0].include?(x.wday))
42
- end
43
- end
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?)
44
51
 
45
- # sub-day is checked for DST and filtered along provided ranges
46
- # noinspection RubyNilAnalysis
47
- starting_with_dst = result.first.dst?
52
+ ##### The following is the actual big magic line, as it creates the raw target array:
53
+ #
54
+ result = (actual_starting..actual_ending).step(step).to_a.map { |x| timezone.at(x) }
55
+ #
56
+ # ###################<3##
48
57
 
49
- # The following lambda is completely misplaces here.
50
- # It should probably relocated to Cotcube::Bardata
51
- # NOTE: In this current version 12 belongs to it succeeding hour
52
- # i.e. 12am is right before 1am and 12pm right before 1pm
53
- convert_to_sec_since = lambda do |clocking|
54
- from_src, to_src = clocking.split(' - ')
55
- regex = /^(?<hour>\d+):(?<minute>\d+)(?<morning>[pa]).?m.*/
58
+ # It should probably relocated to Cotcube::Bardata
59
+ # NOTE: In this current version 12 belongs to it succeeding hour
60
+ # i.e. 12am is right before 1am and 12pm right before 1pm
61
+ convert_to_sec_since = lambda do |clocking|
62
+ from_src, to_src = clocking.split(' - ')
63
+ regex = /^(?<hour>\d+):(?<minute>\d+)(?<morning>[pa]).?m.*/
56
64
 
57
- from = from_src.match(regex)
58
- to = to_src.match(regex)
65
+ from = from_src.match(regex)
66
+ to = to_src.match(regex)
59
67
 
60
- from_i = from[:hour].to_i * 3600 + from[:minute].to_i * 60 + (from[:morning] == 'a' ? 2 : 1) * 12 * 3600
61
- to_i = to[:hour].to_i * 3600 + to[:minute].to_i * 60 + (to[:morning] == 'a' ? 2 : 3) * 12 * 3600
68
+ from_i = from[:hour].to_i * 3600 + from[:minute].to_i * 60 + (from[:morning] == 'a' ? 2 : 1) * 12 * 3600
69
+ to_i = to[:hour].to_i * 3600 + to[:minute].to_i * 60 + (to[:morning] == 'a' ? 2 : 3) * 12 * 3600
62
70
 
63
- (0...5).to_a.map { |i| [from_i + i * 24 * 3600, to_i + i * 24 * 3600] }
64
- end
65
- convert_to_sec_since.call('9:00a.m - 5:00p.m.')
71
+ (0...5).to_a.map { |i| [from_i + i * 24 * 3600, to_i + i * 24 * 3600] }
72
+ end
73
+ convert_to_sec_since.call('9:00a.m - 5:00p.m.')
66
74
 
67
- ranges ||= [
68
- 61_200...144_000, # Sun 5pm .. Mon 4pm
69
- 147_600...230_400, # Mon 5pm .. Tue 4pm
70
- 234_000...316_800, # ...
71
- 320_400...403_200,
72
- 406_800...489_600
73
- ]
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
+ # ]
74
82
 
75
- # if there was a change towards daylight saving time, subtract 1 hour, otherwise add 1 hour
76
- result.map! do |time|
77
- if (not starting_with_dst) && time.dst?
78
- time - 3600
79
- elsif starting_with_dst && (not time.dst?)
80
- time + 3600
81
- else
83
+ # if there was a change towards daylight saving time, subtract 1 hour, otherwise add 1 hour
84
+ result.map! do |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
92
+ end
93
+ puts "#{time} " if debug
82
94
  time
83
95
  end
84
- end
85
96
 
86
- 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 }
98
+ end
87
99
  end
88
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.1
4
+ version: 0.1.7.2
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-02-01 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