def main(): handler = StderrHandler() # handler.formatter = color_formatter handler.level = 2 nullhandler = NullHandler() with nullhandler.applicationbound(): with handler.applicationbound(): with catch_exceptions(""): try: dispatch_command(urltomarkdown) except SystemExit as e: # catch_exceptions is a bit too catchy pass
"humidity": { "value": _humidity, "unit": "%" } }) def send_to_server(data): token = secrets["token"] response = requests.post(url=secrets["collector_url"], data={ "token": token, "data": data }) if response.status_code != 200: raise Exception( "data submission was not successfull: status code {code}".format( code=response.status_code)) if __name__ == "__main__": with logger.applicationbound(): with logbook.catch_exceptions(): try: scale = BeeHiveScale(offset=settings.OFFSET, slope=settings.SLOPE) dht = DHTSensorController(gpio_pin=17) send_to_server(data=read_to_json(scale=scale, dht=dht)) finally: cleanup()
d = json.loads(socket.recv()) if tweet: tweet.insert({'text': d['text'],\ 'location': d['user']['location'], \ 'uid': d['user']['id'], 'tid': d['id'],\ 'created_at': d['created_at'],\ 'username': d['user']['name'],\ 'retweet_count': d['retweet_count'],\ 'screen_name': d['user']['screen_name'],\ 'profile_image_url': d['user']['profile_image_url']}) print "%s ===added to db===", time.ctime() except Exception as e: log_handler.write(e.message) if __name__ == '__main__': with catch_exceptions(): try: with open(LOCK_FILE, 'w') as f: f.write(str(getpid())) store_live_tweets() except IndexError as e: log_handler.write(e.message) except KeyboardInterrupt: log_handler.write('keyboard interrupt\n') finally: try: with open(LOCK_FILE, 'r') as f: #check file exists, I felt this one is better than #os.path.isfile # http://stackoverflow.com/questions/82831/\ #how-do-i-check-if-a-file-exists-using-python
def log_exception_ctx(): with null_handler: with err_handler: with catch_exceptions(): yield