Web, server and command-line solutions log

Dave Everitt 23 Jun 2013, last updated 12-Jun-2015

Anyone who has to solve problems regularly is advised to keep a solutions log, so they never have to think ‘how did I do that last time?’ I haven't yet put up mine in a searchable or indexed way, so here's a massive long page.

Set up Apache2 for local website testing on Mac OS X 10.5

Relevant files:

in /etc/hosts add a line for each test site:

127.0.0.1 mytestsite.lc
127.0.0.1 myothersite.lc
...

in /etc/apache2/httpd.conf Uncomment the include to httpd-vhosts.conf
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

…and make local Apache only look for local files - change:
Listen 80
to:
Listen 127.0.0.1:80

in /private/etc/apache2/extra/httpd-vhosts.conf
change
NameVirtualHost *:80
to
NameVirtualHost 127.0.0.1:80

Add a VirtualHost block (more than one or just one?) e.g.:

<VirtualHost 127.0.0.1:80>
    DocumentRoot "/Users/deveritt/Sites/mytestsite.com/webapps/html"
    ServerName mytestsite.lc
</VirtualHost>

create a file e.g. /etc/apache2/users/mytestsite.conf by copying yourusername.conf and add directory blocks such as:

<Directory "/Users/deveritt/Sites/mytestsite.com/webapps/html">
    Options Indexes Includes MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
    XBitHack on
</Directory>

Links and resources:

apache-php-and-mysql-on-leopard,
namevirtualhost (local copy)

Tags:

Apache2, OSX 10.5, Leopard, virtualhosts, development environment, localhost

Set up SSI (includes) for html files using the XBitHack

In a Directory block for e.g. mytestsite.conf in VirtualHosts (or .htaccess if no root access), add:

XBitHack on

Then make files that need include directives to be processed executable:

chmod +x myfile.html

Links and resources:

ssi (local copy)

Tags:

Apache2, SSI, includes, server side includes, xbithack

Django can’t find flatpages/default.html, even though it’s in a TEMPLATE_DIRS path

Wherever the templates directories are, there needs to be a directory inside it named the same as the project e.g.

django-templates/cms/flatpages/default.html

Links and resources:

Tags:

django, templates

ensure docutils is installed (use pip or easy_install) then add django.contrib.admindocs to INSTALLED_APPS.

Links and resources:

Tags:

django templates, admindocs, docutils

Stop git tracking files that were deleted manually

Use git add -u.

git add .: looks at the working tree and adds all paths to staged changes if they are either changed or new (and not ignored). It does not stage any ‘rm’ actions.

git add -u: looks at all currently tracked files and stages changes to them if different (or removed). It does not add new files, only changes to files already tracked.

git add -A: a handy shortcut for doing both the above.

Links and resources:

Stack Overflow: difference of git add a and git add,
what git gotchas have you been caught by,
difference between git add and git add u

Tags:

git, deleted files

Use Django from trunk in your user directory instead of from Python’s site-packages directory

E.g. ~yourusername/src/django-trunk, then you can update Django from trunk with svn update.

Links and resources:

docs.djangoproject.com/en/dev/topics/install

Tags:

django, svn, trunk

Install South from trunk when it’s already there from easy_install

Delete the egg from Python’s site-packages, and the relevant line in the easy-install.pth file.

$ svn co https://svn.aeracode.org/svn/south/trunk south
$ setup.py develop (to run in it's svn dir)

in .bash_login add the line:
export PYTHONPATH="/Users/MYUSER/src/south-trunk:$PYTHONPATH"

In the Python shell:

>>> import south
>>> south.__version__

Links and resources:

South docs installation archives

Tags:

python, django, south, trunk

Find out what Python packages are installed and if there are newer versions

Install yolk (sudo easy_install yolk) and use the following to: list all packages; see if there are newer versions:

$ yolk -l
$ yolk -U

Links and resources:

tools.assembla.com/yolk

Tags:

python, easy_install, pip, yolk

Amend the descriptive message for the last git commit

git commit --amend -m "amended message here"

Links and resources:

Tags:

git, message, commit, amend

Create and enable a new MySQL user

  1. Log into MySQL as root:
    $ mysql -h localhost -p -u root Enter password: (your password here)
    Switches used above: -h = host -p prompt for pw -u (name) MySQL username

  2. Set up new database (e.g. wedb) and assign to a MySQL user (e.g. webdev);
    if the user doesn’t yet exist (or to change their password):
    mysql> grant all on wedb.* to webdev@localhost identified by "webdevpass";
    if the user does exist:
    mysql> grant all on wedb.* to webdev@localhost;
    In either case, you should see e.g.:
    Query OK, 0 rows affected (0.08 sec)
    then log out…
    mysql> quit;

  3. log in as the user (e.g. webdev):
    $ mysql -h localhost -p -u webdev
    Enter password: (your password here)
    mysql> create database wedb;
    you should see e.g.:
    Query OK, 1 rows affected (0.01 sec)
    then log out…
    mysql> quit;

  4. (optional) log into the new db:
    $ mysql -h localhost -p -u webdev webdb

Links and resources:

Paul DuBois, ‘MySQL and Perl for the Web’, p13

Tags:

mysql, database

Find what’s on Python’s system path

In the Python shell:

>>> import sys
>>> print sys.path

Links and resources:

Tags:

python, python path

HTML in Javascript causes validation issues

For HTML: escape backslashes in closing tags for XHTML (although it’s now dead):

<script type="text/javascript">
    <![CDATA[
    ... unescaped script content ...
    ]]>
</script>

Links and resources:

www.w3.org/TR/xhtml1/#h-4.8

Tags:

Javascript, HTML, XHTML, validation, tags, entities, escaping, unescaped

Download/update and build NodeBox2 from trunk

cd ~/src (or wherever you keep your source files)
git clone git://github.com/nodebox/nodebox.git

To update:

cd ~/src/nodebox
git pull
sudo ant dist-mac

(see link 2 for ‘sudo’ or not)

Open the dist folder under the NodeBox project folder, right-click the mac folder and select Compress. Rename the archive to also contain the version number, e.g. NodeBox-2.0.963.zip. Note that using the zip command from terminal creates an invalid executable.

Links and resources:

https://github.com/nodebox/nodebox/blob/master/INSTALL.txt
https://github.com/nodebox/nodebox/issues/closed#issue/2

Tags:

python, nodebox, nodebox2

Compile Java classes into a separate directory from their source files

cd to a dir where the ‘src’ and ‘bin’ files reside.

$ javac -d bin src/dir1/dir2/source.java

The -d switch recreates the dir tree to ‘source.class’ in the ‘bin’ dir.

For classes that call others, ‘bin’ should be in the CLASSPATH because classes are used during the actual compile as they’re created. Then you can jar up the classes, or recompile a new version. You can use -sourcepath and -classpath.

If there’s a file that uses source.java in: src/dir1/dir3/another.java then this path is required too: src/dir1/dir3/ (separate multiple paths with a colon :).

If working with multiple projects or versions, keep paths away from CLASSPATH or the default Java class directory to avoid old/new class collisions, and consider using Ant or Maven which use XML to manage projects.

Links and resources:

http://www.ibm.com/developerworks/library/j-classpath-unix/
Ant: http://ant.apache.org/
Maven: http://maven.apache.org/

Tags:

java, CLASSPATH, compile

Enable HTML5 elements in IE<9 (and Firefox 2?)

Create a dummy element with Javascript and then just use it in your code - even IE6 will recognise it:

<!--[if lt IE 9]>  
<script>  
 document.createElement("header");  
 document.createElement("footer");  
 document.createElement("nav");  
 document.createElement("article");  
 document.createElement("section");  
</script>  
<![endif]-->

Or use the HTML5 shiv in the head element:

<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

Links and resources:

diveintohtml5.org/semantics.html#unknown-elements,
articles.sitepoint.com/article/html-5-snapshot-2009/2,
How to get html5 working in ie and firefox 2

Tags:

HTML5, Javascript, IE, Internet Explorer

How to declare an HTML character set

Always specify a character encoding on every HTML document, either with the HTTP Content-Type header, the <meta http-equiv> declaration, or the shorter <meta charset> declaration.

Links and resources:

diveintohtml5.org/semantics.html#encoding,
code.google.com/p/doctype/wiki/ArticleUtf7,
joelonsoftware.com/articles/Unicode

Tags:

HTML, charset, UTF-8, character encoding

Find out is Apache is running as prefork or worker

enter:

apache2 -l

modules will be listed, and among them (if prefork):

prefork.c

or enter:

apache2 -V

which will show (e.g.):

Server version: Apache/2.2.9 (Debian)
[...]
Architecture:   32-bit
Server MPM:     Prefork
  threaded:     no
    forked:     yes (variable process count)

Also see my local document: apache-mod_wsgi-php-mod_python.txt.

Links and resources:

Apache multi-threaded vs multi-process pre-forked

Tags:

apache, apache2, httpd, prefork, worker, multi-threaded

Add or remove a source to Ruby gems

gem sources -a http://gem_source_url

To remove a source:

gem sources -r http://gem_source_url

see current sources:

gem env

Note: gems.rubyforge.org, gems.github.com, and gemcutter.org have all been replaced by rubygems.org

Links and resources:

How to add source to ruby gems,
Good Ruby gem sources

Tags:

Ruby gems, gem, sources

Use grep backrefs (in TextMate, etc.)

use numbered dollar signs $n e.g.

<h3>200(\d)
<h3 id="200$1">200$1

Example: regex for width or height attributes with no quotes:

(width|height)=([\d]+) 
$1="$2"

Links and resources:

Tags:

regex, textmate, grep, backrefs

Set up a new domain/subdomain on a Debian Linux server under Apache2

  1. General (domain):
    setup the domain in Fasthosts/Rimu DNS control panel BEFORE setting Rimu name servers at the domain name registrar:
    https://rimuhosting.com/dns/zone.jsp?zone=your_domain_here
    Dallas 72.249.38.189
    London 94.76.200.197
    Rimuhosting (ecoconsulting.co.uk): https://rimuhosting.com/dns/records.jsp
    Fasthosts (subdomain): add new subdomain ‘A’ record in ‘Advanced DNS’ and point to the server IP address.

  2. adduser [newusername] (follow prompts to add password etc.)
    Add sub-directories: html, logs and cgi-bin as specified in the VirtualHost block (or Apache will choke).
    password_here - domain_url_here

  3. Create/copy a .conf a file in: /etc/apache2/sites-available/

  4. Enable the site:
    a2ensite sitename.conf for a list of available sites, just hit return after a2ensite.

  5. Restart apache
    apache2 -k graceful (Ubuntu) or (Debian)
    /etc/init.d/apache2 restart (reload will not do it)

  6. add an index.html file under the html directory - this should appear but might need an hour or so for the DNS to propogate.

  7. chown and chgrp * everything (except the .. file, which will stay root anyway)

Links and resources:

Tags:

apache, a2ensite, dns, adduser, domain, subdomain

enable an apache module

a2enmod include (e.g. turn on SSI) then:
/etc/init.d/apache2 restart

Links and resources:

Tags:

apache module, a2enmod

Unix/Linux warns of stopped jobs on logging out

jobs
or:
ps aux (list jobs)
ps (list jobs with id)
w (see what’s going on)
%1 (1 = job number)

Or interrupt the foreground job before it completes:
C-c

Bring background job to foreground:
fg

Stop job e.g. 1 with
kill %1

Links and resources:

Tags:

unix, linux, ps, jobs, kill

Perl ‘Premature end of script headers’

One often-overlooked cause is the wrong Unix/Linux owner and group for the script file throwing the error.

Links and resources:

Common Web dev error messages and what they mean

Tags:

perl, cgi, script error, premature end of script

Get basic WSGI working with Apache for testing

Get WSGI working with Apache to test things before doing any framework-specific setup (like Django)

General Apache & mod_wsgi instructions:

Do this NOT in the DocumentRoot directory, but in a place (in the user’s directory) where Apache can be made to have read access to the wsgi file and the containing directory.

  1. create a (test) WSGI application script file e.g. ‘test.wsgi’ and put this in it:

    def application(environ, startresponse): #function name: ‘application’ status = ‘200 OK’ output = ‘Hello WSGI!’ responseheaders = [(‘Content-type’, ‘text/plain’), (‘Content-Length’, str(len(output)))] startresponse(status, responseheaders) return [output]`

  2. (a) put your wsgi script in a directory other than your site’s root directory, add the WSGIScriptAlias line to either your apache config file (httpd.conf) or your site’s VirtualHost container (e.g. Debian/Ubuntu: /etc/apache2/sites-enabled/) and allow Apache to access it e.g. for mysite.com/test:

    ServerName blog.mysite.org ServerAdmin deveritt@innotts.co.uk

    DocumentRoot /home/deveritt/html Order allow,deny Allow from all

    WSGIScriptAlias /test /home/deveritt/test/test.wsgi Order allow,deny Allow from all

  3. (b) Or, to serve from the DocumentRoot directory for mysite.com or blog.mysite.com you need to make aliases for Apache (not the WSGI app) to serve static media:
    mysite.com/test:

    ServerName blog.mysite.org ServerAdmin deveritt@innotts.co.uk

    DocumentRoot /home/deveritt/html

    Alias /robots.txt /home/deveritt/html/robots.txt Alias /favicon.ico /home/deveritt/html/favicon.ico

    Alias /media/ /home/deveritt/html/media/

    Order allow,deny Allow from all

    WSGIScriptAlias / /home/deveritt/wsgi-scripts/test.wsgi

    Order allow,deny Allow from all

  4. Or (preferred) start as a daemon process (so you can reload your script with e.g. ‘touch myscript.wsgi’)

    TO FOLLOW

  5. restart Apache and visit: mysite.com/test or (for the DocumentRoot setup) mysite.com

For django see Graham Dumpleton’s guide (on ecoconsulting.co.uk?) ‘grahamd-django-modwsgi.txt’

Links and resources:

modwsgi/wiki/QuickConfigurationGuide,
my post on modwsgi

Tags:

wsgi, apache, mod_wsgi

Quickly create a file and add content from the command-line

solution:

touch filename.txt
echo "stuff to add" >> filename.txt

adds the line with a newline at end.

or create and add multiline text:

cat > filename.txt <<-"END"
stuff to add
another line and a final return
END

then add more with:

cat >> filename.txt <<-"END"
...

(then same as above)

or simpler:

cat > filename.txt
line 1
line 2
another line

when finished, hit return and ctrl-D.

then add more with:

cat >> filename.txt
...

(then same as above)

To prepend text, use sed and mv:

sed '1i\
type new text here then close single quote
' FILENAME_HERE > TEMPFILENAME_HERE

then:

mv TEMPFILENAME_HERE FILENAME_HERE

Links and resources:

sed insert text top file

Tags:

touch, append, prepend, sed, cat, files, file

use sed to replace text in multiple files

on OS X

sed -i '' 's/oldtext/newtext/' *.html

e.g. to replace old XHTML br tags with non self-closing ones in all files with an .html extension:

sed -i '' 's/<br\/>/<br>/' *.html

Note: escape forward slashes within your text strings: \/

Empty '' not needed on Centos (and possibly other Linux?)

or for recursive within dirs (omitting ” for non OS X):

find ./ -name "*.html" | xargs sed -i 's/<br \/>/<br>/' *html

Links and resources:

search and replace in all files within a directory recursively,
sed.sourceforge.net Common one-line sed scripts: : sed.sourceforge.net/sedfaq3 : quick note about seds edit in place option : grymoire.com/Unix/Sed : zytrax.com/tech/web/regex.htm#sed : search and replace recursively using sed and grep

Tags:

sed, search, replace, files, file

Find all files modified after a certain number of days

find . -mtime -60 -ls

where ‘60’ is the number of days.

Links and resources:

Tags:

find, files, modified, date, mtime

Test Apache and get stats readout

Use the Apache HTTP server benchmarking tool ab:

ab -c 10 -n 20 http://mydomain.org/

gives requests per second, etc., and:

/usr/sbin/httpd -V

gives version and compilation flags.

Links and resources:

man ab

Tags:

apache, http, benchmark, server

The code snippets below are provided as examples, with CSS and Javascript comments.

The menu is an unordered list with a .menu class attribute, inside the usual “wrapper” div:

<div id="wrapper">
  <ul class="#menu">
    <li><a href="#">link one</a></li>
    <li><a href="#">link two</a></li>
    <li><a href="#">link three</a></li>
  </ul>
</div>

In this example there is a global link style of pale grey with a dark grey hover:

a:link,
a:visited { color: #999; }
a:hover { color: #666; }

If JQuery is used to fade in the hover state colour at this stage, the first hover over a link will show the sudden css hover - the fade will only appear on the second hover.

So, to preserve this css hover rule without javascript, one solution is to create a css class that matches the css :hover colour attribute to the :link state, allowing the JQuery color fade to show first time. This is only appended to the ‘wrapper’ div by JQuery if javascript is available:

/* if javascript is available the .js class is only added with JQuery, so
   css hover and link colours match and the JQuery fade shows instead */
.js .menu a:hover {
  color: #999;
}

Add the JQuery function just before the closing /body tag

$(function() {
  /* add .js class that matches css link and hover states */
  $("#wrapper").addClass("js");

  /* fade in the hover state colour (and fade it out again) */
  $("#menu li a").mouseenter(function() {
    $(this).animate({ color: "#333" }, 600);
  });
  $("#menu li a").mouseleave(function() {
    $(this).animate({ color: "#999" }, 400);
  });
});

Now, hovering over the links won’t show an initial sudden css colour change (to #666) instead of a fade. Adding the .js class to the wrapper div means that this effect can also be applied to other links inside it - just add their selectors to the $("#menu li a") part like this: $("#menu li a, #submenu li a").

‘View source’ will not show the JQuery-generated class="js" addition to the ‘wrapper’ id - you will need to inspect the generated source code (right-click > ‘inspect element’ in Safari [other browsers?]).

Links and resources:

JQuery API .addClass() page (my post didn’t appear),
JQuery mouseenter function

Tags:

jquery, jqueryui, mouseenter, addclass, hover, css, color, colour

Return a value from a Perl subroutine: a 2-liner

Example:

#!/usr/bin/perl

if (spamcheck()) { print "Spam!" } else { print "In the clear!" }
sub spamcheck { return 1 if (2+2 != 4) }

Links and resources:

Tags:

Perl, subroutine, return

Show Perl CGI errors in the browser

use CGI::Carp qw/fatalsToBrowser/;

Links and resources:

jdporter.perlmonk.org/cgicourse/lesson6

Tags:

Perl, carp, errors, cgi, browser

CPAN basics and maintenance

From within the CPAN shell

save config: o conf commit

update:

install CPAN
reload CPAN

help: h
quit: q

Links and resources:

Tags:

Perl, CPAN

wget isn’t installed by default on OS X

solution: use curl instead, or install wget. curl -O recreates remote dirs locally.

Links and resources:

statusq.org/archives/2005/02/22/610/,
OS X using curl instead of wget

Tags:

unix, curl, wget, os x

Checking disc space (Linux/Unix) and memory use

disc space:

df -h

memory use (nice summary):

free -m

see memory use and swaps at 30-second intervals, and show inactive and active

memory (-a)
vmstat 30 -a

list users:

cat /etc/passwd

Links and resources:

Check free space,
princeton.edu/~unix/Solaris/troubleshoot/ram,
man vmstat

Tags:

memory, ram, swap, disc, disk, unix, linux

Managing Linux/Unix users (e.g. remove a user)

userdel -r USERNAME

Link also handy for user admin

Links and resources:

comptechdoc.org/os/linux/usersguide/linux_ugusers,
ahinc.com/linux101/users

Tags:

linux, unix, user, users, remove

Managing Java versions on OS X

java -version

Leopard 10.5 upgrades to Java 1.6 Check:
Utilities > Java > Java Preferences
drag 1.6 to the top and check the box, but also leave 1.5 checked.
If 1.6 isn’t there, install ‘Java for Mac OS X 10.5 Update 7’

Remove (e.g.) Java 1.6

% rm ~/Library/Caches/Java/deployment.properties
% sudo rm -rf /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0
% sudo rm /System/Library/Frameworks/JavaVM.framework/Versions/1.6

That’s it - you’ll have 1.5 as the default again. If you have a folder named ‘Java SE 6’ in /Applications/Utilities/Java, delete that too.

Scott Kovatch, Apple Inc., Java Runtime Classes

Links and resources:

OS X Java versions: : developer.apple.com/library/mac/#technotes/tn2002/tn2110
: lists.apple.com/archives/java-dev/2007/Oct/msg00637

Java for Mac OS X 10.5 Update 7: : support.apple.com/kb/HT4140 : support.apple.com/kb/DL971

Important Java Directories in OS X

Tags:

OS X, Java, Java 1.6

Get MySQL configuration data (port, etc.)

Open the MySQL shell: $mysql

See MySQL configuration data: mysql> show variables;

View general MySQL status: mysql> status;

Links and resources:

comentum.com/mysql-administration

Tags:

mysql, port, configuration

Simple Python list comprehension examples

>>> timesten=[1,2,3,4,5,6,7,8,9]
>>> print [v*10 for v in timesten if v>4]
[50, 60, 70, 80, 90]
>>> multi = dict([(v,v*10) for v in timesten])
>>> print multi
{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70, 8: 80, 9: 90}

Links and resources:

Programming Collective Intelligence Building Applications (amazon.co.uk preview)

Tags:

python, list comprehension, example

Why document.write isn’t the best method

There are better ways to incorporate strings into HTML content, such as .innerHTML and .createElement or HTML cloning patterns - document.write should be avoided because:

  1. it is recklessly dependent on timing, so if called before the onload event, it appends or inserts text into the page. If it is called after onload, it completely replaces the page, destroying what came before.

  2. document.write encourages bad structure, in which script and markup are intermingled. A cleaner structure has minimal interaction between markup and script.

Links and resources:

javascript.crockford.com/script

Tags:

javascript, document.write, html

Javacript HTML script tag blocks: uses, abuses and hints

  1. Don’t add HTML comments <!-- //--> - these haven’t been necessary for years. Also, technically the comment contains a javascript decrement: -- .

  2. Within Javascript, this: </ should always be escaped thus: <\/

  3. Put the src attribute first: script src="url"

  4. type="text/javascript" is optional - the browser knows what to do

  5. Add scripts as late as possible, just before the closing </body> tag is best, but ensure that they load before they’re called on the page.

Links and resources:

javascript.crockford.com/script

Tags:

javascript, html, script tag, type attribute, script src attribute

Included files inside utf-8 parent file not showing non-ascii characters

After setting up a directory block in vhosts e.g.:

<Directory /home/adomain/html>
    Options Includes
    XBitHack on
    AddType text/html .inc
</Directory>

(NOTE: preceding + only needed if also a - setting e.g. -Indexes)

chmod only files to be parsed (i.e. containing SSI directives) to 744.

Make sure included files themselves are saved with utf-8 encoding from the text editor. Otherwise (even if the parent html page is utf-8) they’ll be imported with their file encoding, and won’t even respond to Apache directives like AddDefaultCharset.

Links and resources:

Server side includes and character encoding

Tags:

SSI, server side includes, includes, Apache, utf-8, encoding, charset

Avoid incompatible filename characters and insignificant files on servers

Windows file system incompatible characters

< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

Small operating-system-specific files that can be deleted safely from a server:
desktop.ini
thumbs.db
.DS_Store
icon\r
.~[SOME_FILENAME_HERE]

Links and resources:

dropbox.com/help/145

Tags:

file, filename, incompatible characters, file system, Windows, OS X, Linux, compatibility

Renumber ordered lists using CSS :before instead of deprecated ‘start’ attribute

Various possibilities, including some styles for aligning :before-generated numbers:

The CSS:

/* the basics */

.numlist {
  counter-reset: item;
}
.numlist li {
  list-style-type: none; /*display: block; also removes numbers*/
}
.numlist li:before {
  content: counter(item) ". ";
  counter-increment: item;
}
.numlist2 {
  counter-reset: item 4;
}

/* variations with increment additions and alignment */

.numlist3 li:before,
.numlist4 li:before {
  counter-increment: item 4; /* count list numbers in fours */
}
.numlist3 li:before {
  float: left; /* or display: inline-block; to pad numbers*/
  width: 1.75em; /* width aligns list items after numbers */
}
.numlist4 li:before {
  float: left;
  text-align: right;
  padding-right: 0.5em; /* padding combined with... */
  width: 1.25em; /* ...width aligns list items */
}

The HTML:

<ol class="numlist">
  <li>group one</li>
  <li>group one</li>
  <li>group one</li>
  <li>group one</li>
</ol>

Continues from the above with counter-reset: item 4:

<ol class="numlist numlist2">
  <li>group two</li>
  <li>group two</li>
  <li>group two</li>
  <li>group two</li>
</ol>

Count in fours with counter-increment and align list items with a width value:

<ol class="numlist numlist3">
  <li>group three</li>
  <li>group three</li>
  <li>group three</li>
  <li>group three</li>
</ol>

Align list items and right-align smaller to larger numbers with width, text-align and padding:

<ol class="numlist numlist4">
  <li>group three</li>
  <li>group three</li>
  <li>group three</li>
  <li>group three</li>
</ol>

Links and resources:

Make ol list start from number different than 1 using css

Tags:

html, css, ordered list, numbering, numbers, start attribute, counter-reset

PNG file with alpha transparency and opacity fades to black in Internet Explorer

Don’t use an ie-specific opacity filter in css for elements having a background png image with alpha channel transparency. There is a fix (see link) using an IE filter and rgba, which can be activated using Javascript/JQuery or placed in a CSS file.

Links and resources:

PNG transparency problems in IE8

Tags:

CSS, IE, Internet Explorer, png, alpha, transparency, opacity

Border-radius fails for images in Firefox

wrap the image in a span with the same height and width as the image, apply border-radius to the span and use the image as a background to the span. The xhtml-only SVG clipping path method works, but fails with box-shadow (adding the path the the outer border of the shadow), and isn’t future-proof (i.e. FF 4 supports border-radius for images).

Links and resources:

Firefox-moz-border-radius-wont-crop-out-image,
Firefox img rounded corners SVG,
Adventures in web 3.0 part 2: css3

Tags:

CSS3, border-radius, Firefox, image, img

For Joomla 1.6, find the template:
/component/com_content/views/article/tmpl/default.php

Use $this->item->parent to wrap the h2 tag in a link to the item’s parent page:

<h2>
  <?php
  echo '<a href="' . JRoute::_(ContentHelperRoute::getArticleRoute($this->item->parent)) . '">';
  echo $this->escape($this->params->get('page_heading')) . '</a>';
  ?>
</h2>

Update: in a later version of Joomla 1.6 this gives an empty ‘?id=’ suffix to the URL, so here’s the solution for the actual URL part above:

str_replace("?id=", "", JRoute::_(ContentHelperRoute::getArticleRoute($this->parent)))

This question about mod_rewrite is related.

Links and resources:

forum.joomla.org/viewtopic.php?f=619&t=606029,
Hiding the article id in a joomla menu

Tags:

Joomla, PHP, this->item->parent, heading, h2

Obtain the digital root of any integer

dr = the integer rt = formula for obtaining the digital root of the integer

Ruby:

irb> dr = 298749875
=> 298749875
irb> rt = 1 + (dr - 1)%9
=> 5

Python:

>>> dr = 298749875
>>> rt = 1 + (dr -1 )%9
>>> rt
5

Perl one-liner in terminal:

perl -e '$dr = 298749875; $rt = 1 + ($dr - 1)%9; print "$rt\n"'
5

Links and resources:

mathworld.wolfram.com/DigitalRoot
affy.blogspot.com/p5be/ch27

Tags:

number theory, maths, digital root, Ruby, Python, Perl

When to use Python xrange() over range()

xrange() is more efficient than range() especially for larger iterations: range creates a list, so if you do range(1, 10000000) it creates a list in memory with 10000000 elements, while xrange is a generator, so it evaluates lazily and keeps only one number in memory. When testing this I found the following:

import time

for x in range(1, 10):

    t = time.time()
    [v*10 for v in range(1, 10000)]
    print "range:  %.4f" % ((time.time()-t)*100)

    t = time.time()
    [v*10 for v in xrange(1, 10000)]
    print "xrange: %.4f" % ((time.time()-t)*100)

run it:

$python range_tests.py
range:  0.4273
xrange: 0.3733
range:  0.3881
xrange: 0.3507
range:  0.3712
xrange: 0.3565
range:  0.4031
xrange: 0.3558
range:  0.3714
xrange: 0.3520
range:  0.3834
xrange: 0.3546
range:  0.3717
xrange: 0.3511
range:  0.3745
xrange: 0.3523
range:  0.3858
xrange: 0.3997 <- garbage collection?

Or, using xrange in the for loop:

range:  0.4172
xrange: 0.3701
range:  0.3840
xrange: 0.3547
range:  0.3830
xrange: 0.3862 <- garbage collection?
range:  0.4019
xrange: 0.3532
range:  0.3738
xrange: 0.3726
range:  0.3762
xrange: 0.3533
range:  0.3710
xrange: 0.3509
range:  0.3738
xrange: 0.3512
range:  0.3703
xrange: 0.3509

Links and resources:

‘Python in a Nutshell’ ,
Difference between range and xrange,
docs.python.org/library/timeit,
Python Performance Tips

Tags:

Python, range, xrange, iterator, timeit

A snippet to wrap an editable string around a selection in TextMate

Bundles > Show Bundle Editor
then choose ‘New Snippet’ from the add + button (bottom left) and use:

${1:editme}$TM_SELECTED_TEXT${1/\s.*//}

where editme is the default editable value. If you add a new snippet shortcut in the ‘Key Equivalent’ field (e.g. ctrl-shift-alt-W) you can then wrap any selection in an editable pair of words.

Note: once the shortcut is allocated, you can’t edit a snippet in place - you need to start again (I think - couldn’t find a way around that).

Links and resources:

TextMate editing 2 words at same time

Tags:

TextMate, snippet, wrap

iTunes won’t accept album artwork or wma files

Convert files to m4a (the free version of Switch works)

Links and resources:

nch.com.au/switch/

Tags:

iTunes, artwork, m4a, wma

Install MongoDB on OS X

The ‘quickstart’ on the MongoDB site could be clearer. Here’s a condensed version with more info.

  1. Get the URL (copy the URL, don’t click) for the latest download for your system from: http://www.mongodb.org/downloads

  2. Open Terminal and go to where you keep source files (good to create a directory if you don’t have it already) e.g.: cd ~/src then type curl, the URL you’ve copied, > (between spaces) and the filename you’ll save it as e.g. (for the 64-bit version of Mongo 1.8.1):
    curl http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-1.8.1.tgz > mongo.tgz

  3. Make the default directory for MongoDB to store it’s data - type:
    mkdir -p /data/db
    (-p also creates the intermediate ‘data’ directory)
    If you want see what’s there after you’ve used MongoDB, type:
    cd /data/db;ls -al

  4. Add a couple of aliases to your .bash_profile, .bashrc, .bash_login (or wherever you keep your custom bash stuff) e.g. for the above version of MongoDB downloaded to ~/src:
    alias mongod="/Users/yourusername/src/mongodb-osx-x86_64-1.8.1/bin/mongod"
    alias mongo="/Users/yourusername/src/mongodb-osx-x86_64-1.8.1/bin/mongo"

  5. To use these new aliases, close the Terminal window and open a new one. You can now test MongoDB by starting the MongoDB server with:
    mongod
    then open another Terminal window and type:
    mongo
    In the MongoDB shell that’s now started up, create and retrieve a test document ‘foo’:
    > db.foo.save( { a : 1 } )
    > db.foo.find()

Links and resources:

Mongo OS X Quickstart,
Bashing up MongoDB on Mac OS X 10.6

Tags:

MongoDB, OS X, bash, Terminal, Leopard, Snow Leopard

No root permission to install Perl modules

  1. cd to a directory where you have permissions and download the module e.g.:
    curl -O http://search.cpan.org/CPAN/authors/id/M/MJ/MJEVANS/DBD-ODBC-1.29.tar.gz

  2. Untar (the usual):
    tar xvfz DBD-ODBC-1.29.tar.gz

  3. use makefile with your directory:
    perl Makefile.PL PREFIX=/home/my_user_dir/my_perl_libs

  4. then make as normal. Then use lib "/home/my_user_dir/my_perl_libs" in your code before ‘use’ing ODBC

Links and resources:

Tags:

perl, module, install, permissions

Installing Rack > 1.2.0 fails with Ruby 1.8.6

Downgrade to Rack 1.2.0:
sudo gem uninstall -v 1.2.2 rack

or, if you don’t already have Rack 1.2.0:
sudo gem install -v 1.2.0 rack

The Github fix for Rack (see links) needs: bacon, memcache-client, thin and fcgi, but the latter won’t install with gem, as it needs the fcgi lib, although apparently you can install fcgi-devel instead (e.g. Gentoo):
yum install fcgi-devel

Could install Ruby 1.8.7 but readline issues on Tiger (OS X 10.4) are a pain.

Links and resources:

Rack fix for Ruby 1.8.6,
How to set up Apache with FastCGI and Ruby bindings,
FastCGI gem installation not possible under Windows?,
Rails: can’t install fcgi?,
Compiling Ruby 1.8.7 on a PowerBook G4 with Mac OS X 10.4

Tags:

fcgi, Ruby, gem, fcgi-devel, Rack, install

Microsoft Word keeps reverting to US English/a default font/whatever

solution: Find and edit the basic template Normal.dotm, then save it. All new documents will reflect the changes. In OS X you’ll find it here:
[home]/Library/Application Support/Microsoft/Office/User Templates/Normal.dotm

Links and resources:

Tags:

Word, template, default, Normal.dotm

Set logs to rotate in Apache

Find rotatelogs, then add the following (e.g. depending on your paths) to the VirtualHost block for that domain:

ErrorLog "|/usr/sbin/rotatelogs /home/USER_NAME/logs/error.log.%Y-%m-%d 5M"
CustomLog "|/usr/sbin/rotatelogs /home/USER_NAME/logs/access.log.%Y-%m-%d 5M" combined

Links and resources:

Tags:

apache, logs, log, rotate, rotatelogs

A simple example to distinguish NoSQL from traditional relational databases

Summary with direct quotes from article (see refs):

NoSQL is a bit of a catch-all: there’s a big difference between key/value stores and document databases”

relational gives you solid transactions and joins, NoSQL is fast and scales. …a many-to-many relationship in a relational database might look like:

Posts(id, title, body)
PostTags(post_id, tag_id)
Tags(id, name)

relational databases are tuned to deal with this scenario using joins and foreign keys. In a document database, the typical way to represent this is:

{
    title: 'My Post',
    body: 'This post has a body',
    Tags: ['ruby', 'rails']
}

Notice the denormalization of tags so that there is no longer a table for it so to get a list of all tags using map/reduce:

db.runCommand({
    mapreduce: 'posts',
    map: function() { 
        for (index in this.tags) {
            emit(this.tags[index], 1);
        }
    },
    reduce: function(key, values) { return; },
    out: "tags"
})

This function can be run periodically to create a tags collection from the posts collection. It’s not quite real-time, but … close enough for most uses. (…for real-time, there are other techniques) …the query is more complicated than just selecting out of Tags, but inserting and updating an individual post (the main use case) is simpler.

Add the remaining, and last 3 links, to my NoSQL Google doc: if you don’t know how to use document database specific features such as map/reduce, or how to model your data … to take advantage of them, you won’t be able to adequately evaluate those databases. Similarly, if you don’t know how to use pessimistic locking or referential integrity in a relational database, you will not see how much time and effort it could be saving you over trying to implement such robustness in a NoSQL database that wasn’t designed for it.

“the most common interpretation of “NoSQL” is “non-relational”, although NoSQL is not meant as anti-RDBMS, but emphasizes the advantages of Key-Value Stores, Document Databases, and Graph Databases” “Typical RDBMS implementations are tuned either for small but frequent read/write transactions or for large batch transactions with rare write accesses. NoSQL on the other hand, services heavy read/write workloads” - WIkipedia

Links and resources:

Relational Or NoSQL With Rails? (Xavier Shay: ‘Robot Has No Heart’),
Wikipedia: NoSQL,
NoSQL doesn’t mean non-relational,
Carlo Strozzi’s original NoSQL (1998)

Tags:

database, NoSQL, MySQL, MongoDB, CouchDB, Redis, normalization

Find the version of your bash shell

$ which bash
/bin/bash
$ /bin/bash --version
GNU bash, version 2.05b.0(1)-release (powerpc-apple-darwin8.0)
Copyright (C) 2002 Free Software Foundation, Inc.

(yeah, I know that’s a really old version of bash I was running!)

Links and resources:

Bash: find out version

Tags:

bash, shell, version

Make changes to currently running bash shell take effect

You’ve changed a variable in a bash configuration file (e.g. with: export VARNAME=the-new-value), and you want it to take effect without leaving the shell - enter:
source .bash_login (or .bash_profile, or whatever the name of the changed bash config file is)

Links and resources:

mcsr.olemiss.edu/unixhelp/environment/exmp_bash

Tags:

bash

solution: Go to the dir where you want the link and type:

ln -s source_file link_name

substituting your details for the following values:

Links and resources:

Tags:

linux, unix,symlink, symbolic link

Mongo ‘Unable to create / open lock file’

If you’re running mongo under a user, and there’s an error on running mongod like this:

"Unable to create / open lock file for lockfilepath: /data/db/mongod.lock errno:13 Permission denied, terminating"

change the owner of mongo’s data directory:

sudo chown `id -u` /data/db

Links and resources:

Stack Overflow: Why can’t I start the mongodb

Tags:

mongo, mongodb, permissions

Mongo 32-bit won’t run on OS X 10.4.11 (Tiger)

Download and use this:
downloads.mongodb.org/osx/mongodb-osx-i386-tiger-latest.tgz

“We’ll end up having a separate build for tiger - so the 32-bit regular os x build isn’t expected to work. We should be getting the tiger build up and running for people to test sometime today. Building from source should also work though.” - Michael Dirolf

Links and resources:

groups.google.com/group/mongodb-user/msg/c43ff99cf615eb5b

Tags:

mongo, mongodb, tiger, OS X 10.4

Ruby on Rails fails with “Could not open library ‘libmagic.1.dylib’”

No need to install the whole of ImageMagick (with it’s dependencies), you can install the missing library from:
ftp.astron.com/pub/file/file-5.04.tar.gz

The script from the link below (by Max Howell, author of Homebrew) can be adapted for use without Homebrew.

Links and resources:

libmagic.rb script by Max Howell (Homebrew)

Tags:

imagemagick, ror, ruby on rails, libmagic

Ruby Version Manager (RVM) install problems on OS X 10.4 (Tiger)

There’s a version of the install script for this OS on the Google Groups RVM list.

Links and resources:

Google Groups: OS X 10.4 RVM install script

Tags:

Ruby Version Manager, rvm, tiger, OS X 10.4

Recipe: watercress and goats’ cheese muffins (cow-dairy-free)

Ingredients (serves 10)

Links and resources:

delamaredairy.co.uk

Tags:

recipes, muffins, scones, goat’s cheese, goat’s milk

Find the obscure privacy settings in LinkedIn

Links and resources:

A box you want to uncheck on linkedin,
LinkedIn privacy stuff up

Tags:

privacy, linkedin, data sharing

easy_install numpy fails

easy_install pip
pip install numpy

Or use the Enthought Python package, which incldes NumPy, SciPy, etc.

Links and resources:

enthought.com/products/epd.php

Tags:

python, numpy, pip, scipy

Remove exsiting Python module install from site-packages

Before updating a Python module, you should remove the existing version.

For Debian:
/usr/local/lib/python2.6/site-packages

OS X (e.g. Python 2.6)
/Library/Frameworks/Python.framework/versions/2.6/lib/python2.6/site-packages

remove module folder and egg. If listed in easy-install.pth, also remove relevant line.

Links and resources:

Tags:

Python, site-packages

Apache2.2 command apachectl fails on OS X 10.5 or 10.6

Running apachectl (command) throws error:

/usr/sbin/apachectl: line 82: ulimit: open files: cannot modify limit: Invalid argument

Open /usr/sbin/apachectl and comment out line:

ULIMIT_MAX_FILES="ulimit -S -n `ulimit -S -n"

replace with:

ULIMIT_MAX_FILES="ulimit -S -n"

Links and resources:

Open files cannot modify limit invalid argument

Tags:

apache2, os x, apachectl

Require a specific Ruby gem

Example for oci8

gem 'ruby-oci8', '=1.0.7'
require 'oci8'     # NOTE: file required (oci8.rb) not same
                   # name as gem, as is frequently the case

Links and resources:

Require a specific version of a Ruby gem

Tags:

ruby, gem, require

Get Safari to show a new favicon

Safari tends to hang on to the version of a favicon loaded when a website is first visited. Go to ~/Library/Safari and move the file ‘WebpageIcons.db’. Reload the page, then replace the file. If this doesn’t work, you may need to go to History > Show All History and delete all pages showing the icon you want to replace, and try again.

Links and resources:

Tags:

Safari, favicon

Calculate one number as a percentage of another

Excel: =((smallerNumber/largerNumber)%)*10000
e.g. for 7.84 as a % of 29.75 = 26.35 =((7.84/29.75)%)*10000

Calculator without % key:
20% of 190 = 38 190 * 0.2
7.84 as a % of 29.75 = 26.352 7.84 / 29.75 * 100

Links and resources:

percentagecalculator.net

Tags:

percentage, percent, excel

Find location of a Python module

>>>import [module]
>>>[module].__file__

Links and resources:

Location of Python module sources

Tags:

python, module

Git: remove a file after adding but before committing

git rm --cached [filename]

Links and resources:

Stack Overflow: undo git add before commit

Tags:

git, remove

Convert Markdown to HTML using Kramdown:

E.g. enerate an HTML file ‘mywebfile.html’ from a markdown file ‘mymdfile.md’:

kramdown my_md_file.md | cat > my_web_file.html

Links and resources:

Tags:

markdown, kramdown

Use RedirectMatch for changed file extensions

RedirectMatch takes an argument e.g. for a permanent redirect use 301:

RedirectMatch 301 (.*)\.htm$ http://myserver.com$1.html

Links and resources:

301 redirect

Tags:

Apache, redirectmatch

Use mod_rewrite for a changed domain

R=301 marks the Redirect as permanent (301), L means make this the last rule:

RewriteEngine ON
RewriteRule ^(.*)$ http://mynewdomain.com/$1 [R=301,L]

Links and resources:

http://www.gnc-web-creations.com/301-redirect.htm

Tags:

Apache, mod_rewrite

Add rar and unrar capability to OS X

Firstly, download RAR for OS X from RarSoft, double-click to archive and drag the resulting rar folder to Applications.

Leopard 10.5: go to your Home folder and open Library/Workflows/Applications/Finder.

Snow Leopard 10.6: go to Home/Library/Services.

Create any of these folders if they don’t exist.

Download this Automator script and put it in the Finder folder above (the megaupload.com download is not available).

You can now right- or ctrl-click on a folder or file in the finder and (from the bottom of the resulting menu) choose More > Automator > RAR selected files. It’s slower than creating a zip archive (so if it’s a large file or folder, give it time), but it works.

To just unrar on OS X, use MacPAR, UnrarX or The Unarchiver.

Links and resources:

How to compress RAR without WInRar Alternative mirror of Automator script.

Tags:

rar, archive, compression, OS X

Create a Javascript two-dimensional array

var a = new Array(4);  
for (i = 0; i < 4; i++) {  
  a[i] = new Array(4);  
  for (j = 0; j < 4; j++) {  
    a[i][j] = "[" + i + "," + j + "]";  
  }  
}

This creates an array with the following rows:

[0,0] [0,1] [0,2] [0,3] //Row 0
[1,0] [1,1] [1,2] [1,3] //Row 1
[2,0] [2,1] [2,2] [2,3] //Row 2
[3,0] [3,1] [3,2] [3,3] //Row 3

Links and resources:

Mozilla developer, Javascript: Two-Dimensional Arrays

Tags:

javascript, array, 2d array

Remove a homebrew-installed package and all dependencies

You have to check orphaned dependencies against those Homebrew itself requires:

brew rm thing_to_remove
brew rm $(join <(brew leaves) <(brew deps thing_to_remove))

Example:

$ brew rm ruby
Uninstalling /usr/local/homebrew/Cellar/ruby/1.9.3-p194...
$ brew rm $(join <(brew leaves) <(brew deps ruby))
Uninstalling /usr/local/homebrew/Cellar/gdbm/1.10...
Uninstalling /usr/local/homebrew/Cellar/libyaml/0.1.4...
Uninstalling /usr/local/homebrew/Cellar/pkg-config/0.25...

Links and resources:

GitHub: Add an option to brew rm to uninstall / remove a package, including all it’s dependencies. StackOverflow: Uninstall / remove a Homebrew package including all its dependencies

Tags:

homebrew, brew, os x, uninstall, dependencies

Get series of n grouped values from a Ruby array, like an extended hash

Say you want to loop through groups of three values at a time:

['Home', '/', 'The home page',
'Page Two', 'two', 'The second page',
'Page three', 'three', 'The third page'
].each_slice(3) do|label,link,title|
   li {a label, :href => link, :title => title}
   # print "#{label} | #{link} | #{title}\n"
end

Example from Ruby micro-framework Camping. To use without html markup uncomment the print statement. (note: Ruby < 1.8.7 will need require 'enumerator' for each_slice)

Links and resources:

Stack Overflow, reply to: the right way to iterate through a Ruby array

Tags:

ruby, array, hash

Use a lambda in Ruby to make an array from hash pairs

def with_names(fn)
  result = []
  [ ["Christopher", "Alexander"],
    ["John", "McCarthy"],
    ["Joshua", "Norton"] ].each do |pair|
      result << fn.call(pair[0], pair[1])
  end
  result
end
l = lambda { |a, b| "#{a} #{b}" }
with_names(l)

Links and resources:

RubyMonk: functional programming

Tags:

ruby, lambda, hash

Change default column wrap in TextMate

Open up Terminal and type:

defaults write com.macromates.textmate OakWrapColumns '( 60, 70, 80, 120 )'

Links and resources:

http://manual.macromates.com/en/expert_preferences

Tags:

textmate, wrap, column, defaults

Regex to wrap bracketed numbers in a cite tag

Includes hyphenated series (n-n).

find: \((.[\d-]+)\) replace: <cite>($1)</cite>

Links and resources:

Tags:

grep, regex, textmate

Stop git tracking deleted files

git rm $(git ls-files --deleted)
git add -u

git status will list them to be deleted at the next commit.

Links and resources:

Tags:

git, deleted

Add a file or directory to an existing .gitignore file

echo "path/to_files/" >> .gitignore

Links and resources:

Tags:

git, ignore

A basic Javascript constructor

function Thing(param1, param2) {
    this.property1 = param1;
    this.property2 = param2;
}

var my_thing = new Thing(eg_number, "eg_string");

Or alternative syntax:

var Thing = function(param1, param2) {
    this.property1 = param1;
    this.property2 = param2;
}

Or use object literal sytnax:

var Thing = {
    property1: param1,
    property2: param2
}

Links and resources:

Tags:

javascript, constructor

Setting RVM default Ruby

View your installed version of Ruby with rvm list, and copy the version you want as a default.

rvm alias create default ruby-1.9.2-p290 or in latest RVM docs(?):
rvm --default use 1.9.2-p290 which sets the default Ruby for all new terminal windows (the alias method didn’t do this on OS X 10.5.8 for new windows?).

Links and resources:

Tags:

rvm, ruby, default

Debian apt-get cheat sheet

aptitude is the text-based interface to the Debian GNU/Linux package system. Debian package names end in .deb

apt-get

install {pkg name} or upgrade to latest
remove {pkg name} (but keep configuration files)
--purge remove {pkg name} (remove all files)
update 
upgrade {pkg name} find multiple upgrades
  display the list of all available upgrades (updates) using -u option,
  to upgrade all shown packages, hit 'y'
dist-upgrade (after update) upgrade Debian
 - intelligently handles changing dependencies with new package versions
 - apt-get has a "smart" conflict resolution system

apt-cache

apt-cache search "Text-to-search"
 - search for package/package description before installing (if you don't know the exact name)
apt-cache depends {name} list each dependency of a package

dpkg - install, query uninstall packages

dpkg --info {.deb-package-name} get package information
dpkg -l list installed packages
dpkg -l | grep -i 'pkg name' see if a package is installed
dpkg -L {pkg name} list files provided (or owned) by the installed package
dpkg -S {/path/to/file} find what package owns a file
dpkg -s {package-name} | grep Status find out if Debian package is installed or not

Links and resources:

Linux debian package management cheat sheet, apt-get cheat sheet, dpkg cheat sheet

Tags:

debian, linux, apt-get, cheatsheet

Java: compiling and directory hierarchy

dot-separated classnames need separate directories - in the src directory make a hierarchy that mirrors your package hierarchy e.g. given a class named com.elharo.math.Fraction put Fraction.java inside the math directory:

src com elharo math Fraction.java

Compiling Java code is tricky because you need to keep track of several related but different things:

By default, the javac compiler thinks these are all the current working directory, which is almost never what you want. Consequently, you need to explicitly specify each of these elements when you compile.

If you use the Java CLASSPATH (because you’re only compiling to the same directories every time, say if you only have one Java project), it needs both the source and target directory because the compiler needs to find the .class and .java files each target file imports (check this).

Links and resources:

IBM Developer work: Java classpaths

Tags:

java, classpath

github error: SSL certificate problem, verify that the CA cert is OK

Quick fix (don’t use --global if it’s just for one repo) disable the SSL CERT verfication: git config --global http.sslVerify false

Links and resources:

SSL certificate rejected trying to access GitHub over HTTPS behind firewall

Tags:

github, git, SSL, certificate

Installing Fortran on OS X as a dependency of scipy (or of R)

If pip install scipy or easy_install scipy fail with pip giving timeout: timed out or easy_install failing with:

error: SandboxViolation: open('/dev/null', 'w') {}

The package setup script has attempted to modify files on your system
that are not within the EasyInstall build area, and has been aborted.

This package cannot be safely installed by EasyInstall, and may not
support alternate installation locations even if you run its setup
script by hand.  Please inform the package's author and the EasyInstall
maintainers to find out if a fix or workaround is available.

the issue for me was that scipy requires Fortran, which doesn’t come with OS X and wasn’t installed on my (Leopard) system with Xcode 3.1.4.

A search for the latter error turned up a red herring: issue93 in setuptools; while searching for numpy and scipy on OS X might turn up Calvin Cheng’s instructions, which are more useful, except that the link to the OS X GFortran binaries at gcc.gnu.org appears to be broken (for me, gcc.gnu.org never loaded), but there’s an alternative source from a page on R for OS X, although (unsure of whether 32- or 64-bit was the way to go for my OS X 10.5 system) I used the GNU Fortran installer for ‘OS X 10.4 or higher’ on this ‘tools for R’ page (although the latest GFortran 4.2.4 is also offered for 10.5, and these are all OS X .dmg installers). I also used the OS X scipy installer, and all was well. However, my virtualenv now has to use my system Python libraries, as scipy wouldn’t install inside a virtual env with no site packages. Sigh.

Fortran is also required for the R language.

Links and resources:

Tags:

python, OS X, scipy, GNU Fortran, GFortran, [R], CRAN, virtualenv

How to scale SVGs for responsive web design

Make sure the SVG root element has a viewBox attribute e.g.
<svg version="1.1" viewBox="0 0 300 185">
where first two parameters (0 0) are the origins of the viewBox, the second two are the width and height.

Links and resources:

Autoscaling an SVG embedded in HTML on window resize, Making Sense of SVG viewBox’s Madness

Tags:

SVG, responsive design

Stretch and scale CSS background

Use the CSS 3 property background-size. To preserve the aspect ratio use the values background-size: cover;, contain; or 100% 100%;. Louis-Rémi built a polyfill for IE8.

Links and resources:

Stretch and scale CSS background

Tags:

css, css3, background, background-image, scale

Clear screen while in the python shell

Use the system’s command in *nix:

>>> import os
>>> os.system("clear")

Or in a function:

>>>def cls():
...    os.system("clear")
...
>>>cls

Links and resources:

Any way to clear python’s IDLE window?

Tags:

Python, terminal, shell, IDLE, clear screen

What does init do in Python classes?

This ensures that all subsequent classes inherit the functionality defined in __init__:

>>> class MyClass(object):
...     i = 123
...     def __init__(self):
...         self.i = 345
... 
>>> a = MyClass()
>>> print a.i
345
>>> print MyClass.i
123

Links and resources:

Python __init__ and self what do they do?, Python, __init__ and self confusion

Tags:

Python, __init__, self

Two examples using Ruby’s inject

A function to add any list of numbers:

def add(*numbers)
  numbers.inject(0) { |sum, number| sum + number }
end

puts add(1)
puts add(1, 2)
puts add(1, 2, 3)
puts add(1, 2, 3, 4)

Multiply a list of numbers by two if the number is even:

[1, 2, 3, 4, 5, 6].inject([]) do |result, elm|
  result << elm * 2 if elm % 2 == 0
  result
end
# => [4, 8, 12]

Grouping a list of words by the first letter of the word:

["alpha", "bravo", "charlie", "bark", "almond"].inject({}) do |result, elm|
  result[elm[0].to_sym] ||= []
  result[elm[0].to_sym] << elm
end
# => {:a=>["alpha", "almond"], :b=>["bravo", "bark"], :c=>["charlie"]}

Links and resources:

rubymonk lesson 69, Map If in Ruby and an Introduction to Ruby’s Inject

Tags:

ruby, inject

Using Javascript libraries for enabling media queries in IE<=8

There are two main Javascript libraries with the following features:

Respond.js : simply enables min-width and max-width, focused mainly on Internet Explorer 8 and under. Also, read the caveats closely as there are many edge cases! css3-mediaqueries.js : covers all media queries, and also IE 5+, Firefox 1+ and Safari 2

Links and resources:

How to get IE8 to understand CSS media queries

Tags:

css3, media queries, IE8, internet explorer

Ruby if-then, and array from YAML data in one erb line

If a Yaml array planets exists, create a Ruby array p from it:

Yaml:

planets:
- Sun
- Moon
- Venus

erb:

<% p = []; @item[:planets].each do |i| p << i end %>

Links and resources:

Writing if statement in one line with elsif condition

Tags:

yaml, ruby, erb

Illustrator ‘collect in new layer’ defaults to outline view

If the ‘eye’ icon for the item in the Layers palette shows an ‘outline’ eye, cmd-click it to change to ‘preview’.

Links and resources:

Illustrator: About layers

Tags:

adobe, illustrator, layers, outline view

Create an equilateral triangle in CSS

To calculate the desired height, add the left and right borders (the total width) and multiply by 86.6%:

w * 86.6/100 = h
(50 + 50) * 86.6/100 = 86.6

To calculate a desired width from a given height:

h * 115.470054/100 = w
100 * 115.470054/100 = 115.470054

Example CSS:

#triangle-equi {
  margin: 0 auto;
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 86px solid green; /* 86.6% of width (50+50) */
}

Or use em for measurements to get fractions:

  border-left: 5em solid transparent;
  border-right: 5em solid transparent;
  border-bottom: 8.66em solid red;

Links and resources:

Equilateral Triangle Calculator, The Shapes of CSS

Tags:

css, geometry, triangle, mathematics

Using RSA for ssh, sftp, rsync, etc. without a password

If you’re developing websites and frequently logging into the server via a command line to upload files (e.g. for static site generator nanoc’s nanoc deploy command) you can set things up so you don’t have to enter (or remember) the user’s password every time.

Open a command-line terminal while logged as the user who will run scp, ssh or rsync (i.e. when making/uploading websites).

To find out if you already have an rsa keypair enter cd ~;ls -al - if you see an .ssh folder, enter cd .ssh;ls and check for the keys: id_rsa and id_rsa.pub

If there is no .ssh folder, make sure you’re at the root level, create it and enter it:

cd ~
mkdir .ssh
cd .ssh

Create the rsa keypair

Now you’re inside the .ssh folder, create a private and public keypair by typing:

ssh-keygen -t rsa

When prompted ‘Enter file in which to save the key’, press return or enter to name the pair with the default name ‘id_rsa’.

Do the same for ‘Enter passphrase’, otherwise you will need to enter a passphrase each time you log in, which defeats the password-free purpose.

You will see something like the following:

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/username/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
14:f0:06:ba:8c:d7:d0:b7:33:28:ca:38:fa:30:24:52 username@.local
The key's randomart image is:
+--[ RSA 2048]----+
|         o =o    |
|        o.+ .o   |
|   = o .o  o     |
|  . B .  = o     |
|   o =           |
|    E .o..o .    |
|    .            |
|        ..+o.    |
|                 |
+-----------------+

You normally won’t need the randomart image, so it can be ignored, or you can copy and save it in a text file.

Create a public key folder for a user on your server

You will need to do this for each user on the remote machine where you upload website files via a terminal. If you’re managing multiple websites, your server will likely have a user account for each website. If you only manage one, you only need to do this once.

Make sure you’re still in the .ssh folder and can see the keypair:

cd ~.ssh;ls

If you see ‘id_rsa.pub’ and ‘id_rsa’ both listed, login in to the remote user’s account on the server:

ssh username@servername_or_ip

Enter the usual password, make sure you’re at the user’s root directory, and check for an .ssh directory:

cd ~
ls -al

If there isn’t an ‘.ssh’ directory, create it, set permissions that restrict everyone except that user, and cd (change directory) into it:

mkdir .ssh
chmod 700 .ssh
cd .ssh

Upload the public key and save its contents

Create an ‘authorized_keys’ file to hold your public key data and set the file’s permissions:

touch authorized_keys
chmod 600 authorized_keys

Confirm the exact path from the server’s root folder to the user’s .ssh folder:

pwd

Copy this path (something like /home/username/.ssh) and use it in the following command, which will securely copy (scp) your local public key file to the user’s .ssh folder. Use the same ‘username@servername’ you used to log in via ssh:

scp id_rsa.pub username@servername:/home/username/.ssh/id_rsa.pub

Use ls to check that the key has uploaded—if you see the two files ‘authorized_keys’ and ‘id_rsa.pub’, enter the following command to copy the contents of your public key to the ‘authorized_keys’ file:

cat id_rsa.pub >>authorized_keys

If you enter less authorized_keys you should see a string of text, which means that your public key is now stored in ‘authorized_keys’. Type q to exit less, and delete your public key file:

rm id_rsa.pub

You should now be able to logout of ssh and login again as before, but without being prompted for a password.

Note: if the user’s home directory on the server is accessed via NFS, then permissions for .ssh has to be 711 and ‘authorized_keys’ 644.

Links and resources:

How To scp, ssh and rsync without prompting for password, SSH and SCP: Howto, tips & tricks

Tags: rsa, rsa keys, id_rsa, ssh, sftp, rsync, password, server

Updating to a more recent version of Ruby on OS X with Homebrew

Always start by checking Homebrew is healthy and up-to-date:

brew update
brew doctor

then

brew install ruby

Check where the new Ruby gems are:

gem environment
[...]
  - GEM PATHS:
     - /usr/local/Cellar/ruby/2.0.0-p247/lib/ruby/gems/2.0.0
     - /Users/admin/.gem/ruby/2.0.0
[...]

Like rvm, you can now install gems without sudo for your user (not system-wide):

gem install (gem_name)

Homebrew advises:

NOTE: By default, gem installed binaries will be placed into `/usr/local/opt/ruby/bin`

You may want to add this to your PATH.

so you will probably need to add /usr/local/opt/ruby/bin to the end of the colon-separated PATH variable in .bash_profile (if you already have one—in your home folder—create the file if you don’t already have it) e.g.:
export PATH=/usr/local/bin:(more paths here):/usr/X11/bin:/usr/local/opt/ruby/bin

Installing Ruby gems not working with Home Brew

Tags: ruby, homebrew, ruby gems

Reverse a string in Javascript

mystring.split("").reverse().join("");

Beware that this fails with Unicode string (e.g. accented characters, etc.)—for those, use Mathias Bynens’ Esrever library.

Reversing strings in JavaScript, How do you reverse a string in place in JavaScript?, A Unicode-aware string reverser written in JavaScript

Tags: javascript, reverse, string

Install node.js with Homebrew

Run the commands below in order:

brew update
brew doctor
brew upgrade node

How to install latest version of Node using Brew

Tags: homebrew, brew, node

printf() variable substitution in Javascript consoles

var userName = 'Dave', color = 'purple', age = 100;
console.log("My name is %s and I am age %d. I like the colour %s.", userName, age, color);

The substitution strings (Formatting not yet supported for integers or floats):

Jason Strimpel: printf() in JavaScript???

Tags: javascript, printf, console.log

Export/import multiple images to/from Photoshop layers

Import multiple images as layers with Adobe Bridge

Open Adobe Bridge, navigate to the directory with the images and select all the files you want in layers.

In the menu bar select Tools > Photoshop > Load Files into Photoshop layers.

Export multiple layers as separate images

Files -> Scripts -> Export Layers to Files…

Photoshop: Importing multiple images into multiple layers, How to Export Layers in Photoshop CS5 as Separate Images

Tags: photoshop, adobe bridge, multiple images, layers

Share an iCloud calendar in iCal 4 (OS X 10.6 Snow Leopard)

If you use Snow Leopard and want to access/edit calendars that you have in iCloud (say from more recent OS X versions or iOS devices), older advice is far too complex—the most recent iCal 4 in Snow Leopard makes this possible. Follow these steps:

Getting to your iCloud calendar from iCal 4 (OSX10.6) or a CalDAV client, 10.6: Add iCloud Calendar in iCal (out of date but a good reference)

Tags: caldav, calendar, snow leopard

Loading a CDN resource on a secure site under HTTPS

For example, the recommended CDN for Font Awesome shows the URL without an HTTP-protocol prefix:
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

So if your site is running under https (TLS/SSL), add the appropriate protocol to the start of the URL:
href="https://maxcdn.bootstrapcdn.com/font-awesome/…

​SSL: How to Move Your Website to HTTPS

Tags: https, cdn

Upgrading Node and Ember CLI

This uses the n Node helper package so first, clean the cache and install it:
sudo npm cache clean -f
sudo npm install -g n

Then upgrade node to the latest stable version:
sudo n stable

Or specify a desired version:
sudo n 0.8.21

and - if you like - check the version:
node -v

Finally, uninstall Ember CLI and re-install the latest:

npm uninstall -g ember-cli
npm cache clean
bower cache clean
npm install -g ember-cli

Then upgrade your Ember CLI app.

Upgrade Node.js via NPM Upgrading an Ember CLI App

Tags: ember cli, ember, node, upgrade

Install/downgrade to a specific version of Ember CLI

npm install -g ember-cli@0.2.3

Tags: ember cli, ember, downgrade, version

Clone an Ember CLI project

Clone the repo:
git clone /path_to_project

Install the project’s Node and Bower package dependencies npm install bower install

Tags: ember, bower, git