Reading items from a fileΒΆ

Salabim models often need to read input values from a file.

As these data are quite often quite unstructured, using the standard read facilities of text files can be rather tedious.

Therefore, salabim offers the possibility to read a file item by item.

Example usage

with sim.ItemFile(filename) as f:
    run_length = f.read_item_float()
    run_name = f.read_item()

Or (not recommended)

f = sim.InputFile(filename)
run_length = f.read_item_float()
run_name = f.read_item()

The input file is read per item, where blanks, linefeeds, tabs are treated as separators.
Any text on a line after a # character is ignored.
Any text within curly brackets ( {} ) is ignored (and treated as an item separator).
Note that this strictly on a per line basis.
If a blank or tab is to be included in a string, use single or double quotes.
The recommended way to end a list of values is //

So, a typical input file is

# Typical experiment file for a salabim model
1000              # run length
'Experiment 2.0'  # run name

 #Model          speed color
 #-------------- ----- ------

 'Peugeot 208'       150 red
 'Peugeot 3008'      175 orange
 'Citroen C5'        160 blue
 'Renault "Twingo"'  165 green

 France {country} Europe {continent}

 #end of file

Instead of the filename as a parameter to ItemFile, also a string with the content can be given. In that case, at least one linefeed has to be in the content string. Usually, the content string will be triple quoted. This can be very useful during testing as the input is part of the source file and not external, e.g.

test_input = '''
one two
three four
with sim.ItemFile(test_input) as f:
    while True:
        except EOFError: