Skip to content

Instantly share code, notes, and snippets.

@charlee
Last active December 12, 2015 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlee/4711535 to your computer and use it in GitHub Desktop.
Save charlee/4711535 to your computer and use it in GitHub Desktop.
Programming Cheatsheets
// element dimension
var w = $(this).width();
var h = $(this).height();
// current window(viewport) dimension
var w = $(window).width();
var h = $(window).height();
// document dimension
var w = $(document).width();
var h = $(document).height();
// enclosure a scope
(function(params) {
// real code
})(params);
# check if file exists
os.path.isfile(filename)
# command line parameters parsing
parser = argparse.ArgumentParser()
parser.add_argumoent("-c", "--config", metavar="filename", dest="config_file", help="specify a local config file")
args = parser.parse_args()
config_file = args.config_file
# check root
os.geteuid() == 0
# check python version
sys.version_info < (2, 7)
# exit python script
sys.exit(errmsg) # return 1 to shell
# uppercase/lowercase
str.lower()
str.upper()
# strip whitespace
str.strip()
# copy file
shutil.copyfile(src, dst)
# remove tree
shutil.rmtree('folder_name')
# make directory
os.makedirs(path)
# copy tree
shutil.copytree(src, dst)
# get current datetime as string
from datetime import datetime
datetime.now().strftime("%Y%m%d%H%M%S")
# run shell command and catch stdout/stderr
p = subprocess.Popen(['/bin/ls', '-l'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode == 0:
# success
else:
# fail
# get gid/uid
try:
uid = pwd.getpwnam('apache').pw_uid
gid = grp.getgrnam('apache').gr_gid
except KeyError:
log.error("apache does not exist")
# get git commit count
$ git shortlog -s -n | awk 'BEGIN { count = 0; } { count += $1; } END { print count; }'
# git svn revision
$ svn info | grep "Revision:" | awk '{ print $2; }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment