Dark Launch

This is a Dark Launch.

Fix ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

To fix the "No module named MySQLdb" error, you need to install MySQL support for Python; do the following:

Code
pip install MySQL-python

NOTE: Don't use sudo with pip. Fix your directory permissions if you find yourself needing to use sudo.

Other errors:

Code
EnvironmentError: mysql_config not found
fix with:
Code
sudo apt-get install libmysqlclient15-dev
Code
Python.h: No such file or directory
fix with
Code
sudo apt-get install python2.7-dev

JavaScript Hungarian Notation Variable Prefix Naming Convention

Hungarian Notation is a language agnostic naming convention that prefixes variables with the variable's type. This allows the reader to determine the type and use of a variable by such an identifier.

JavaScript Hungarian Prefixes:

  • a - array
  • b - boolean
  • f - float
  • fn - function
  • i - integer
  • n - node
  • o - object
  • s - string
Javascript
var aData = [1, 2, 3];
var bFound = false;
var fGoldenRatio = 1.618;
var fnCallback = function() { };
var iCurrentPage = 1;
var nNewRow = document.createElement("tr");
var oSettings = {
    type: "GET",
    url: "test.json",
    dataType: "jsonp"
};
var sLabel = "First Name";

Prefixes may also be combined where appropriate.

Javascript
var asData = "foo,bar,baz".split(","); // Array of type String.

Examples in other languages:

C++
int iNumber = 2;
PHP
$sQuote = "Imagination is more important than knowledge.";
AutoIt
$sMsg = "Example message string."

PHP Return Function Response to Variable rather than Print or Echo it

To capture the function's response or output to a variable, use the following function:

PHP
function get_contents($function) {
    ob_start();
    $function();
    $contents = ob_get_contents();
    ob_end_clean();
 
    return $contents;
}

To use, pass an anonymous function to get_contents().

Examples:

PHP
// Example: get function's response to variable.
function add($this, $that) {
    echo $this . ' + ' . $that . ' = ' . ($this + $that);
}
 
$example = get_contents(function() {
    add(1, 1);
});
 
echo $example . "\n"; // 1 + 1 = 2
PHP
// Example 2: get function's response using return. Even if the function echos output, it can be captured to a variable.
// Compare print_r() that has a return parameter to var_dump() that has no such parameter.
 
$return = true;
$example2 = print_r(array('a', 'b', 'c'), $return);
 
// Solution: use get_contents() to capture the function's response to a variable.
$example3 = get_contents(function() {
    var_dump(array('a', 'b', 'c'));
});
 
echo $example3;
// array(3) {
//   [0]=>
//   string(1) "a"
//   [1]=>
//   string(1) "b"
//   [2]=>
//   string(1) "c"
// }

Note: Anonymous functions are available since PHP 5.3.0.

OSX Install Pip, Virtualenv & VirtualenvWrapper on Mac

To install pip, virtual environment (virtualenv, virtualenvwrapper) on OS X, do the following on the command line:

Code
sudo easy_install pip
sudo pip install virtualenv
sudo pip install virtualenvwrapper
 

To enable the workon tab-completion feature, append the following to your profile file (located at ~/.profile):

Code
source /usr/local/bin/virtualenvwrapper.sh
 

Create a directory to house the virtual environments:

Code
mkdir ~/.virtualenvs
 

Create a new virtual environment for your project:

Code
cd ~/.virtualenvs
virtualenv --no-site-packages myproject
 
  • NOTE: Apple's development tools needs to be installed: Xcode http://itunes.apple.com/us/app/xcode/id448457090
  • The "installer" only downloads the file to Applications -- you still need to install Xcode.
  • Running the installer normally may freeze. If so, right click and choose "Show Package Contents" then look for Xcode.mpkg in /Applications/Install Xcode.app/Contents/Resources/Xcode.mpkg and run that instead.

Bash stdout & write output to file

To display dual output on standard output (stdout) and write or append the output to a file, use tee. For example, do the following:

python ./myscript.py | tee -a output.log

The output from the script is piped to tee and tee prints to stdout and appends to the specified log file.