Form

class rocky_worlds_data_challenge.Form(path: str = None, dictionary: dict = None, validate: bool = True)[source]

Bases: object

Submission 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, path is 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, path is 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

dictionary

In-memory form dictionary keyed by question number.

path

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.

standardized_dictionary()

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:

Form

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:

Form

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 filepath to be replaced.

  • validate (bool, optional) – Validate the form before writing it.

Returns:

Absolute path to the written file.

Return type:

str

Raises:

FileExistsError – If filepath already exists and overwrite is False.

standardized_dictionary()[source]

Return a standardized JSON-serializable form dictionary.

Returns:

Copy of the form dictionary with two-digit question keys and a response field for every question.

Return type:

dict

to_json(validate=True)[source]

Return this form as a formatted JSON string.

Parameters:

validate (bool, optional) – Validate the form before serializing it.

Returns:

Form dictionary serialized as formatted JSON.

Return type:

str

validate()[source]

Validate all form responses against their declared formats.

Raises:

ValueError – If a required response is missing or a response cannot be parsed according to its declared format.