h1

Get old scanners working on Mac OSX (Lion)

March 3, 2012

My old scanner (Canon N67OU) stopped working on the update to Lion. After a bit of web searching I found the following brilliant project which magically works with old scanners from the command line:

http://www.ellert.se/twain-sane/

Install the lib-usb package first followed by the SANE backends then TWAIN SANE Interface.

I found I could then scan from the command line with the following:

scanimage –format=tiff -x 215 -y 297 –depth 8 –resolution 100 > scan.tiff

For more help try,
scanimage –help

Or the FAQ online here:
http://www.ellert.se/twain-sane/faq.html

Advertisements
h1

Manage multiple Linux users editing the same projects

March 1, 2012

I work on projects on remote servers where a number of developers might login and edit a file. This usually results in the file permissions changing so that they own it which can cause problems. A simple solution is for everyone to be a member of the same group, then make it so that by default it is that group that is set on a file when someone creates it. We also then want to set the group write flag by default as well so that anyone can edit that file.

The steps involved in setting this up on a server are as follows.

— create a new group everyone will share (mygroup)
groupadd mygroup

— create any new users (if required, this can be skipped if you have existing users)
useradd -G mygroup -m myusername
passwd myusername

— Make the default group for a user to be mygroup so everyone in that group can access their files
usermod -g mygroup myusername

— Set the global umask in the global bashrc file so that all users set group write on files (needs everyone to logout and in again)
vim /etc/bashrc
umask 002

Note for the bashrc thing to work it has to be called from the user’s local .bashrc file intheir home directory. Most linux distros do this by default but it is possible for individuals to remove the include for the global bashrc which will then mean that group write is not set on files they create. The bit in the users .bashrc file that includes the global bashrc looks like this…

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

Now if your shared work is in /var/www/html for example, you’ll need to change the group owner on all files in that directory to the new mygroup

chgrp -R mygroup /var/www/html

And you will also want to make sure every file is writable by the group

chmod -R g+w /var/www/html

h1

MySQL Create new user and grant commands

January 19, 2012

Make a new database (schema) …


mysql> create schema myschema;
Query OK, 0 rows affected (0.00 sec)

Add a new user …


mysql> create user 'fred'@'localhost' identified by 'password-secret-string';
Query OK, 0 rows affected (0.00 sec)

Let the user have access to the new schema …


mysql> grant all privileges on myschema.* to 'fred'@'localhost';
Query OK, 0 rows affected (0.01 sec)

h1

Partition Wizard

January 12, 2012

Just used Partition Wizard 7 to resize Windows XP partitions from within Windows XP. Home edition is free and worked really well:
http://partitionwizard.com/

h1

Calculating permutations of a list

January 11, 2012

A recent project found me with the need to calculate all the possible permutations of a list of values. This is the possible order of all the values in the list.

For example, the list

1, 2, 3

Has 6 possible permutations:

1, 2, 3
1, 3, 2
2, 1, 3
2, 3, 1
3, 1, 2
3, 2, 1

My first attempt tried to calculate all the permutations in one go using a recursive function. In PHP, this looks like this:

/**
* Recursive permutations function for calculating the
* possible permutations of an array.
*
* @param array $list
* The list to calculate the pemutations of.
*
* @return array
* An array of arrays. Each sub array is a permutation
* of the originial array.
*/
function permutations(array $list) {

if (count($list) == 1) {
return array($list);
}

$perms = array();

foreach ($list as $key => $value) {
$sublist = $list;
unset($sublist[$key]);
foreach (permutations($sublist) as $subperm) {
array_unshift($subperm, $value);
$perms[] = $subperm;
}
}

return $perms;
}

This worked great for small lists of up to about 8 elements but past 8 the number of permutations grows great and you start to run out of memory in the recursive calculations. Incidentally, the total number of permutations is always given as the factorial of the number of elements. So, for 3 elements the total number is 3! or 3 x 2 x 1 = 6.

Here is a short piece of code for calculating the factorial of a positive integer.

/**
* Factorial function. For calculating some of the numbers
* of possible combinations.
*/
function nfact($n) {
if ($n == 0) {
return 1;
}
else {
return $n * nfact($n - 1);
}
}

So, my second attempt was to come up with a relationship between the permutation number and the order of the elements in a list given the starting order. I came up with this (in PHP again)

/**
* Given a list in it's initial state workout the
* i'th permutation.
*
* @param array $initial_state
* The initial state of the array.
* @param int $i
* The permutation of the initial state to find. Initial state is 0.
*
* @return array
* The i'th permutation of the initial state.
*/
function permutation(array $initial_state, $i) {

$n = count($initial_state);

// Requires all keys to be numeric in ascending order.
$initial_state = array_values($initial_state);

if ($n == 1) {
return $initial_state;
}

$t = nfact($n);

$first_value_position = floor($i / ($t / $n));
$first_value = $initial_state[$first_value_position];

// Setup the args of for the next value in the permutation.
unset($initial_state[$first_value_position]);
$i = $i % ($t / $n);

return array_merge(array($first_value), permutation($initial_state, $i));
}

To get all permutations you can then loop $i from 0 to the total number of permutations for the list.

$initial_state = array(1, 2, 3, 4, 5, 6, 7, 8);
$permutations = array();

for ($i = 0; $i < nfact(count($initial_state)); $i++) {
$permutations[] = permutation($initial_state, $i);
}

h1

2011 in review

January 4, 2012

The WordPress.com stats helper monkeys prepared a 2011 annual report for this blog.

Here’s an excerpt:

A San Francisco cable car holds 60 people. This blog was viewed about 3,300 times in 2011. If it were a cable car, it would take about 55 trips to carry that many people.

Click here to see the complete report.

h1

replace syntax in mysql

November 3, 2011

The mysql REPLACE syntax allows you to do string replacements within the content of mysql table text fields. One application I used recently was to rewrite links in blocks of text entered into the Druapl CMS.

UPDATE table SET col = REPLACE(col, '/some/path', '/some/other/path');

This will replace all instances of ‘/some/path’ with ‘/some/other/path’ within the col column of the table table.