rbbt-util 5.18.0 → 5.18.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ab39fdc1ea50f9747effe02d0c7008a288194a88
4
- data.tar.gz: 4359eaf63e8c0592bb0d035099bd94e1e12fa3b9
3
+ metadata.gz: 7f4bb8ad71b9080b39bc7217d9f4a5dc0da2719d
4
+ data.tar.gz: 94b7dfcbd524c888c73f945cf95e223b318205a7
5
5
  SHA512:
6
- metadata.gz: cccceae6eee4bbc8d64dcfc8e9791c7e7b00ee32bfec839ff3bb914b2511c29b1382acf626f641e1b38c022caabe347492ea3eb1315f5a5b06b01dbc3d2a312c
7
- data.tar.gz: dc1dc72ef5f0f69f36d81b9b7bbfcb8f84c5259618aa9e466967a750e13192eeaed3a5d69589fbbbead6347d3df8306cb22e43c098de7414b5fb91f049dc5adb
6
+ metadata.gz: 17e4da0d2455c8c93de87d51b4166b2156552611c3bafc17ce561fcb6d278e3ad16df23ece0727fea9a783c66d97b528e889246cd5da51a183ba368ba8c7ecff
7
+ data.tar.gz: 2c981aee80405b0d9794c4568114da6c3d2916f1fac755357f081566ad9d40c17346d5fe14c3388cb66f3a490ede94ab261bb1cb5e2d79cc0f51584d8fbd7b23
@@ -113,6 +113,18 @@ module Misc
113
113
  Digest::MD5.hexdigest(text)
114
114
  end
115
115
 
116
+ def self.sample_large_obj(obj, max = 100)
117
+ length = obj.length
118
+ head = obj[0..max/2]
119
+ tail = obj[-max/2..-1]
120
+ middle = (1..9).to_a.collect{|i| pos = (length / 10) * i + i; obj[pos-1..pos+1]}.flatten
121
+ if Array === obj
122
+ head + middle + tail + ["LENGTH: #{obj.length}"]
123
+ else
124
+ head << "..." << middle*"," << "..." << tail << "(#{obj.length})"
125
+ end
126
+ end
127
+
116
128
  HASH2MD5_MAX_STRING_LENGTH = 1000
117
129
  HASH2MD5_MAX_ARRAY_LENGTH = 100
118
130
  def self.hash2md5(hash)
@@ -184,4 +196,61 @@ module Misc
184
196
  end
185
197
  end
186
198
 
199
+
200
+ def self.obj2str(obj)
201
+ _obj = obj
202
+ obj = Annotated.purge(obj) if Annotated === obj
203
+
204
+ str = case obj
205
+ when TrueClass
206
+ 'true'
207
+ when FalseClass
208
+ 'false'
209
+ when Hash
210
+ "{"<< obj.collect{|k,v| obj2str(k) << '=>' << obj2str(v)}*"," << "}"
211
+ when Symbol
212
+ obj.to_s
213
+ when String
214
+ if obj.length > HASH2MD5_MAX_STRING_LENGTH
215
+ sample_large_obj(obj, HASH2MD5_MAX_STRING_LENGTH)
216
+ else
217
+ obj
218
+ end
219
+ when Array
220
+ if obj.length > HASH2MD5_MAX_ARRAY_LENGTH
221
+ "[" << sample_large_obj(obj, HASH2MD5_MAX_ARRAY_LENGTH).collect{|v| obj2str(v)} * "," << "]"
222
+ else
223
+ "[" << obj.collect{|v| obj2str(v)} * "," << "]"
224
+ end
225
+ when TSV::Parser
226
+ remove_long_items(obj)
227
+ when File
228
+ "[File:" << obj.path << "]"
229
+ else
230
+ obj_ins = obj.inspect
231
+ if obj_ins =~ /:0x0/
232
+ obj_ins.sub(/:0x[a-f0-9]+@/,'')
233
+ else
234
+ obj_ins
235
+ end
236
+ end
237
+
238
+ if defined? Annotated and Annotated === _obj and not (defined? AssociationItem and AssociationItem === _obj)
239
+ info = Annotated.purge(_obj.info)
240
+ str << "_" << hash2str(info)
241
+ end
242
+
243
+ str
244
+ end
245
+
246
+
247
+ def self.obj2md5(obj)
248
+ str = obj2str(obj)
249
+
250
+ if str.empty?
251
+ ""
252
+ else
253
+ digest(str)
254
+ end
255
+ end
187
256
  end
@@ -586,8 +586,7 @@ module Workflow
586
586
  if inputs.any? or dependencies.any?
587
587
  tagged_jobname = case TAG
588
588
  when :hash
589
- input_hash = Hash[*(1..inputs.length).zip(inputs).flatten]
590
- hash_str = Misc.hash2md5(input_hash)
589
+ hash_str = Misc.obj2md5(inputs)
591
590
  jobname + '_' << hash_str
592
591
  else
593
592
  jobname
@@ -466,4 +466,50 @@ eum fugiat quo voluptas nulla pariatur?"
466
466
  end
467
467
  end
468
468
 
469
+ def test_obj2md5
470
+ str = "string" * 1000000
471
+ obj1 = [1,2,"test", 0.3, [1,2,3], {:a => [1,2], :b => str}]
472
+ obj2 = [1,2,"test", 0.3, [1,2,3], {:a => [1,2,3], :b => str}]
473
+ obj3 = [1,2,:test, 0.3, [1,2,3], {:a => [1,2,"3"], :b => str}]
474
+ assert_not_equal Misc.obj2md5(obj1), Misc.obj2md5(obj2)
475
+ assert_equal Misc.obj2md5(obj2), Misc.obj2md5(obj3)
476
+ hash = Hash[*(0..obj1.length).zip(obj1).flatten]
477
+
478
+ Misc.benchmark(10000) do
479
+ Misc.hash2md5(hash)
480
+ end
481
+ Misc.benchmark(10000) do
482
+ Misc.obj2md5(obj1)
483
+ end
484
+ Misc.profile do
485
+ 10.times do
486
+ Misc.hash2md5(hash)
487
+ end
488
+ end
489
+ Misc.profile do
490
+ 10.times do
491
+ Misc.obj2md5(obj1)
492
+ end
493
+ end
494
+ end
495
+
496
+ def test_sample_large_string
497
+ str = "string" * 1000000
498
+
499
+ max = 100
500
+ assert_equal Misc.sample_large_obj(str, max).length, max+51
501
+
502
+ max = 1000
503
+ assert_equal Misc.sample_large_obj(str, max).length, max+51
504
+ end
505
+
506
+ def test_sample_large_array
507
+ str = (0..1000000).to_a
508
+
509
+ max = 100
510
+ assert_equal Misc.sample_large_obj(str, max).length, max+29
511
+
512
+ max = 1000
513
+ assert_equal Misc.sample_large_obj(str, max).length, max+29
514
+ end
469
515
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbbt-util
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.18.0
4
+ version: 5.18.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Vazquez