def write_to_csv(results, filename): """Write an iterable of `CloseApproach` objects to a CSV file. The precise output specification is in `README.md`. Roughly, each output row corresponds to the information in a single close approach from the `results` stream and its associated near-Earth object. :param results: An iterable of `CloseApproach` objects. :param filename: A Path-like object pointing to where the data should be saved. """ fieldnames = ('datetime_utc', 'distance_au', 'velocity_km_s', 'designation', 'name', 'diameter_km', 'potentially_hazardous') with open(filename, 'w') as outfile: writer = csv.DictWriter(outfile, fieldnames=fieldnames) writer.writeheader() for elem in results: d = {} d["datetime_utc"] = datetime_to_str(elem.time) d["distance_au"] = elem.distance d["velocity_km_s"] = elem.velocity d["designation"] = elem.neo.designation d["name"] = elem.neo.name if elem.neo.name else 'None' d["diameter_km"] = elem.neo.diameter if elem.neo.diameter else 'nan' d["potentially_hazardous"] = 'True' if elem.neo.hazardous else 'False' writer.writerow(d)
def write_to_json(results, filename): """Write an iterable of `CloseApproach` objects to a JSON file. The precise output specification is in `README.md`. Roughly, the output is a list containing dictionaries, each mapping `CloseApproach` attributes to their values and the 'neo' key mapping to a dictionary of the associated NEO's attributes. :param results: An iterable of `CloseApproach` objects. :param filename: A Path-like object pointing to where the data should be saved. """ with open(filename, 'w') as outfile: l = [] for elem in results: d = {} d["datetime_utc"] = datetime_to_str(elem.time) d["distance_au"] = elem.distance d["velocity_km_s"] = elem.velocity neo = {} neo["designation"] = elem.neo.designation neo["name"] = elem.neo.name neo["diameter_km"] = elem.neo.diameter neo["potentially_hazardous"] = elem.neo.hazardous d["neo"] = neo l.append(d) json.dump(l, outfile, indent=2)
def write_to_json(results, filename): """Write an iterable of `CloseApproach` objects to a JSON file. The precise output specification is in `README.md`. Roughly, the output is a list containing dictionaries, each mapping `CloseApproach` attributes to their values and the 'neo' key mapping to a dictionary of the associated NEO's attributes. :param results: An iterable of `CloseApproach` objects. :param filename: A Path-like object pointing to where the data should be saved. """ # Write the results to a JSON file, following the specification in the instructions. dict_for_json = list() for each_approach in results: row_for_json = { 'datetime_utc': datetime_to_str(each_approach.time), 'distance_au': each_approach.distance, 'velocity_km_s': each_approach.velocity, 'neo': { 'designation': each_approach.designation, 'name': each_approach.neo.name, 'diameter_km': each_approach.neo.diameter, 'potentially_hazardous': each_approach.neo.hazardous } } dict_for_json.append(row_for_json) with open(filename, 'w') as json_to_write: json_to_write.write(json.dumps(dict_for_json, indent=4))
def write_to_json(results, filename): """Write an iterable of `CloseApproach` objects to a JSON file. The precise output specification is in `README.md`. Roughly, the output is a list containing dictionaries, each mapping `CloseApproach` attributes to their values and the 'neo' key mapping to a dictionary of the associated NEO's attributes. :param results: An iterable of `CloseApproach` objects. :param filename: A Path-like object pointing to where the data should be saved. """ neo_fieldnames = ('designation', 'name', 'diameter_km', 'potentially_hazardous') approach_fieldnames = ('datetime_utc', 'distance_au', 'velocity_km_s', 'neo') out_info = list() with open(filename, 'w') as output_file: for result in results: neo_info = [ result.neo.designation, result.neo.name, result.neo.diameter, result.neo.hazardous ] approach_info = [ datetime_to_str(result.time), result.distance, result.velocity, dict(zip(neo_fieldnames, neo_info)) ] out_info.append(dict(zip(approach_fieldnames, approach_info))) json.dump(out_info, output_file, default=str)
def write_to_json(results, filename): """Write an iterable of `CloseApproach` objects to a JSON file. The precise output specification is in `README.md`. Roughly, the output is a list containing dictionaries, each mapping `CloseApproach` attributes to their values and the 'neo' key mapping to a dictionary of the associated NEO's attributes. :param results: An iterable of `CloseApproach` objects. :param filename: A Path-like object pointing to where the data should be saved. """ json_row = [] for data in results: approach = { 'datetime_utc': datetime_to_str(data.time), 'distance_au': data.distance, 'velocity_km_s': data.velocity, 'neo': { 'designation': data.neo.designation, 'name': data.neo.name, 'diameter_km': data.neo.diameter, 'potentially_hazardous': data.neo.hazardous } } json_row.append(approach) with open(filename, "w") as outfile: json.dump(json_row, outfile, indent=2)
def write_to_csv(results, filename): """Write an iterable of `CloseApproach` objects to a CSV file. The precise output specification is in `README.md`. Roughly, each output row corresponds to the information in a single close approach from the `results` stream and its associated near-Earth object. :param results: An iterable of `CloseApproach` objects. :param filename: A Path-like object pointing to where the data should be saved. """ fieldnames = ('datetime_utc', 'distance_au', 'velocity_km_s', 'designation', 'name', 'diameter_km', 'potentially_hazardous') with open( filename, 'w', ) as f: writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() for row in results: writer.writerow({ 'datetime_utc': datetime_to_str(row.time), 'distance_au': row.distance, 'velocity_km_s': row.velocity, 'designation': row._designation, 'name': row.neo.name, 'diameter_km': row.neo.diameter, 'potentially_hazardous': str(row.neo.hazardous) })
def serialize(self): """Return a dictionary with key attributes of the close approach.""" return { "datetime_utc": datetime_to_str(self.time), "distance_au": self.distance, "velocity_km_s": self.velocity, }
def write_to_json(results, filename): """Write an iterable of `CloseApproach` objects to a JSON file. The precise output specification is in `README.md`. Roughly, the output is a list containing dictionaries, each mapping `CloseApproach` attributes to their values and the 'neo' key mapping to a dictionary of the associated NEO's attributes. :param results: An iterable of `CloseApproach` objects. :param filename: A Path-like object pointing to where the data should be saved. """ # list saving all the results in dictionary format result_dict = [] for row in results: entry = { "datetime_utc": helpers.datetime_to_str(row.time), "distance_au": row.distance, "velocity_km_s": row.velocity, "neo": { "designation": row.neo.designation, "name": row.neo.name, "diameter_km": row.neo.diameter, "potentially_hazardous": row.neo.hazardous } } result_dict.append(entry) # save the list of approach dictionaries in json format with open(filename, "w") as outfile: json.dump(result_dict, outfile, indent=2)
def write_to_json(results, filename): """Write an iterable of `CloseApproach` objects to a JSON file. The precise output specification is in `README.md`. Roughly, the output is a list containing dictionaries, each mapping `CloseApproach` attributes to their values and the 'neo' key mapping to a dictionary of the associated NEO's attributes. :param results: An iterable of `CloseApproach` objects. :param filename: A Path-like object pointing to where the data should be saved. """ with open(filename, 'w') as outfile: data = {} dump_data = [] for result in results: data['datetime_utc'] = datetime_to_str(result.time) data['distance_au'] = result.distance data['velocity_km_s'] = result.velocity neo = {} neo['designation'] = result.neo.designation neo['name'] = result.neo.name neo['diameter_km'] = result.neo.diameter neo['potentially_hazardous'] = result.neo.hazardous data['neo'] = neo dump_data.append(data) json.dump(dump_data, outfile, indent=2)
def serialize(self): return { 'datetime_utc': datetime_to_str(self.time), 'distance_au': self.distance, 'velocity_km_s': self.velocity, 'neo': self.neo.serialize() }
def write_to_csv(results, filename): """ I must say, I hate this documation strings I find them impractical and they polute the code Should be a better way there """ fieldnames = ('datetime_utc', 'distance_au', 'velocity_km_s', 'designation', 'name', 'diameter_km', 'potentially_hazardous') with open(filename, 'w') as fd: writer = csv.DictWriter(fd, fieldnames=fieldnames) writer.writeheader() for ca in results: writer.writerow({ 'datetime_utc': datetime_to_str(ca.time), 'distance_au': ca.distance, 'velocity_km_s': ca.velocity, 'designation': ca.designation, 'name': ca.neo.name, 'diameter_km': ca.neo.diameter, 'potentially_hazardous': str(ca.neo.hazardous) })
def __json__(self): """Return a serialized version of an instance for json dump purpose.""" return { 'datetime_utc': datetime_to_str(self.time), 'distance_au': self.distance, 'velocity_km_s': self.velocity, 'neo': self.neo.__json__() }
def serialize(self): """Returns a dictionary with the correctly formatted output""" return { 'datetime_utc': datetime_to_str(self.time), 'distance_au': self.distance, 'velocity_km_s': self.velocity }
def serialize(self): """To serialize an object. :return: serialized object of CloseApproach """ return {'datetime_utc': datetime_to_str(self.time), 'distance_au': self.distance, 'velocity_km_s': self.velocity}
def serialize(self): """Return serialized dictionary data.""" return { 'datetime_utc': datetime_to_str(self.time), 'distance_au': self.distance, 'velocity_km_s': self.velocity, 'neo': self.neo.serialize() }
def serialize(self): """Return an attribute dict used in the write_to_json function.""" return { 'datetime_utc': datetime_to_str(self.time), 'distance_au': self.distance, 'velocity_km_s': self.velocity, 'neo': self.neo.serialize() }
def measured(self): """Returns the measured dictionary data into the json or csv file""" return { 'datetime_utc': datetime_to_str(self.time), 'distance_au': self.distance, 'velocity_km_s': self.velocity, 'neo': self.neo.measured() }
def serialize(self): """Create dict of object data for json/csv writing. Returns: 'datetime_utc', 'distance_au', 'velocity_km_s' as keys. """ json_compatible_dict = { 'datetime_utc': datetime_to_str(self.time), 'distance_au': self.distance, 'velocity_km_s': self.velocity } return json_compatible_dict
def serialize(self): result = { 'datetime_utc': datetime_to_str(self.time), 'distance_au': self.distance, 'velocity_km_s': self.velocity, 'neo': { 'designation': self.neo.designation, 'name': self.neo.name, 'diameter_km': self.neo.diameter, 'potentially_hazardous': self.neo.hazardous }, } return result
def time_str(self): """Return a formatted representation of this `CloseApproach`'s approach time. The value in `self.time` should be a Python `datetime` object. While a `datetime` object has a string representation, the default representation includes seconds - significant figures that don't exist in our input data set. The `datetime_to_str` method converts a `datetime` object to a formatted string that can be used in human-readable representations and in serialization to CSV and JSON files. """ return datetime_to_str(self.time)
def serialize(self): """Produces a dictionary containing relevant attributes.""" result = { 'datetime_utc': datetime_to_str(self.time), 'distance_au': self.distance, 'velocity_km_s': self.velocity, 'neo': { 'designation': self.neo.designation, 'name': self.neo.name, 'diameter_km': self.neo.diameter, 'potentially_hazardous': self.neo.hazardous } } return result
def serialize(self): """ Sample output: {'datetime_utc': '2025-11-30 02:18', 'distance_au': 0.39, 'velocity_km_s': 3.72}""" # (, , '', '', 'name', '', '') res = { 'datetime_utc': datetime_to_str(self.time), 'distance_au': self.distance, 'velocity_km_s': self.velocity, 'neo': { 'designation': self.neo.designation, 'potentially_hazardous': self.neo.hazardous, 'diameter_km': self.neo.diameter, 'name': '' if self.neo.name is None else self.neo.name } } return res
def write_to_json(results, filename): s = list() for ca in results: dict_obj = { 'datetime_utc': datetime_to_str(ca.time), 'distance_au': ca.distance, 'velocity_km_s': ca.velocity, 'neo': { 'designation': ca.designation, 'name': ca.neo.name, 'diameter_km': ca.neo.diameter, 'potentially_hazardous': ca.neo.hazardous } } s.append(dict_obj) with open(filename, 'w') as fd: fd.write(json.dumps(s, indent=2))
def write_to_csv(results, filename): """Write an iterable of `CloseApproach` objects to a CSV file. The precise output specification is in `README.md`. Roughly, each output row corresponds to the information in a single close approach from the `results` stream and its associated near-Earth object. :param results: An iterable of `CloseApproach` objects. :param filename: A Path-like object pointing to where the data should be saved. """ with open(filename, 'w') as Close_approach_csv: fieldnames = [ 'datetime_utc', 'distance_au', 'velocity_km_s', 'designation', 'name', 'diameter_km', 'potentially_hazardous' ] writer = csv.DictWriter(Close_approach_csv, fieldnames) writer.writeheader() if results: for elem in results: item = { 'datetime_utc': datetime_to_str(elem.time), 'distance_au': elem.distance, 'velocity_km_s': elem.velocity, 'designation': elem.designation, 'name': elem.neo.name if elem.neo.name else '', 'diameter_km': elem.neo.diameter if elem.neo.diameter else float('nan'), 'potentially_hazardous': 'True' if elem.neo.hazardous else 'False' } writer.writerow(item)
def write_to_json(results, filename): """Write an iterable of `CloseApproach` objects to a JSON file. The precise output specification is in `README.md`. Roughly, the output is a list containing dictionaries, each mapping `CloseApproach` attributes to their values and the 'neo' key mapping to a dictionary of the associated NEO's attributes. :param results: An iterable of `CloseApproach` objects. :param filename: A Path-like object pointing to where the data should be saved. """ with open(filename, 'w') as outfile: output = list() for approach in results: values = approach.serialize() item = { 'datetime_utc': helpers.datetime_to_str(values['datetime_utc']), 'distance_au': float(values['distance_au']), 'velocity_km_s': float(values['velocity_km_s']), 'neo': { 'designation': str(values['designation']), 'name': str(values['name']), 'diameter_km': float(values['diameter_km']), 'potentially_hazardous': bool(values['potentially_hazardous']) } } output.append(item) json.dump(output, outfile, indent=2)
def time_str(self): dt = self.time # TODO: Use this object's `.time` attribute and the `datetime_to_str` function to # build a formatted representation of the approach time. # TODO: Use self.designation and self.name to build a fullname for this object. return datetime_to_str(dt)
def __str__(self): """Return `str(self)`.""" time = datetime_to_str(self.time) return (f"On {time}, '{self.designation}' approaches Earth at a " f"distance of {self.distance} au and a velocity of " f"{self.velocity} km/s.")
def time_str(self): """Return string version of time attribute.""" return datetime_to_str(self.time)
def time_str(self): return datetime_to_str(self.time)