cotcube-helpers 0.1.5 → 0.1.7.1

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: 22fa00c18900f6c955d12685727b76560b6a426007d644f489f585d5f1488649
4
- data.tar.gz: ffa3ca8962565929835410d50a60fb878ee782db074bcb7392139acf5bd946b9
3
+ metadata.gz: c53dd160cd7774a8808b28681dd7ad363cb46f7b69ae402d89c190e531060837
4
+ data.tar.gz: 0f0731c06b715e753c73685f7acc967881b616410edde425705f43c38df601fc
5
5
  SHA512:
6
- metadata.gz: 48f5a7982b6ab4bc14cc123dbe49b12ad7e2aa5a664696244f808bb1293b31721315a9ceb098a6b8c57e0f8da6d7940d3becc6296c4184adf53f6ee3faf44d8c
7
- data.tar.gz: 3d640f4fac8b7e2a84985bf00f09a3a34862b1bebf78fac2f03326a72133ca32e4d1f710ead8995ebbb1e50a0f8a24ad3f70b1fc5dc593b9d2e30ab864ad6ebc
6
+ metadata.gz: 3920e5754a1b8ae41f8d0ebe83502d9935766b08da022367694fe0ed4591e7c759378a8badca27e0672851fb7599ad97ceff1986146add4285ce4386d8e5852a
7
+ data.tar.gz: 419275382240a93ad604cb32208b57e1b9e3cbf9db95bd0853b80b0fe4a29c10d162af2a20d801c5cb19fd04296d6519b442d66786e5bc56d82ee4f0da37b841
@@ -1,3 +1,25 @@
1
+ ## 0.1.7.1 (January 17, 2021)
2
+ - bugfix
3
+
4
+ ## 0.1.7 (January 17, 2021)
5
+ - added new method 'simple_series_stats'
6
+
7
+ ## 0.1.6 (January 15, 2021)
8
+ - removing :ranges from Range#select_within
9
+ - Added Array#select_right_by
10
+
11
+ ## 0.1.5.4 (January 02, 2021)
12
+
13
+
14
+ ## 1.5.1.3 (January 02, 2021)
15
+ - hotfixing the hotfix (hello CI tools, c ya coming)
16
+
17
+ ## 1.5.1.2 (January 02, 2021)
18
+ - hotfix problem in Range.to_time_intervals
19
+
20
+ ## 0.1.5.1 (January 02, 2021)
21
+ - Hotfixing parallelize
22
+
1
23
  ## 0.1.5 (January 02, 2021)
2
24
  - applied new datetime helper to Range#to_time_intervals
3
25
  - added new DateTime extension, containing 'to_seconds_since_sunday_morning'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.1.7.1
@@ -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
@@ -7,9 +7,9 @@ module Cotcube
7
7
  def parallelize(ary, processes: 1, threads: 1, progress: '', &block)
8
8
  chunks = []
9
9
  if [0, 1].include? processes
10
- result = Parallel.map(ary, in_threads: threads) { |u, &in_thread| in_thread.call(u) }
11
- elsif [0, 1].include?(threads)
12
- result = Parallel.map(ary, in_processes: processes) { |u, &in_process| in_process.call(u) }
10
+ result = Parallel.map(ary, in_threads: threads, &block)
11
+ elsif [0, 1].include? threads
12
+ result = Parallel.map(ary, in_processes: processes, &block)
13
13
  else
14
14
  ary.each_slice(threads) { |chunk| chunks << chunk }
15
15
  result = if progress == ''
@@ -2,11 +2,17 @@
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 to_time_intervals(step:,
6
+ timezone: Time.find_zone('America/Chicago'),
7
+ # ranges: nil,
8
+ debug: false)
6
9
  unless step.is_a? ActiveSupport::Duration
7
10
  raise ArgumentError,
8
11
  ":step must be a 'ActiveSupport::Duration', like '15.minutes', but '#{step}' is a '#{step.class}'"
9
12
  end
13
+ unless [15.minutes, 60.minutes, 1.hour, 1.day].include? step
14
+ raise ArgumentError, 'Sorry, currently supporting only 15.minutes, 1.hour, 1.day as :step'
15
+ end
10
16
 
11
17
  valid_classes = [ActiveSupport::TimeWithZone, Time, Date, DateTime]
12
18
  unless timezone.is_a? ActiveSupport::TimeZone
@@ -26,63 +32,65 @@ class Range
26
32
  ":self.end seems not to be proper time value: #{ending} is a #{ending.class}"
27
33
  end
28
34
 
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##
35
+ # here sub-day and super-day need to be distinguished, as they react differently to daylight time
36
+ # for super-day, just return an array containing all calendar days
37
+ if step.to_i >= 1.day
38
+ (starting.to_date..ending.to_date).to_a.map(&:to_datetime)
39
+ else
34
40
 
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
41
+ # sub-day is checked for DST
42
+ # noinspection RubyNilAnalysis
43
+ actual_starting = starting.to_time.to_i
44
+ actual_ending = ending.to_time.to_i
45
+ actual_ending -= 3600 if starting.dst? && (not ending.dst?)
46
+ actual_ending += 3600 if ending.dst? && (not starting.dst?)
44
47
 
45
- # sub-day is checked for DST and filtered along provided ranges
46
- # noinspection RubyNilAnalysis
47
- starting_with_dst = result.first.dst?
48
+ ##### The following is the actual big magic line, as it creates the raw target array:
49
+ #
50
+ result = (actual_starting..actual_ending).step(step).to_a.map { |x| timezone.at(x) }
51
+ #
52
+ # ###################<3##
48
53
 
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.*/
54
+ # It should probably relocated to Cotcube::Bardata
55
+ # NOTE: In this current version 12 belongs to it succeeding hour
56
+ # i.e. 12am is right before 1am and 12pm right before 1pm
57
+ convert_to_sec_since = lambda do |clocking|
58
+ from_src, to_src = clocking.split(' - ')
59
+ regex = /^(?<hour>\d+):(?<minute>\d+)(?<morning>[pa]).?m.*/
56
60
 
57
- from = from_src.match(regex)
58
- to = to_src.match(regex)
61
+ from = from_src.match(regex)
62
+ to = to_src.match(regex)
59
63
 
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
64
+ from_i = from[:hour].to_i * 3600 + from[:minute].to_i * 60 + (from[:morning] == 'a' ? 2 : 1) * 12 * 3600
65
+ to_i = to[:hour].to_i * 3600 + to[:minute].to_i * 60 + (to[:morning] == 'a' ? 2 : 3) * 12 * 3600
62
66
 
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.')
67
+ (0...5).to_a.map { |i| [from_i + i * 24 * 3600, to_i + i * 24 * 3600] }
68
+ end
69
+ convert_to_sec_since.call('9:00a.m - 5:00p.m.')
66
70
 
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
- ]
71
+ # ranges ||= [
72
+ # 61_200...144_000, # Sun 5pm .. Mon 4pm
73
+ # 147_600...230_400, # Mon 5pm .. Tue 4pm
74
+ # 234_000...316_800, # ...
75
+ # 320_400...403_200,
76
+ # 406_800...489_600
77
+ # ]
74
78
 
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
79
+ # if there was a change towards daylight saving time, subtract 1 hour, otherwise add 1 hour
80
+ result.map! do |time|
81
+ print "#{time}\t" if debug
82
+ if (not starting.dst?) && time.dst?
83
+ time -= 3600
84
+ print "Time reduced (not starting_DST, but current\t" if debug
85
+ elsif starting.dst? && (not time.dst?)
86
+ time += 3600
87
+ print "Time extended (starting DST, but not current\t" if debug
88
+ end
89
+ puts "#{time} " if debug
82
90
  time
83
91
  end
84
- end
85
92
 
86
- result.select_within(ranges: ranges) { |x| x.to_datetime.to_seconds_since_monday_morning }
93
+ result # .select_within(ranges: ranges) { |x| x.to_datetime.to_seconds_since_sunday_morning }
94
+ end
87
95
  end
88
96
  end
@@ -0,0 +1,55 @@
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
+ puts reductor
27
+ worker = worker[reductor..base.size - reductor]
28
+ puts worker.size
29
+ end
30
+ result = {}
31
+
32
+ result[:size] = worker.size
33
+ result[:min] = worker.first
34
+ result[:avg] = (worker.reduce(:+) / result[:size]).round(precision+1)
35
+ result[:lower] = worker[ (result[:size] * 1 / 4).round ]
36
+ result[:median] = worker[ (result[:size] * 2 / 4).round ]
37
+ result[:upper] = worker[ (result[:size] * 3 / 4).round ]
38
+ result[:max] = worker.last
39
+
40
+ output = result.
41
+ reject{|k,_| k == :size}.
42
+ map{|k,v| { type: k, value: v, output: "#{k}: #{format(format, v)}".colorize(k==:avg ? :light_yellow : :white) } }.
43
+ sort_by{|x| x[:value]}.
44
+ map{|x| x[:output]}
45
+ result[:output] = "#{format '%20s',(prefix.empty? ? '' : (prefix + ': '))}" +
46
+ "[" +
47
+ " size: #{format '%6d', result[:size]} | ".light_white +
48
+ output.join(' | ') +
49
+ " ]"
50
+
51
+ puts result[:output] if print
52
+ result
53
+ end
54
+ end
55
+ 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
+ version: 0.1.7.1
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-01-17 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