Input File#

class InputFile(program: str, text_input: str = '', string_leafs: bool = False, lowercase: bool = False)#
class InputFile(definition: InputDefinition, text_input: str = '', string_leafs: bool = False, lowercase: bool = False)

Class representing a key and block based text input file as used in the Amsterdam Modeling Suite.

class EntryCategory(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)#

Enum representing the category of an entry in an InputFile.

Values:
  • Block: the entry is a block containing keys and other blocks

  • Key: the entry is a key with a value

class EntryType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)#

Enum representing the type of an entry in an InputFile.

Values:
  • FixedBlock: the entry is a block containing defined keys and other blocks

  • FreeBlock: the entry is a free block that is read as a list of strings representing the lines

  • EngineBlock: the entry is a free block that ends with “EndEngine”

  • InputBlock: the entry is a free block that ends with “EndInput”

  • StringKey: the entry is a key of type string

  • MultipleChoiceKey: the entry is a multiple-choice key

  • LogicalKey: the entry is a key of type logical

  • IntegerKey: the entry is a key of type integer

  • IntegerListKey: the entry is a key of type integer_list

  • RealKey: the entry is a key of type float

  • RealListKey: the entry is a key of type float_list

  • VerbatimBlock: the entry is a free block that gets read without any changes (e.g. whitespace stripping)

__init__(program: str, text_input: str = '', string_leafs: bool = False, lowercase: bool = False)#
__init__(definition: InputDefinition, text_input: str = '', string_leafs: bool = False, lowercase: bool = False)

Creates a new InputFile from a text input, either for a program or for an InputDefinition.

For a program name, the input definition is loaded for this InputFile with InputDefinition.for_program(), and read() reuses it. To parse several inputs for one program, it is faster to load the definition once and pass it to every new InputFile.

Different InputFile objects can be used by different threads at the same time, also if they use the same InputDefinition. One InputFile object must not be used by two threads at the same time.

get(query: str) → int | float | bool | str | List[int] | List[float] | List[str]#

Returns the value of a key or free block. The return type depends on the type of the accessed key.

get_category(query: str) → EntryCategory#

Returns the category of an entry as defined in the JSON definition for the program.

get_entries_in_order(query: str) → List[str]#

Returns the entries of an ordered fixed block in the order they occurred in the input.

get_freeblock(query: str) → List[str]#

Returns the contents of a free block as a list of lines.

get_header(query: str) → str#

Returns the header of a block as a string. If a block does not have a header, an empty string is returned.

get_int(query: str) → int#

Returns the value of an integer key.

get_int_list(query: str) → List[int]#

Returns the value of an integer_list key.

get_json() → str#

Returns the JSON representation of the input file as a string.

get_logical(query: str) → bool#

Returns the value of a logical key.

get_real(query: str) → float#

Returns the value of a float key.

get_real_list(query: str) → List[float]#

Returns the value of a float_list key.

get_string(query: str) → str#

Returns the value of a string or multiple choice key.

get_type(query: str) → EntryType#

Returns the type of an entry as defined in the JSON definition for the program.

is_defined(query: str) → bool#

Checks if an entry is defined in the JSON definition for the program.

is_ordered(query: str) → bool#

Checks if a fixed block is declared order sensitive in the JSON definition for the program.

is_present(query: str) → bool#

Checks if an entry is present in the input.

is_unique(query: str) → bool#

Check whether an entry is declared unique or recurring in the keywords definition file.

number_of_entries(query: str) → int#

Returns the number of occurrances of a non-unique entry.

read(text_input: str, string_leafs: bool = False, lowercase: bool = False) → None#

Read more text input for the same program or input definition. This replaces the previously read input.

property program: str#

The name of the program that this instance is reading input for (for an InputDefinition: its label).

class InputDefinition#

A loaded and checked input definition: the JSON that defines the keys and blocks of a program’s text input.

Loading a definition is much more expensive than parsing a typical text input for it. Load it once and create an InputFile for every text input with InputFile(definition, text_input). An InputDefinition is never modified, so any number of InputFiles can use it at the same time, also in different threads. InputFiles keep their InputDefinition alive.

from scm.base import InputDefinition, InputFile

definition = InputDefinition.from_json("myprogram", json_definition)
inp = InputFile(definition, "MyKey 3")

ams_definition = InputDefinition.for_program("ams")
ams_inp = InputFile(ams_definition, "Task SinglePoint")
static for_program(program: str) → InputDefinition#

Loads and checks the input definition of a program: $AMSBIN/input_def/<program>.json, or the file program if it ends in .json or contains a /. Every call loads the file: keep the InputDefinition if you parse more than one input for the program. Raises InputError if the definition can not be read, parsed, or is invalid.

static from_json(label: str, json: str) → InputDefinition#

Loads and checks an input definition from its JSON. The label is the program name of InputFiles created for this definition. Raises InputError if the definition can not be parsed or is invalid.

property label: str#

The label (program name) of this input definition.

class InputError#

Error thrown for issues with the input reading, e.g. reading non-existing keywords.