示例#1
0
def worker(file_handle: click.utils.LazyFile, queue: Queue) -> None:
    """Write YAML to a file in an incremental manner.

    This implementation doesn't use `pyyaml` package and composes YAML manually as string due to the following reasons:
      - It is much faster. The string-based approach gives only ~2.5% time overhead when `yaml.CDumper` has ~11.2%;
      - Implementation complexity. We have a quite simple format where all values are strings and it is much simpler to
        implement it with string composition rather than with adjusting `yaml.Serializer` to emit explicit types.
        Another point is that with `pyyaml` we need to emit events and handle some low-level details like providing
        tags, anchors to have incremental writing, with strings it is much simpler.
    """
    current_id = 1
    stream = file_handle.open()

    def format_header_values(values: List[str]) -> str:
        return "\n".join(f"      - '{v}'" for v in values)

    def format_headers(headers: Dict[str, List[str]]) -> str:
        return "\n".join(f"      {name}:\n{format_header_values(values)}"
                         for name, values in headers.items())

    while True:
        item = queue.get()
        if isinstance(item, Initialize):
            stream.write(f"""command: '{get_command_representation()}'
recorded_with: 'Schemathesis {constants.__version__}'
http_interactions:""")
        elif isinstance(item, Process):
            for interaction in item.interactions:
                stream.write(f"""\n- id: '{current_id}'
  status: '{item.status}'
  seed: '{item.seed}'
  elapsed: '{interaction.response.elapsed}'
  recorded_at: '{interaction.recorded_at}'
  request:
    uri: '{interaction.request.uri}'
    method: '{interaction.request.method}'
    headers:
{format_headers(interaction.request.headers)}
    body:
      encoding: 'utf-8'
      base64_string: '{interaction.request.body}'
  response:
    status:
      code: '{interaction.response.status_code}'
      message: '{interaction.response.message}'
    headers:
{format_headers(interaction.response.headers)}
    body:
      encoding: '{interaction.response.encoding}'
      base64_string: '{interaction.response.body}'
    http_version: '{interaction.response.http_version}'""")
                current_id += 1
        else:
            break
    file_handle.close()
示例#2
0
def main(
    image_directory: str,
    diff_file: click.utils.LazyFile,
    show_plot: bool,
    smoothing_option: str,
):
    print("Computing diffs...")
    file_list = glob.glob(os.path.join(image_directory, "*.jpg"))
    diffs = calculate_diffs(file_list)
    if diff_file is not None:
        print(f"Writing diffs to {diff_file.name}")
        for diff in diffs:
            diff_file.write(f"{diff}\n")
    else:
        print(f"Diffs are {np.array(diffs)}")
    if show_plot:
        smoothed_diffs = smooth_values(diffs,
                                       SmoothingOption[smoothing_option])
        plt.plot(diffs)
        plt.plot(smoothed_diffs)
        plt.show()