Class: BatchExperiment::Comm2FnameConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/batch_experiment.rb

Overview

Converts a command to a filename using a given sanitizer, gives different names to different calls with the same arguments. Example: if a call with “sleep 1” yields “sleep_1”, the second call with the same argument yields “sleep_1.2”, and so on. Note that this is done by remembering previous calls, the object don’t inspect the filesystem to check if that name was or wasn’t used.

Instance Method Summary collapse

Constructor Details

#initialize(sanitizer = FnameSanitizer) ⇒ Comm2FnameConverter

Creates a new Comm2FnameConverter, with no memory of any previous calls.

Parameters:

  • sanitizer (#call) (defaults to: FnameSanitizer)

    Callable object used to create a filename from the arguments passed to Comm2FnameConverter.call. This class expects that sanitizer has no internal state, so when an instance of this class is cloned, there’s no problem with sharing the sanitizer between the clones. Default: BatchExperiment::FnameSanitizer.



57
58
59
60
# File 'lib/batch_experiment.rb', line 57

def initialize(sanitizer = FnameSanitizer)
  @num_times_seen = {}
  @sanitizer = sanitizer
end

Instance Method Details

#call(comm) ⇒ String

Note:

Note that different arguments can be reduced to the same sanitized filename and, if this happens, they will NOT overwrite each other. Example: ‘echo “abc”’ -> ‘echo_abc’; ‘echo abc’ -> ‘echo_abc.2’.

Takes a command, creates a fname for it, if this fname was already seen before, returns the fname + “.N”, where N is the number of times fname was already seen.

Parameters:

  • comm (String)

    A system command.

Returns:

  • (String)

    The sanitized filename created from that command.



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/batch_experiment.rb', line 72

def call(comm)
  fname = @sanitizer.call(comm)
  if @num_times_seen.include? fname
    @num_times_seen[fname] += 1
    fname << ".#{@num_times_seen[fname]}"
  else
    @num_times_seen[fname] = 1
  end

  fname.clone
end

#initialize_clone(old) ⇒ Object

Used to guarantee that a clone of Comm2FnameConverter will not share relevant state with the original. So calls to #call on a clone don’t affect the state of original (and vice versa).



87
88
89
# File 'lib/batch_experiment.rb', line 87

def initialize_clone(old)
  @num_times_seen = old.num_times_seen.clone
end