Commits

ubershmekel committed 930de57

A bit cleaning up and fixed bug about "0 miles" cases.

Comments (0)

Files changed (2)

         # number is between 1 and -1
         # make sure that we only show 2 digits other than zero.
         # eg 0.00001234521 will become 0.000012
-        prec_point = math.log(abs(number)) / math.log(10)
-        prec_point = math.floor(prec_point) - 1
-        prec_fac = decimal.Decimal(10) ** int(prec_point)
-        number = decimal.Decimal(number).quantize(prec_fac)
-        # normalize removes trailing zeroes (eg 0.30 turns into 0.3)
-        return '{0}'.format(number.normalize())
+        if number == 0:
+            return '0'
+        else:
+            prec_point = math.log(abs(number)) / math.log(10)
+            prec_point = math.floor(prec_point) - 1
+            prec_fac = decimal.Decimal(10) ** int(prec_point)
+            number = decimal.Decimal(number).quantize(prec_fac)
+        
+            # normalize removes trailing zeroes (eg 0.30 turns into 0.3)
+            return '{0}'.format(number.normalize())
+
     
 
 def suspected_converted_already(text ):
     avoid_subreddits = []
     
     def handle_comment(self, comm):
+        #import pdb;pdb.set_trace()
+        # permalink log is expensive timewise
+        #self.log('parent: ' + comm.submission.permalink)
         self.log('body: %s' % comm.body)
         response = metric_response(comm.body)
         if response != '':
         return False
         
     def handle_link(self, link):
+        self.log('link: ' + link.permalink)
         self.log('title: ' + link.title)
         response = metric_response(link.title)
         if response != '':
         self.log(log_line)
         print log_line
     
+    def handle(self, object, method):
+        res = False
+        try:
+            res = method(object)
+        except reddit.errors.RateLimitExceeded, e:
+            self.handle_time_block(e)
+        except reddit.errors.APIException, e:
+            # eg comment deleted
+            logging.error('reddit.errors.APIException: ' + str(e))
+        except urllib2.URLError:
+            logging.error('urllib2.URLError: ' + str(e))
+        
+        return res
+    
     def feed_comments(self):
         r = self.r
         comments = []
 
             # handle
             comments.append(comm)
-            res = False
-            try:
-                res = self.handle_comment(comm)
-            except reddit.errors.RateLimitExceeded, e:
-                self.handle_time_block(e)
-            except reddit.errors.APIException, e:
-                # eg comment deleted
-                logging.error('reddit.errors.APIException: ' + str(e))
-                
+            
+            res = res = self.handle(comm, self.handle_comment)
+            
             if res:
                 responded += 1
                 self.report_handled(comm, self.comment_place_holder)
             
             # handle
             links.append(link)
-            res = False
-            try:
-                res = self.handle_link(link)
-            except reddit.errors.RateLimitExceeded, e:
-                self.handle_time_block(e)
-            except reddit.errors.APIException, e:
-                # eg comment deleted
-                logging.error('reddit.errors.APIException: ' + str(e))
+            
+            res = self.handle(link, self.handle_link)
+            
             if res:
                 responded += 1
                 self.report_handled(link, self.link_place_holder)