mydream378 Chapter6 Code Review #3

Open
AIopenmind007 opened this Issue on 3 Oct 2017 · 0 comments

Comments

Projects
Projects
None yet
1 participant
Lock conversation

Lock conversation on this issue

  • Other users can’t add new comments to this issue.
  • You and other members of teams with write access to this repository can still leave comments that others can see.
  • You can always unlock this issue again in the future.

Optionally, choose a reason for locking that others can see. Learn more about when it’s appropriate to lock conversations.

@AIopenmind007

AIopenmind007 commented on 3 Oct 2017

1. 代码格式问题:

这个文件 get_weather.py:

More details
--- 
+++ 
@@ -6,10 +6,11 @@
 reload(sys)
 sys.setdefaultencoding('utf-8')
 
+
 def get_weather(city_name):
     url = 'https://api.seniverse.com/v3/weather/now.json?key=tyx1q4fjgqjmlu42'
-    locality= "&location=" + city_name
-    final_url=url+locality+"&language=zh-Hans&unit=c"
+    locality = "&location=" + city_name
+    final_url = url + locality + "&language=zh-Hans&unit=c"
 
     r = requests.get(final_url)
     return_dict = r.json()
@@ -20,6 +21,6 @@
     # # weather_report = f‘city_name+' : '+ current_dict['text']+ " ; " +'温度 : '+current_dict['temperature']+'度'
     a = current_dict['text']
     b = current_dict['temperature']
-    weather_report = "%s: %s 温度:%s℃" %(city_name,a,b)
+    weather_report = "%s: %s 温度:%s℃" % (city_name, a, b)
 
     return weather_report

这个文件 handle.py:

More details
--- 
+++ 
@@ -5,11 +5,12 @@
 import web
 from get_weather import get_weather
 
+
 class Handle(object):
     def POST(self):
         try:
             webData = web.data()
-            print "Handle Post webdata is ", webData   #后台打日志
+            print "Handle Post webdata is ", webData  #后台打日志
             recMsg = receive.parse_xml(webData)
             if isinstance(recMsg, receive.Msg) and recMsg.MsgType == 'text':
                 toUser = recMsg.FromUserName

这个文件 main.py:

More details
--- 
+++ 
@@ -2,11 +2,9 @@
 from handle import Handle
 
 urls = (
-    '/wx', 'Handle',
-)
+    '/wx',
+    'Handle', )
 
 if __name__ == '__main__':
     app = web.application(urls, globals())
     app.run()
-
-

这个文件 receive.py:

More details
--- 
+++ 
@@ -1,4 +1,5 @@
 import xml.etree.ElementTree as ET
+
 
 def parse_xml(web_data):
     if len(web_data) == 0:
@@ -10,6 +11,7 @@
     elif msg_type == 'image':
         return ImageMsg(xmlData)
 
+
 class Msg(object):
     def __init__(self, xmlData):
         self.ToUserName = xmlData.find('ToUserName').text
@@ -18,10 +20,12 @@
         self.MsgType = xmlData.find('MsgType').text
         self.MsgId = xmlData.find('MsgId').text
 
+
 class TextMsg(Msg):
     def __init__(self, xmlData):
         Msg.__init__(self, xmlData)
         self.Content = xmlData.find('Content').text.encode("utf-8")
+
 
 class ImageMsg(Msg):
     def __init__(self, xmlData):

这个文件 reply.py:

More details
--- 
+++ 
@@ -1,10 +1,13 @@
 import time
+
 
 class Msg(object):
     def __init__(self):
         pass
+
     def send(self):
         return "success"
+
 
 class TextMsg(Msg):
     def __init__(self, toUserName, fromUserName, content):
@@ -25,7 +28,8 @@
         </xml>
         """
         return XmlForm.format(**self.__dict)
-    
+
+
 class ImageMsg(Msg):
     def __init__(self, toUserName, fromUserName, mediaId):
         self.__dict = dict()
@@ -33,6 +37,7 @@
         self.__dict['FromUserName'] = fromUserName
         self.__dict['CreateTime'] = int(time.time())
         self.__dict['MediaId'] = mediaId
+
     def send(self):
         XmlForm = """
         <xml>

2. 代码相似问题:

这些代码非常相似,建议抽象:

reply.py 文件中的 10 至 28 行:

    def __init__(self, toUserName, fromUserName, content):
        self.__dict = dict()
        self.__dict['ToUserName'] = toUserName
        self.__dict['FromUserName'] = fromUserName
        self.__dict['CreateTime'] = int(time.time())
        self.__dict['Content'] = content

    def send(self):
        XmlForm = """
        <xml>
        <ToUserName><![CDATA[{ToUserName}]]></ToUserName>
        <FromUserName><![CDATA[{FromUserName}]]></FromUserName>
        <CreateTime>{CreateTime}</CreateTime>
        <MsgType><![CDATA[text]]></MsgType>
        <Content><![CDATA[{Content}]]></Content>
        </xml>
        """
        return XmlForm.format(**self.__dict)
    

reply.py 文件中的 30 至 49 行:

    def __init__(self, toUserName, fromUserName, mediaId):
        self.__dict = dict()
        self.__dict['ToUserName'] = toUserName
        self.__dict['FromUserName'] = fromUserName
        self.__dict['CreateTime'] = int(time.time())
        self.__dict['MediaId'] = mediaId
    def send(self):
        XmlForm = """
        <xml>
        <ToUserName><![CDATA[{ToUserName}]]></ToUserName>
        <FromUserName><![CDATA[{FromUserName}]]></FromUserName>
        <CreateTime>{CreateTime}</CreateTime>
        <MsgType><![CDATA[image]]></MsgType>
        <Image>
        <MediaId><![CDATA[{MediaId}]]></MediaId>
        </Image>
        </xml>
        """
        return XmlForm.format(**self.__dict)

3. 复杂度及其他问题:

文件 handle.py 的 12-12 行

Error: Missing parentheses in call to 'print' (, line 12)

Select a reply ctrl .

The content you are editing has changed. Please try again.

Attach files by dragging & dropping, selecting them, or pasting from the clipboard. Uploading your files… We don’t support that file type. with a GIF, JPEG, JPG, PNG, DOCX, GZ, LOG, PDF, PPTX, TXT, XLSX or ZIP. Attaching documents requires write permission to this repository. with a GIF, JPEG, JPG, PNG, DOCX, GZ, LOG, PDF, PPTX, TXT, XLSX or ZIP. We don’t support that file type. with a GIF, JPEG, JPG, PNG, DOCX, GZ, LOG, PDF, PPTX, TXT, XLSX or ZIP. Yowza, that’s a big file with a file smaller than 10MB. This file is empty. with a file that’s not empty. This file is hidden. with another file. Something went really wrong, and we can’t process that file.

Nothing to preview

@ZoomQuiet
Select a reply ctrl .