A "Best of the Best Practices" (BOBP) guide to developing in Python.
- "Build tools for others that you want to be built for you." - Kenneth Reitz
- "Simplicity is alway better than functionality." - Pieter Hintjens
| # Ask for the user password | |
| # Script only works if sudo caches the password for a few minutes | |
| sudo true | |
| # Install kernel extra's to enable docker aufs support | |
| # sudo apt-get -y install linux-image-extra-$(uname -r) | |
| # Add Docker PPA and install latest version | |
| # sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9 | |
| # sudo sh -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list" |
| # http://flask.pocoo.org/snippets/88/ | |
| import os, sqlite3 | |
| from cPickle import loads, dumps | |
| from time import sleep | |
| try: | |
| from thread import get_ident | |
| except ImportError: | |
| from dummy_thread import get_ident |
| # Assuming an Ubuntu Docker image | |
| $ docker run -it <image> /bin/bash |
| # Add the following 'help' target to your Makefile | |
| # And add help text after each target name starting with '\#\#' | |
| help: ## Show this help. | |
| @fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//' | |
| # Everything below is an example | |
| target00: ## This message will show up when typing 'make help' | |
| @echo does nothing |
| /* | |
| * This script will download a package (and all of its dependencies) from the | |
| * online NPM registry, then create a gzip'd tarball containing that package | |
| * and all of its dependencies. This archive can then be copied to a machine | |
| * without internet access and installed using npm. | |
| * | |
| * The idea is pretty simple: | |
| * - npm install [package] | |
| * - rewrite [package]/package.json to copy dependencies to bundleDependencies | |
| * - npm pack [package] |
| import time | |
| import requests | |
| import zlib | |
| #!pip install lz4 pylzma zstd | |
| import lz4.block | |
| import pylzma as lzma | |
| import zstd | |
| def measure_time_and_compress_decompress(compress_func, decompress_func, data, *args): | |
| # Measure compression time |
| # taken from http://www.piware.de/2011/01/creating-an-https-server-in-python/ | |
| # generate server.xml with the following command: | |
| # openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes | |
| # run as follows: | |
| # python simple-https-server.py | |
| # then in your browser, visit: | |
| # https://localhost:4443 | |
| import BaseHTTPServer, SimpleHTTPServer | |
| import ssl |
| /* | |
| * I add this to html files generated with pandoc. | |
| */ | |
| html { | |
| font-size: 100%; | |
| overflow-y: scroll; | |
| -webkit-text-size-adjust: 100%; | |
| -ms-text-size-adjust: 100%; | |
| } |
Each of these commands will run an ad hoc http static server in your current (or specified) directory, available at http://localhost:8000. Use this power wisely.
$ python -m SimpleHTTPServer 8000