Form¶
- class rocky_worlds_data_challenge.Form(path: str = None, dictionary: dict = None, validate: bool = True)[source]¶
Bases:
objectSubmission questionnaire loaded from or written to JSON.
- Parameters:
path (str, optional) – Path to a JSON form file to load.
dictionary (dict, optional) – In-memory form dictionary keyed by question number. If supplied,
pathis not read.validate (bool, optional) – Whether to validate responses during initialization.
- Variables:
path (str) – Path to a JSON form file to load.
dictionary (dict) – In-memory form dictionary keyed by question number. Each question stores metadata such as prompt, description, format, example, and response.
Load the form dictionary from disk and optionally validate it.
- Parameters:
path (str, optional) – Path to a JSON form file to load.
dictionary (dict, optional) – In-memory form dictionary keyed by question number. If supplied,
pathis not read.validate (bool, optional) – Whether to validate responses during initialization.
- Raises:
ValueError – If validation is enabled and a form response is invalid.
Attributes Summary
In-memory form dictionary keyed by question number.
Path to a JSON form file to load.
Methods Summary
blank()Return a blank copy of the packaged submission form.
load(json_filepath[, zip_filepath])Load one form JSON file from a submission ZIP archive.
save(filepath[, overwrite, validate])Save this form as a plain JSON file.
Return a standardized JSON-serializable form dictionary.
to_json([validate])Return this form as a formatted JSON string.
validate()Validate all form responses against their declared formats.
Attributes Documentation
- dictionary: dict¶
In-memory form dictionary keyed by question number.
- path: str¶
Path to a JSON form file to load.
Methods Documentation
- classmethod blank()[source]¶
Return a blank copy of the packaged submission form.
- Returns:
Form initialized from the packaged blank form template without validating empty responses.
- Return type:
- classmethod load(json_filepath, zip_filepath='submission.zip')[source]¶
Load one form JSON file from a submission ZIP archive.
- Parameters:
json_filepath (str) – Name of the JSON form file inside
zip_filepath.zip_filepath (path-like, optional) – Path to the submission ZIP archive.
- Returns:
Form loaded from the JSON file in the archive.
- Return type:
- save(filepath, overwrite=False, validate=True)[source]¶
Save this form as a plain JSON file.
- Parameters:
filepath (path-like) – Destination JSON filepath.
overwrite (bool, optional) – Allow an existing file at
filepathto be replaced.validate (bool, optional) – Validate the form before writing it.
- Returns:
Absolute path to the written file.
- Return type:
str
- Raises:
FileExistsError – If
filepathalready exists andoverwriteisFalse.
- standardized_dictionary()[source]¶
Return a standardized JSON-serializable form dictionary.
- Returns:
Copy of the form dictionary with two-digit question keys and a
responsefield for every question.- Return type:
dict