コード例 #1
0
def query(database, args):
    """Perform the `query` subcommand.

    Create a collection of filters with `create_filters` and supply them to
    the database's `query` method to produce a stream of matching results.

    If an output file wasn't given, print these results to stdout, limiting
    to 10 entries if no limit was specified. If an output file was given,
    use the file's extension to infer whether the file should hold CSV
    or JSON data, and then write the results to the output file in
    that format.

    :param database: The `NEODatabase` containing data on NEOs and
    their close approaches.
    :param args: All arguments from the command line, as parsed by
    the top-level parser.
    """
    # Construct a collection of filters from arguments supplied at
    # the command line.
    filters = create_filters(
        date=args.date,
        start_date=args.start_date,
        end_date=args.end_date,
        distance_min=args.distance_min,
        distance_max=args.distance_max,
        velocity_min=args.velocity_min,
        velocity_max=args.velocity_max,
        diameter_min=args.diameter_min,
        diameter_max=args.diameter_max,
        hazardous=args.hazardous,
    )
    # Query the database with the collection of filters.
    results = database.query(filters)

    if not args.outfile:
        # Write the results to stdout, limiting to 10 entries if
        # not specified.
        for result in limit(results, args.limit or 10):
            print(result)
    else:
        # Write the results to a file.
        if args.outfile.suffix == ".csv":
            write_to_csv(limit(results, args.limit), args.outfile)
        elif args.outfile.suffix == ".json":
            write_to_json(limit(results, args.limit), args.outfile)
        else:
            print(
                "Please use output file that ends with `.csv` or `.json`.",
                file=sys.stderr,
            )
コード例 #2
0
    def setUpClass(cls, mock_file):
        results = build_results(5)

        with UncloseableStringIO() as buf:
            mock_file.return_value = buf
            try:
                write_to_json(results, None)
            except csv.Error as err:
                raise cls.failureException(
                    "Unable to write results to CSV.") from err
            except ValueError as err:
                raise cls.failureException(
                    "Unexpected failure while writing to CSV.") from err
            else:
                # Rewind the unclosed buffer to fetch the contents saved to "disk".
                buf.seek(0)
                cls.value = buf.getvalue()
コード例 #3
0
def query(database, args):
    """Perform the `query` subcommand.

    :param database: The `NEODatabase` containing data on NEOs and their close approaches.
    :param args: All arguments from the command line, as parsed by the top-level parser.
    """
    # Construct a collection of filters from arguments supplied at the command line.
    filters = create_filters(date=args.date,
                             start_date=args.start_date,
                             end_date=args.end_date,
                             distance_min=args.distance_min,
                             distance_max=args.distance_max,
                             velocity_min=args.velocity_min,
                             velocity_max=args.velocity_max,
                             diameter_min=args.diameter_min,
                             diameter_max=args.diameter_max,
                             hazardous=args.hazardous)

    results = database.query(filters)

    if not args.outfile:
        # Write the results to stdout, limiting to 10 entries if not specified.
        for result in limit(results, args.limit or 10):
            try:
                print(result)
            except StopIteration:
                break
    else:
        # Write the results to a file.
        if args.outfile.suffix == '.csv':
            write_to_csv(limit(results, args.limit), args.outfile)
        elif args.outfile.suffix == '.json':
            write_to_json(limit(results, args.limit), args.outfile)
        else:
            print(
                "Please use an output file that ends with `.csv` or `.json`.",
                file=sys.stderr)