项目1官方地址:https://github.com/udacity/nd303-c1-advanced-python-techniques-project-starter

1 概述

本项目概言之,实现一个命令行工具来检查和查询近地天体的数据集。

完成后,您将能够检查数据集中的近地天体的属性,并使用以下任意组合的过滤器查询近地天体的数据集:

  • 发生在给定的日期。
  • 在给定的开始日期或之后发生。
  • 在给定的结束日期或之前发生。
  • 以至少(或至多)X天文单位的距离接近地球。
  • 以至少(或最多)Y千米每秒的相对速度接近地球。
  • 直径至少等于(或至少小于)Z公里。
  • 被美国宇航局标记为有潜在危险(或没有)。

学习目标

通过完成本项目,您将展示以下能力:

  • 用Python表示结构化数据。
  • 将结构化文件中的数据提取到Python中。
  • 根据所需的行为在Python中转换数据。
  • 以结构化的方式将结果保存到文件中。

在此过程中,您必须能够:

  • 编写Python函数来转换数据和执行算法。
  • 设计Python类来封装有用的数据类型。
  • 为复杂的实现提供接口抽象。

在这个过程中遇到bug是很正常的,所以很有可能,您还将获得宝贵的调试技能的练习,无论是解释堆栈跟踪、跟踪系统错误、处理和抛出适当的错误、使用pdb遍历代码、使用assert检查先决条件,还是仅仅使用print显示内部状态。

具体任务

  • 任务0:检查数据。(数据/ neos.csv和数据/ cad.json)
  • 任务1:构建模型来表示数据。(models.py)
  • 任务2:将数据提取到自定义数据库中(Extract.py和database.py)
  • 任务3:创建过滤器来查询数据库以生成匹配的close - approach对象流,并限制结果大小。(filters.py和database.py)
  • 任务4:将数据保存到文件中。(write.py)

技能大模块:

  • 单元测试Python3 unittest

    • 类的测试
    • 文件内容测试
    • python版本测试
    • 文件存在否测试
    • 文件格式测试
    • 。。。

2 项目结构

.
├── README.md       # This file.
├── main.py
├── models.py       # Task 1.
├── read.py         # Task 2a.
├── database.py     # Task 2b and Task 3b.
├── filters.py      # Task 3a and Task 3c.
├── write.py        # Task 4.
├── helpers.py
├── data├── neos.csv└── cad.json

Task 1:设计存储数据的两个类


from helpers import cd_to_datetime, datetime_to_strclass NearEarthObject:"""近地天体的基本信息类,处理neos.csv文件"""def __init__(self, **info):"""参数 info: 一个字典.例子:dict_info = {'designation':'编号GT520', 'time':'哈雷彗星', 'diameter':101, 'hazardous':'Y'}neo = NearEarthObject(**dict_info)"""self.designation = (str(info['designation']) if info['designation'] else '')self.name = str(info['name']) if info['name'] else Noneself.diameter = (float(info['diameter']) if info['diameter'] else float('nan'))self.hazardous = True if info['hazardous'] == 'Y' else Falseself.approaches = []#加了@property后,可以用调用属性的形式来调用方法,后面不需要加()@propertydef fullname(self):return f"{self.designation} ({self.name or 'N/A'})"#print(类的实例名)时,会调用此方法。功能相当于此实例的“自我介绍”。def __str__(self):return (f"""{self.fullname} 是一个直径为 """f"""{self.diameter:.3f}km 且 """f"""{'具有' if self.hazardous else '不具有'} 危险的近地天体。""")# print(类的实例名)时,当类中没有__str__方法的时候,会去调用__repr__方法,所以一般类中需要定义repr方法# 也可以直接这样调用__repr__方法:print(repr(类的实例名))# 如果没有自定义__repr__方法和__str__方法,print(类的实例名)时输出默认的__repr__方法:“类名+object at+内存地址”def __repr__(self):return (f"NearEarthObject(designation={self.designation!r}, name={self.name!r}, "f"diameter={self.diameter:.3f}, hazardous={self.hazardous!r})")class CloseApproach:"""临近地球时的状态类,处理cad.json文件"""def __init__(self, **info):"""参数 info: 一个字典."""self._designation = (str(info['designation']) if info['designation']else '')self.time = cd_to_datetime(info['time']) if info['time'] else Noneself.distance = float(info['distance']) if info['distance'] else 0.0self.velocity = float(info['velocity']) if info['velocity'] else 0.0# Create an attribute for the referenced NEO, originally None.self.neo = None@propertydef fullname(self):"""Return a representation of the full name of this NEO."""return f"{self._designation} ({self.neo.name or 'N/A'})"@propertydef time_str(self):return datetime_to_str(self.time)def __str__(self):return (f"""在 {self.time_str}时刻, '{self.fullname}' 将接近地球, """f"""最近距离为 {self.distance:.2f} au,速度为 """f"""{self.velocity:.2f} km/s.""")def __repr__(self):return (f"CloseApproach(time={self.time_str!r}, distance={self.distance:.2f}, "f"velocity={self.velocity:.2f}, neo={self.neo!r})")

NearEarthObject类的测试效果如下:

Task 2:从文件中提取数据放到Python类对象中

1 CSV和json文件数据提取函数

import csv
import jsonfrom models import NearEarthObject, CloseApproachdef load_neos(neo_csv_path):"""Read near-Earth object information from a CSV file.:param neo_csv_path: A path to a CSV file containing data about near-Earth objects.:return: A collection of `NearEarthObject`s."""# TODO: Load NEO data from the given CSV file.neos = []with open(neo_csv_path, 'r') as infile:reader = csv.DictReader(infile)for row in reader:neo = NearEarthObject(designation=row['pdes'],name=row['name'],diameter=row['diameter'],hazardous=row['pha'],)neos.append(neo)return neosdef 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.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

2


class NEODatabase:"""一个包含near-Earth类及其近地时信息的数据库.A `NEODatabase` contains a collection of NEOs and a collection of closeapproaches. It additionally maintains a few auxiliary data structures tohelp fetch NEOs by primary designation or by name and to help speed upquerying for close approaches that match criteria."""def __init__(self, neos, approaches):"""Create a new `NEODatabase`.As a precondition, this constructor assumes that the collections of NEOsand close approaches haven't yet been linked - that is, the`.approaches` attribute of each `NearEarthObject` resolves to an emptycollection, and the `.neo` attribute of each `CloseApproach` is None.However, each `CloseApproach` has an attribute (`._designation`) thatmatches the `.designation` attribute of the corresponding NEO. Thisconstructor modifies the supplied NEOs and close approaches to link themtogether - after it's done, the `.approaches` attribute of each NEO hasa collection of that NEO's close approaches, and the `.neo` attribute ofeach close approach references the appropriate NEO.:param neos: A collection of `NearEarthObject`s.:param approaches: A collection of `CloseApproach`es."""self._neos = neosself._approaches = approachesself._data = {}for approach in self._approaches:if approach._designation not in self._data:self._data[approach._designation] = {'approaches': [],'neo': None,}self._data[approach._designation]['approaches'].append(approach)for neo in self._neos:if neo.designation not in self._data:self._data[neo.designation] = {'approaches': [],'neo': None,}self._data[neo.designation]['neo'] = neoif neo.name not in self._data:self._data[neo.name] = neo.designationneo.approaches.extend(self._data[neo.designation]['approaches'])for approach in self._approaches:approach.neo = self._data[approach._designation]['neo']def get_neo_by_designation(self, designation):"""Find and return an NEO by its primary designation.If no match is found, return `None` instead.Each NEO in the data set has a unique primary designation, as a string.The matching is exact - check for spelling and capitalization if nomatch is found.:param designation: The primary designation of the NEO to search for.:return: The `NearEarthObject` with the desired primary designation, or `None`."""if designation in self._data:return self._data[designation]['neo']return Nonedef get_neo_by_name(self, name):"""Find and return an NEO by its name.If no match is found, return `None` instead.Not every NEO in the data set has a name. No NEOs are associated withthe empty string nor with the `None` singleton.The matching is exact - check for spelling and capitalization if nomatch is found.:param name: The name, as a string, of the NEO to search for.:return: The `NearEarthObject` with the desired name, or `None`."""if name in self._data:return self.get_neo_by_designation(self._data[name])return Nonedef query(self, filters=()):"""Query close approaches to generate those that match a collection of filters.This generates a stream of `CloseApproach` objects that match all of theprovided filters.If no arguments are provided, generate all known close approaches.The `CloseApproach` objects are generated in internal order, which isn'tguaranteed to be sorted meaninfully, although is often sorted by time.:param filters: A collection of filters capturing user-specified criteria.:return: A stream of matching `CloseApproach` objects."""if filters:for approach in self._approaches:if all(map(lambda f: f(approach), filters)):yield approachelse:for approach in self._approaches:yield approach

Task 3:设计数据库条件查询方法

Task 4:保存查询结果

重要知识点:

  1. @property作用:可以用调用属性的形式来调用方法(函数),后面不需要加()。
  2. @classmethod作用:很多博客只是说@calssmethod的作用就是“可以不需要实例化,直接类名.方法名()来调用。这有利于组织代码,把某些应该属于某个类的函数给放到那个类里去,同时有利于命名空间的整洁”。很抽象,其实具体作用如下:

    @classmethod的作用实际是可以在class内实例化class,一般使用在有工厂模式要求时。作用就是比如输入的数据需要清洗一遍再实例化,可以把清洗函数定义在class内部并加上@classmethod装饰器已达到减少代码的目的。总结起来就是:@class method可以用来为一个类创建一些预处理的实例。https://blog.csdn.net/qq_23981335/article/details/103798741

优达学城python项目P1:搜索和探索近地天体(NEOs)相关推荐

  1. 优达学城无人驾驶工程师——P1寻找车道线

    这次介绍的是优达学城的无人驾驶工程师的P1项目,利用车的前摄像头来识别当前车道的左右两边两条的车道线.测试图片和视频在文章最后的链接里. 一开始先倒包 #importing some useful p ...

  2. 优达学城 python 人工智能入门_有人报过优达学城的人工智能入门小班吗,真的有用吗?...

    先自报家门,文科生,研究生学的管理,已过而立之年,0基础. 按要求回答你的问题: 1.人工智能就业前景.说实话,我刚人工智能基础毕业,现在在学深度学习,所以还没转型,暂时还不知道.但是我知道,现在经济 ...

  3. python人工智能入门优达视频_看优达学城python入门视频学习C++

    在如今这个时代,计算机领域中,各种技术瞬息万变,不断更新迭代.随着人工智能.机器学习等先进技术的兴起,近年来,python入门视频是很多人关注的,Python程序员的数量已经大大超过C++程序员.然而 ...

  4. python课程 优达学城_优达学城自动驾驶课程项目——车道检测

    汽车在道路上行驶需要遵循一定的行驶规则,路面的车道则起到规范汽车行驶规则的作用.车道的种类有很多种,如单行线.双行线,虚线.网格线等,不同颜色.形状的车道线代表着不同的行驶规则,汽车和行人可以根据这些 ...

  5. 优达学城计算机视觉pkl,优达学城机器学习工程师纳米学位项目介绍

    本文由 meelo 原创,请务必以链接形式注明 本文地址,简书同步更新地址 一对一的项目辅导是优达学城纳米学位的一大亮点.本文将简要介绍优达学城机器学习工程师纳米学位的6个项目.项目覆盖了机器学习的主 ...

  6. 优达学城_数据清洗_项目三wrangle_act

    下面是我优达学城项目三的记录报告 里面的思路和文字说明大多都在代码块里面的注释中,#后面?,可能不太容易看,需要认真看.? #导入可能需要的包 import os import numpy as np ...

  7. 学习优达学城《无人驾驶入门》,具体需要掌握哪些python知识点?

    在上一篇文章< 002 零基础如何学习优达学城的<无人驾驶入门>?>中,我介绍了学习<无人驾驶入门>需要哪些先修知识,其中,最重要的是python.优达官方对pyt ...

  8. 无人驾驶8: 粒子滤波定位(优达学城项目)

    优达学城无人车定位的项目实现: 粒子滤波算法流程图 粒子滤波的伪代码: step1:初始化 理论上说,当粒子数量足够多时,能准确地呈现贝叶斯后验分布,如果粒子太少,可能漏掉准确位置,粒子数量太多,会拖 ...

  9. 零基础如何学习优达学城的《无人驾驶入门》?

    因为感兴趣,而且看好无人驾驶行业,我学习了优达学城的<无人驾驶入门>课程.最近整理了无人驾驶领域的资料,写成文章分享给大家. 作为系列文章的第一篇,我想介绍一下<无人驾驶入门> ...

最新文章

  1. apache php wordpress,windows 安装 apache php wordpress
  2. 关于python的打包打包完生成exe文件之后,不能正常执行程序的问题解决。
  3. 【STM32】STM32F4系统架构
  4. 云计算技术的跃进睿云智合专业先进水平
  5. 《Easy RL:强化学习教程》出版了!文末送书
  6. 青岛农商银行计算机防病毒应用培训,青岛农商银行胶州支行多元化培训提升安防管理水平...
  7. webpack自定义打包命令
  8. 今天高考,讲几句大实话
  9. 为什么那么多自学WEB前端的人后来都放弃了?
  10. JavaScriptjQuery.带参数的函数
  11. python 字符串数组转换为浮点数,如何在numpy中将字符串数组转换为浮点数数组?...
  12. 《第一行代码》第三版之我的第一行Android代码(一)
  13. coreldraw快速撤回_CDR X8撤销、重做与重复操作方法介绍
  14. 谈判如何在谈判中_工资谈判软件开发人员指南
  15. OCR识别软件(uTools)→{个人笔记记录}
  16. RGB 颜色透明16进制表示
  17. 25岁,我明白了这些道理
  18. 知道公司抢“坑位”是什么意思吗?
  19. python视频操作——python实现将视频分解为图片序列
  20. WebRTC gateway janus入门教程:从配置到插件编写

热门文章

  1. HarmonyOS UI开发 DependentLayout(依赖布局) 的使用
  2. Ubuntu 系统 在终端中过滤log 特殊的信息
  3. Android Dialog 弹框之外的区域 默认透明背景色修改
  4. 【js操作url参数】获取指定url参数值、取指定url参数并转为json对象
  5. LeetCode 35.搜索插入位置
  6. 运行在CentOS7.5上的Django项目时间不正确问题
  7. 数据库Mysql的学习(八)-储存过程和事务和导入导出
  8. 机器学习入门(16)— CNN 池化层概念和特征
  9. RecylerView为item添加点击事件
  10. 给View 添加手势,点击无反应 如何给View添加点击事件,手势方法