Skip to content

Instantly share code, notes, and snippets.

@CamilleMo
Last active February 6, 2019 11:02
Show Gist options
  • Save CamilleMo/cbd133d41413e51949b4c6b988cb1a6a to your computer and use it in GitHub Desktop.
Save CamilleMo/cbd133d41413e51949b4c6b988cb1a6a to your computer and use it in GitHub Desktop.
A starting point
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
[bitbucket.org]
User = hg
[topsecret.server.com]
Port = 50022
ForwardX11 = no
#!usr/bin/env python
# -*- coding: utf-8 -*-
"""
Python version : Python 3.6
Script purpose : This is a starting point
Date : 2019-02-06
Author : Camille Moatti
Maintenance : Camille Moatti - camille.moatti@gmail.com
"""
#### Libraries
# Standard library
import sys
import logging
import configparser
import traceback
import pdb
# Third-Party Libraries
# If Pandas is a dependency, a good practice is to consider chained assignments as an error :
import numpy as np
import pandas as pd
pd.set_option('mode.chained_assignment', 'raise')
# it should avoid "SettingWithCopyWarning"
### logger config :
# create logger
logger = logging.getLogger('MyScript') # s'affiche après la date
logger.setLevel(logging.DEBUG)
ch = logging.FileHandler("logs.log")
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
# Usage example :
#logger.debug('debug message')
#logger.info('info message')
#logger.error('error message')
#logger.critical('critical message')
### ini file config :
config = configparser.ConfigParser()
config.read('configuration.ini')
# Usage example :
# >>> config.sections()
# ['bitbucket.org', 'topsecret.server.com']
# >>> 'bitbucket.org' in config
# True
# >>> 'bytebong.com' in config
# False
# >>> config['bitbucket.org']['User']
# 'hg'
# >>> config['DEFAULT']['Compression']
# 'yes'
# >>> topsecret = config['topsecret.server.com']
# >>> topsecret['ForwardX11']
# 'no'
# >>> topsecret['Port']
# '50022'
# >>> for key in config['bitbucket.org']: print(key)
# ...
# user
# compressionlevel
# serveraliveinterval
# compression
# forwardx11
# >>> config['bitbucket.org']['ForwardX11']
# 'yes'
###
# get arguments if any
if sys.argv[0]:
argument1 = sys.argv[0]
def wrong_div():
return 8/0
def main():
# some WIP code that maybe raises an exception
#raise BaseException("oh no, exception!")
print(2)
wrong_div()
return 0
if __name__ == "__main__":
try:
ret = main()
except:
traceback.print_exc()
logger.error(traceback.format_exc())
pdb.post_mortem()
sys.exit(ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment