def load_approaches(cad_json_path='data/cad.json'): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ with open(cad_json_path, 'r') as file: cads_data = sorted(json.load(file)['data']) cads_dict = {} for i in range(len(cads_data)): des = cads_data[i][0] time = cads_data[i][3] distance = cads_data[i][5] velocity = cads_data[i][7] if des not in cads_dict: cads_dict[des] = [] cads_dict[des].append( {time: CloseApproach(des, time, distance, velocity)}) else: cads_dict[des].append( {time: CloseApproach(des, time, distance, velocity)}) return cads_dict
def load_approaches(cad_json_path): with open(cad_json_path, 'r') as infile: contents = json.load(infile) fields = contents['fields'] """approaches are stored in approaches list where get reuired data and create CloseApproach object and stroe into appproaces list """ # key for get index in json data for object key = {} for i in range(len(fields)): key[fields[i]] = i data = contents['data'] count = 0 for approach in data: approach_data = { 'des': approach[key['des']], 'cd': approach[key['cd']], 'dist': approach[key['dist']], 'v_rel': approach[key['v_rel']], } cad = CloseApproach(**approach_data) """check if that cad designation neos is eist or not if exist then refernece tha cad to neo and vice vers """ if cad._designation in neos_id: neo_index = neos_id[cad._designation] neo = neos[neo_index] cad.setNeo(neo) neo.addCad(cad) neos[neo_index] = neo approaches.append(cad) return list(set(approaches))
def get(self): query = CloseApproach.all().order('-approach_date') fetch_count = 20 close_approaches = query.fetch(fetch_count) values = { 'close_approaches': close_approaches, 'fetch_count': fetch_count } path = os.path.join(os.path.dirname(__file__), 'main.html') self.response.out.write(template.render(path, values))
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ approaches = [] with open(cad_json_path) as infile: content = json.load(infile) data = content['data'] fields = {} for key in content['fields']: fields[key] = content['fields'].index(key) for row in data: approach = CloseApproach( designation=row[fields['des']], time=row[fields['cd']], distance=row[fields['dist']], velocity=row[fields['v_rel']], ) approaches.append(approach) return approaches
def load_approaches(cad_json_path): # get the data from the json file infile = pathlib.Path(cad_json_path) with open(infile, 'r') as input: raw_data = json.load(input) # create a list of fields fields = list() # create a list of approaches with complete row data raw_approaches = list() # do the processing of data for item in raw_data: # find the dict with fields and values if item == 'fields': for item in raw_data[item]: fields.append(item) # find the dict with data and add them to approaches list if item == 'data': for list_item in raw_data[item]: raw_approaches.append(list_item) # helper to get the indexes of the fields in json file fields_dict = build_dict_of_fields(fields) # get a list of approaches based on the index of fields from raw_approaches approaches = build_list_of_approaches(raw_approaches, fields_dict) list_of_ca_objects = list() # finally we can create a list of CAs based on keywords for approach in approaches: list_of_ca_objects.append(CloseApproach(designation=approach['des'], time=approach['cd'], distance=approach['dist'], velocity=approach['v_rel'])) return list_of_ca_objects
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ cad = [] with open(cad_json_path, 'r') as infile: contents = json.load(infile) # Parse JSON data into a Python object. fields = contents['fields'] key = {} for i in range(len(fields)): key[fields[i]] = i data = contents['data'] for approach in data: info = { '_designation': approach[key['des']], 'time': approach[key['cd']], 'distance': approach[key['dist']], 'velocity': approach[key['v_rel']], } cad.append(CloseApproach(**info)) return cad
def ca_objects(approach_lst): for ca in approach_lst: return CloseApproach(des=ca['des'], time=ca['cd'], distance=ca['dist'], velocity=ca['v_rel'])
def load_approaches(cad_json_path): """ Returns close approach list from a JSON file. Parameters: cad_json_path (str): A path to a JSON file containing data about close approaches. Returns: approeaches (list): A collection of CloseApproaches. """ approaches = list() with open(cad_json_path) as f: cad = json.load(f) for ca in cad['data']: des = ca[0] cd = ca[3] dist = ca[4] v_rel = ca[7] approach = CloseApproach(designation=des, time=cd, distance=dist, velocity=v_rel) approaches.append(approach) return approaches
def get(self, approach_id = None): try: ca = CloseApproach.get(approach_id) values = { 'ca': ca } path = os.path.join(os.path.dirname(__file__), 'miss.html') self.response.out.write(template.render(path, values)) except: self.redirect('/')
def load_to_generator(): with open(cad_json_path, 'r') as infile: data = json.load(infile)['data'] for line in data: yield CloseApproach(designation=line[0], time=line[3], distance=line[4], velocity=line[-4], neo=NearEarthObject(''))
def load_approaches(cad_json_path): result = [] with open(cad_json_path, mode='r') as json_file: approaches_dict = json.load(json_file) for row in approaches_dict['data']: designation = row[0] time = row[3] distance = float(row[4]) velocity = float(row[7]) result.append(CloseApproach(time, distance, velocity, designation)) return result
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ with open(cad_json_path) as f: result = json.load(f) return [ CloseApproach(des=x[0], cd=x[3], dist=x[4], v_rel=x[7]) for x in result['data'] ]
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ with open(cad_json_path) as json_file: data = json.load(json_file)['data'] return tuple((CloseApproach(designation=row[0], time=row[3], distance=row[4], velocity=row[7]) for row in data))
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param cad_json_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ approaches_list = [] with open(cad_json_path, "r") as f: file = json.load(f) approaches = file["data"] for line in approaches: approaches_list.append( CloseApproach(line[0], line[3], line[4], line[7])) return approaches_list
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. """ results_obj = (CloseApproach.serialize(x) for x in results) with open(filename, 'w') as f: json.dump(list(results_obj), f)
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ json_data = [] with open(cad_json_path) as json_to_read: data_json = json.load(json_to_read) for each_row_json in data_json['data']: each_row_json = dict(zip(data_json['fields'], each_row_json)) json_data.append(CloseApproach(**each_row_json)) return json_data
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ approaches = [] # created blank dictionary with open(cad_json_path) as cadinfile: data = json.load(cadinfile) for i in data['data']: caddata = {'des': i[0], 'cd': i[3], 'v_rel': i[7], 'dist': i[4]} cad = CloseApproach(**caddata) approaches.append(cad) return approaches
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ approaches = [] with open(cad_json_path, 'r') as f: cad_json = json.load(f) cad_fields = cad_json['fields'] cad_data = cad_json['data'] for cad_datum in cad_data: cad_dict = dict(zip(cad_fields, cad_datum)) approaches.append(CloseApproach(**cad_dict)) return tuple(approaches)
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param cad_json_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ with open(cad_json_path, 'r') as approaches: approaches_collection = [] file = json.load(approaches) fields = file['data'] for field in fields: approach = CloseApproach(field[0], field[3], field[4], field[7]) approaches_collection.append(approach) return approaches_collection
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ cads = [] with open(cad_json_path, 'r') as infile: cad_dict = json.load(infile) for cad in cad_dict['data']: new_cad = CloseApproach(cad[0], cad[3], cad[5], cad[7]) cads.append(new_cad) return cads
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ list_ca = [] with open(cad_json_path, 'r') as f: data = json.load(f) for elem in data['data']: list_ca.append(CloseApproach(designation=elem[0], time=elem[3], distance=float(elem[4]), velocity=float(elem[7]))) return list_ca
def load_approaches(cad_json_path='data/cad.json'): """Load cloase approaches and unpack them in order to build objects.""" with open(cad_json_path) as f: cad = json.load(f) # I further organized this json object by creating a dictionary to map # field names to each value. fields = cad['fields'] cad_dicts = [] for object in cad['data']: section = {} x = 0 for label in fields: section[label] = object[x] x += 1 cad_dicts.append(section) approaches = [] with open('neo_dicts.pkl', 'rb') as f: neo_dicts = pkl.load(f) for approach in cad_dicts: a = CloseApproach(approach) neo = neo_dicts.get(a._designation, None) a.neo = neo approaches.append(a) return approaches
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ """ {'pdes':elem['pdes'], 'name': elem['name'], 'phs': elem['pha'], 'diameter': elem['diameter']} """ closeApproach = [] with open(cad_json_path, 'r') as fileObject: approachesObj = json.load(fileObject) for approach in approachesObj['data']: closeApproach.append(CloseApproach( approach[0], time=approach[3], distance=float(approach[4]), velocity=float(approach[7]))) return closeApproach
def load_approaches(cad_json_path) -> list: """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ approach_objects = list() with open(cad_json_path, 'r') as approaches_file: approaches_info = json.load(approaches_file) for approach_info in approaches_info["data"]: approach_args = dict(zip(approaches_info["fields"], approach_info)) approach_objects.append(CloseApproach(**approach_args)) return approach_objects
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param cad_json_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ with open(cad_json_path) as f: cad = json.load(f) fields = cad['fields'] cas = [] for c in cad['data']: data = dict(zip(fields, c)) ca = CloseApproach(**data) cas.append(ca) return cas
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ # TODO: Load close approach data from the given JSON file. with open(cad_json_path, 'r') as fh: reader = json.load(fh) approaches = [] for approach in reader['data']: approaches.append( CloseApproach(designation=approach[0], time=approach[3], distance=approach[4], velocity=approach[7])) return approaches
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ coll_cat = [] with open(cad_json_path, 'r') as cad_json_file: cad_file = json.load(cad_json_file) for approach_item in cad_file['data']: ca_item = dict(zip(cad_file['fields'], approach_item)) coll_cat.append( CloseApproach(designation=ca_item['des'], time=ca_item['cd'], distance=float(ca_item['dist']), velocity=float(ca_item['v_rel']))) return coll_cat
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ approaches = [] with open(cad_json_path, 'r') as infile: approachVals = json.load(infile) for approach in approachVals['data']: approaches.append( CloseApproach(approach[0], time=approach[3], distance=float(approach[4]), velocity=float(approach[7]))) return approaches
def load_approaches(cad_json_path='data/cad.json'): """Read close approach data from a JSON file. :param cad_json_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ ca_lst = [] with open(cad_json_path, 'r') as f: reader = json.load(f) for row in reader['data']: time = row[3] distance = float(row[4]) velocity = float(row[7]) designation = row[0] ca = CloseApproach(time, distance, velocity, designation) ca_lst.append(ca) return ca_lst
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param cad_json_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ """ A list for keeping all the `CloseApproach`s created from each JSON `data` entry """ cad_list = [] with open(cad_json_path, 'r') as cad_file_obj: json_reader = json.loads(cad_file_obj.read()) cad_fields = json_reader['fields'] for entry in json_reader['data']: cad = {} for index, value in enumerate(cad_fields): cad[value] = entry[index] cad_list.append(CloseApproach(**cad)) return cad_list
def get(self): query = CloseApproach.all().order('-approach_date') close_approaches = query.fetch(20) feed_items = [] base_url = 'http://justmissedearth.appspot.com' # TODO: Clean this up for ca in close_approaches: feed_items.append(PyRSS2Gen.RSSItem( title = '"%s" just missed earth...' % ca.object_name, link = '%s/misses/%s' % (base_url, ca.key()), description = self._render_feed_description(ca), guid = PyRSS2Gen.Guid('%s/misses/%s' % (base_url, ca.key())), pubDate = ca.approach_date)) rss = PyRSS2Gen.RSS2( title = "just missed earth - latest misses", link = "%s/feed" % base_url, description = "just missed earth - latest misses", lastBuildDate = datetime.now(), items = feed_items) self.response.headers['Content-Type'] = 'application/rss+xml' self.response.out.write(rss.to_xml(encoding = 'utf-8'))
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param neo_csv_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ cads = list() with open(cad_json_path, 'r') as infile: contents = json.load(infile) for row in contents['data']: cads.append( CloseApproach( des=row[0], time=row[3], dist=row[5], # using min distance here vrel=row[7])) return cads
def load_approaches(cad_json_path): """Read close approach data from a JSON file. :param cad_json_path: A path to a JSON file containing data about close approaches. :return: A collection of `CloseApproach`es. """ with open(cad_json_path) as f: close_approaches = [] ca_dict = json.load(f) header = ca_dict["fields"] for row in ca_dict["data"]: close_approaches.append( CloseApproach( designation=row[header.index("des")], time=row[header.index("cd")], distance=row[header.index("dist")], velocity=row[header.index("v_rel")], ) ) return close_approaches