Module: BatchExperiment::Extractor
- Included in:
- FirstLineExtractor, PyaExtractor, UKP5Extractor
- Defined in:
- lib/batch_experiment/extractor.rb
Overview
Module that defines the interface used for extracting info from other programs output. You don’t need to include it in your object, will suffice that the object (that you will use to extract info from the output) has the #names and #extract methods defined.
Class Method Summary collapse
-
.get_field(lines, field) ⇒ String
Find a line in the following format: “field: value”, return value.
-
.get_hfield(lines, field) ⇒ String
Find a line in the following format: “field<linebreak>value”, return value.
Instance Method Summary collapse
-
#extract(content) ⇒ Array<String>
Extract N values of some program output, where N is equal to #names.size.
-
#extract_from_lines(lines) ⇒ Array<String>
Optionally, you can define this method instead of #extract.
-
#names ⇒ Array<String>
Return the field names for each of the elements returned by extract.
Class Method Details
.get_field(lines, field) ⇒ String
Find a line in the following format: “field: value”, return value.
15 16 17 18 19 20 |
# File 'lib/batch_experiment/extractor.rb', line 15 def self.get_field(lines, field) lines.grep(/^#{field}: .*/).each do | l | return l.match(/:[\t ]+(.*)/)[1] end '' end |
.get_hfield(lines, field) ⇒ String
Find a line in the following format: “field<linebreak>value”, return value.
33 34 35 |
# File 'lib/batch_experiment/extractor.rb', line 33 def self.get_hfield(lines, field) if ix = lines.find_index(field) then lines[ix + 1] else '' end end |
Instance Method Details
#extract(content) ⇒ Array<String>
Extract N values of some program output, where N is equal to #names.size. If some value isn’t present, the array should remain the same size, and the value at the corresponding position should be nil.
56 57 58 |
# File 'lib/batch_experiment/extractor.rb', line 56 def extract(content) extract_from_lines(content.lines.map! { | l | l.chomp! }) end |
#extract_from_lines(lines) ⇒ Array<String>
Optionally, you can define this method instead of #extract. The #extract method will call this method if not overrided.
66 67 68 |
# File 'lib/batch_experiment/extractor.rb', line 66 def extract_from_lines(lines) raise 'This method should have been overwritten by a subclass.' end |
#names ⇒ Array<String>
To be on the safe side you should create a new array at each call. If you always return a reference to the same array the array can be modified.
Return the field names for each of the elements returned by extract. Ex.:
['Time', 'Max Mem Use', 'opt', ... ]
46 47 48 |
# File 'lib/batch_experiment/extractor.rb', line 46 def names raise 'This method should have been overwritten by a subclass.' end |