Last active
December 12, 2015 04:08
-
-
Save charlee/4711535 to your computer and use it in GitHub Desktop.
Programming Cheatsheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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