2017年1月15日17:53:10

前面第一个版本搞得我涕泪交加,老实说我现在很累,但为了更好的开展接下来的工作,不得不先将针对第一个版本的优化点罗列一下:

1.界面友好

“提示用户输入-->用户输入-->打印结果”,整个过程很简单,但还不充足,至少还需要添加:

  • 欢迎信息打印。
  • 界面布局调整。
  • 提供支持信息。

2.体验增强

用户在查询一个城市之后需要再次运行程序来查询其他城市,为了支持循环查询,需要根据用户输入的指令完成更多样化的查询功能。

3.效率提升

当前的实现是每一次查询都需要去读取文件内容、重构字典数据,其实在一开始就可以讲字典数据构造好,之后查询时直接从字典里面获取查询结构。

4.异常处理

目前可以预见的异常有两种:

  • 没有用户输入城市的数据。
  • 当天气预报数据增大,不能使用字典进行存放。

优化一:界面友好

优化之后的脚本名称更改为WQuery,版本信息为0.1。运行效果如下:

不足:代码混乱。想到的解决方法:将显示与逻辑相分离,分别使用函数进行封装。

优化二:体验增强

想要支持循环查询,必须要修改查询逻辑,将主动权放到用户手中,优化之后版本信息为0.2。运行效果如下:

异常处理

我要回家吃饭!... 我先简单点儿了,仅仅针对不存在的城市提供错误提示,软件版本递进到0.3,运行效果如下:

模块化

对于超大天气文件数据的支持目前没有时间做了,最后将代码用函数来进行模块化,以此最为本周任务的结束吧。

刚去提交作业,发现需求漏掉了,急忙补上。

演示效果:

成品代码:

# -*- coding: utf-8 -*-
import sys

encoding_type = sys.getfilesystemencoding()
weather_report = {}
history_record = {}

def print_welcom():
    print '\n'
    print u" ----------------------------------".encode(encoding_type)
    print u"  -   欢迎使用简易天气查询软件   -".encode(encoding_type)
    print u"            WQuery 1.1            ".encode(encoding_type)
    print "          [email protected]       "
    print '\n'

def print_bye():
    print " -                                -"
    print u"          谢谢使用,再见!          ".encode(encoding_type)
    print " ----------------------------------"

def print_manul():
    print u"      输入<城市名称> 查询城市天气       ".encode(encoding_type)
    print u"      输入<  help > 查看帮助信息       ".encode(encoding_type)
    print u"      输入<history> 打印历史记录       ".encode(encoding_type)
    print u"      输入<  quit > 退出查询软件       ".encode(encoding_type)
    print '\n'

def print_history():
    for c, w in history_record.items():
        city = c.decode('utf-8').encode(encoding_type)
        weather = w.decode('utf-8').encode(encoding_type)
        print u" %s --> %s".encode(encoding_type) % (city, weather)

def build_weather_dictionary():
    with open('weather_info.txt') as weather_info:
        for line in weather_info:
            pair = line.split(',')
            weather_report[pair[0]] = pair[1]

def lookup_city_weather(query_city):
    city = query_city.decode('utf-8').encode(encoding_type)
    if weather_report.has_key(query_city):
        history_record[query_city] = weather_report[query_city]
        weather = weather_report[query_city].decode('utf-8').encode(encoding_type)
        print u"> %s --> %s".encode(encoding_type) % (city, weather)
    else:
        print u" OOPS...您输入的城市[%s]不存在,请重试。".encode(encoding_type) %city
        print_manul()

def query_weather():
    print_manul()
    while (True):
        print u" 请输入要查询天气的城市名称:".encode(encoding_type)
        query_city = raw_input("> ").decode(sys.stdin.encoding).encode('utf-8')

        if query_city == 'quit':
            break
        elif query_city == 'help':
            print_manul()
        elif query_city == 'history':
            print_history()
        else:
            lookup_city_weather(query_city)

print_welcom()
build_weather_dictionary()
query_weather()
print_bye()

results matching ""

    No results matching ""