cotcube-helpers 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +101 -0
- data/VERSION +1 -1
- data/lib/cotcube-helpers/parallelize.rb +11 -14
- data/lib/cotcube-helpers/range_ext.rb +3 -3
- data/lib/cotcube-helpers/subpattern.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01aae33281c410de2818d90ab99e2125c5841ad2f6148a71c7950eb84bb07c33
|
4
|
+
data.tar.gz: 1e4e1fbeaa9d7a3b71bff8fd0325f77ab390620ddd933992b6a138db48fbd77d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e15e9005df3ed00b77b90f12d99b67b92838034a2339ff9255b7c4986cbcd7764ed7fc01bf4de5edcd1032bd885d39de4d045529bc34a6826a6e97c9b954cdf
|
7
|
+
data.tar.gz: edf104056c0a4ea83bf3384a4f5c10ed240f6638e04da12053a84322853d036b1f38bb75ddf96146a195bf2861be9f999bf743a80ff2a14968f5dbdc36b7d2a9
|
data/CHANGELOG.md
CHANGED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Benjamin L. Tischendorf
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
## Cotcube::Helpers
|
2
|
+
|
3
|
+
Just a collection of helpers not fitting into other repositories of the Cotcube suite. Also usable aside of Cotcube. Where appropriate, these are provided as core\_extensions, otherwise within the model Cotcube::Helpers
|
4
|
+
|
5
|
+
#### Extending Array
|
6
|
+
|
7
|
+
##### compact\_or\_nil
|
8
|
+
|
9
|
+
Returns the array compacted--or just nil, when the result is empty?
|
10
|
+
|
11
|
+
##### split\_by(attr)
|
12
|
+
|
13
|
+
This is a rather old implementation. Most probably something I developed before I met Array#group\_by.
|
14
|
+
|
15
|
+
##### pairwise(&block) / triplewise(&block)
|
16
|
+
|
17
|
+
Yields block on each consecutive pair of the array, hence returning array.size-1 results.
|
18
|
+
|
19
|
+
##### triplewise(&block)
|
20
|
+
|
21
|
+
Yields block on each consecutive triple of the array, hence returning array.size-2 results.
|
22
|
+
|
23
|
+
#### Extending Enumerator
|
24
|
+
|
25
|
+
##### shy\_peek
|
26
|
+
|
27
|
+
Peeks into the successor without raising if there is none available--returning nil instead.
|
28
|
+
|
29
|
+
#### Extending Hash
|
30
|
+
|
31
|
+
##### keys\_to\_sym
|
32
|
+
|
33
|
+
Transforms the keys of a Hash (recursivly for subsequent Arrays and Hashes) into symbols.
|
34
|
+
|
35
|
+
#### Extending Range
|
36
|
+
|
37
|
+
##### to\_time\_intervals(timezone: Time.find\_zone('America/Chicago'), step:, ranges: nil)
|
38
|
+
|
39
|
+
Uses the range of *date alikes* delivered to create an Array of time periods of length :step
|
40
|
+
(which is a ActiveSupport::Duration, e.g. 5.minutes or 1.hour).
|
41
|
+
|
42
|
+
When the step is sub-day, the periods are cleared for DST-changes.
|
43
|
+
|
44
|
+
When ranges are nil, only periods are returned that are within the full trading hours. This is
|
45
|
+
accomplished by the fact, that Sunday is wday==0. If you want to use ranges, just send a an array
|
46
|
+
of ranges, providing seconds starting at Sunday morning midnight. Here is the default as example:
|
47
|
+
|
48
|
+
```
|
49
|
+
ranges ||= [
|
50
|
+
61200..143999, # Sun 5pm .. Mon 4pm
|
51
|
+
147600..230399, # Mon 5pm .. Tue 4pm
|
52
|
+
234000..316799, # ...
|
53
|
+
320400..403199,
|
54
|
+
406800..489599
|
55
|
+
]
|
56
|
+
```
|
57
|
+
|
58
|
+
#### Extending String
|
59
|
+
|
60
|
+
##### is\_valid\_json?
|
61
|
+
|
62
|
+
#### Subpattern
|
63
|
+
|
64
|
+
##### sub(minimum: 1) { [:hyper, :mega] }
|
65
|
+
|
66
|
+
sub (should be 'subpattern', but too long) is for use in case / when statements
|
67
|
+
it returns a lambda, that checks the case'd expression for matching subpattern
|
68
|
+
based on the the giving minimum. E.g. 'a', 'ab' .. 'abcd' will match sub(1){'abcd'}
|
69
|
+
but only 'abc' and 'abcd' will match sub(3){'abcd'}
|
70
|
+
|
71
|
+
The recommended use within evaluating user input, where abbreviation of incoming commands
|
72
|
+
is desirable (h for hoover and hyper, what will translate to sub(2){'hoover'} and sub(2){hyper})
|
73
|
+
|
74
|
+
To extend functionality even more, it is possible to send a group of patterns to, like
|
75
|
+
sub(2){[:hyper,:mega]}, what will respond truthy to "hy" and "meg" but not to "m" or "hypo"
|
76
|
+
|
77
|
+
*paired with keystroke() it allows an easy build of an inputhandler*
|
78
|
+
|
79
|
+
#### SimpleOutput
|
80
|
+
|
81
|
+
This is a very simple class, that mocks a more complex output handler. It provides puts, print,
|
82
|
+
puts! and print!. The actual OutputHandler is another project that needs to be rewritten. Once
|
83
|
+
that is done, SimpleOutput will be replaced. The new OutputHandler is a tool to handle information
|
84
|
+
flow like logs, to pause and continue output.
|
85
|
+
|
86
|
+
#### Input
|
87
|
+
|
88
|
+
##### keystroke(quit: false)
|
89
|
+
|
90
|
+
A version of STDIN.gets, that does not wait for pressing 'enter' but instantly returns the content
|
91
|
+
of the keystroke.
|
92
|
+
|
93
|
+
*paired with subpattern it allows an easy build of an inputhandler*
|
94
|
+
|
95
|
+
#### Parallelize
|
96
|
+
|
97
|
+
##### parallelize(ary, processes: 1, threads: 1, progress: "", &block)
|
98
|
+
|
99
|
+
Based on https://github.com/grosser/parallel, it is a quite convenient way to parallelize tasks.
|
100
|
+
|
101
|
+
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
@@ -1,32 +1,29 @@
|
|
1
1
|
module Cotcube
|
2
2
|
module Helpers
|
3
3
|
|
4
|
-
def parallelize(ary,
|
5
|
-
processes = opts[:processes].nil? ? 1 : opts[:processes]
|
6
|
-
threads_per_process = opts[:threads ].nil? ? 1 : opts[:threads]
|
7
|
-
progress = opts[:progress ].nil? ? "" : opts[:progress]
|
4
|
+
def parallelize(ary, processes: 1, threads: 1, progress: "", &block)
|
8
5
|
chunks = []
|
9
|
-
if
|
10
|
-
|
11
|
-
elsif [0,1].include?(
|
12
|
-
|
6
|
+
if [0,1].include? processes
|
7
|
+
result = Parallel.map(ary, in_threads: threads) {|u| v = yield(u); v}
|
8
|
+
elsif [0,1].include?(threads)
|
9
|
+
result = Parallel.map(ary, in_processes: processes) {|u| v = yield(u)}
|
13
10
|
else
|
14
|
-
ary.each_slice(
|
11
|
+
ary.each_slice(threads) {|chunk| chunks << chunk }
|
15
12
|
if progress == ""
|
16
|
-
|
17
|
-
Parallel.map(chunk, in_threads:
|
13
|
+
result = Parallel.map(chunks, :in_processes => processes) do |chunk|
|
14
|
+
Parallel.map(chunk, in_threads: threads) do |unit|
|
18
15
|
yield(unit)
|
19
16
|
end
|
20
17
|
end
|
21
18
|
else
|
22
|
-
|
23
|
-
Parallel.map(chunk, in_threads:
|
19
|
+
result = Parallel.map(chunks, :progress => progress, :in_processes => processes) do |chunk|
|
20
|
+
Parallel.map(chunk, in_threads: threads) do |unit|
|
24
21
|
yield(unit)
|
25
22
|
end
|
26
23
|
end
|
27
24
|
end
|
28
25
|
end
|
29
|
-
|
26
|
+
result
|
30
27
|
end
|
31
28
|
|
32
29
|
end
|
@@ -29,9 +29,9 @@ class Range
|
|
29
29
|
starting_with_dst = result.first.dst?
|
30
30
|
seconds_since_sunday_morning = lambda {|x| x.wday * 86400 + x.hour * 3600 + x.min * 60 + x.sec}
|
31
31
|
ranges ||= [
|
32
|
-
|
33
|
-
147600..230399,
|
34
|
-
234000..316799,
|
32
|
+
61200..143999, # Sun 5pm .. Mon 4pm
|
33
|
+
147600..230399, # Mon 5pm .. Tue 4pm
|
34
|
+
234000..316799, # ...
|
35
35
|
320400..403199,
|
36
36
|
406800..489599
|
37
37
|
]
|
@@ -12,7 +12,7 @@ module Cotcube
|
|
12
12
|
#
|
13
13
|
# To extend functionality even more, it is possible to send a group of patterns to, like
|
14
14
|
# sub(2){[:hyper,:mega]}, what will respond truthy to "hy" and "meg" but not to "m" or "hypo"
|
15
|
-
def sub(minimum
|
15
|
+
def sub(minimum: 1)
|
16
16
|
pattern = yield
|
17
17
|
case pattern
|
18
18
|
when String, Symbol, NilClass
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cotcube-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin L. Tischendorf
|
@@ -76,6 +76,8 @@ files:
|
|
76
76
|
- ".irbrc.rb"
|
77
77
|
- CHANGELOG.md
|
78
78
|
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
79
81
|
- VERSION
|
80
82
|
- cotcube-helpers.gemspec
|
81
83
|
- lib/cotcube-helpers.rb
|