Google API Python Client: working with Google Fusion Tables

Finally I’ve managed to work google-api-python-client. It has really strange interface.

So, the working code:

import httplib2
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

# Here's the file you get from API Console -> Service Account.
f = file('key.p12', 'rb')
key = f.read()
f.close()

# Create an httplib2.Http object to handle our HTTP requests and authorize it
  # with the Credentials. Note that the first parameter, service_account_name,
  # is the Email address created for the Service account. It must be the email
  # address associated with the key that was created.
credentials = SignedJwtAssertionCredentials(
    '...@developer.gserviceaccount.com',
    key,
    scope='https://www.googleapis.com/auth/fusiontables')
http = httplib2.Http()
http = credentials.authorize(http)

service = build("fusiontables", "v1", http=http)
# For example, let make SQL query to SELECT ALL from Table with
# id = 1gvB3SedL89vG5r1128nUN5ICyyw7Wio5g1w1mbk
print(service.query().sql(sql='SELECT * FROM 1gvB3SedL89vG5r1128nUN5ICyyw7Wio5g1w1mbk').execute())

How to fix paper jam in Samsung ml-1630 printer

So, I’ve faced very annoying problem: printer (samsung ml-1630) indicated that there is paper jam problem,  but it’s not true in fact.
To solve this I’ve done a lot of stuff (all instructions from manual, inner cleaning, etc.), but nothing helped.

At the end I’ve finally solved the problem via opening the paper tray and clicking several times on white clickers at the end of tray on both side.

UP.: it also might help with paper jam (advice from comments)

Clean the the rubber that grip the paper out of the tray with some medical alcohol (70° is enough). They these rubbers apparently tend to catch some paper fibers or grease that could also make the printer think that it’s jammed.

UP. №2: also might help to blow using air-duster back side of printer. Before blowing take off back panel.

MacOS Lion + Matlab: “Tried to setup shared memory more then once” problem

I’ve tried Matlab 2011b on my macbook air with MacOS X Lion and faced the problem of not starting matlab after installation. Matlab’s icon just blinked once and that’s it.

I’ve found log in console:

com.apple.launchd.peruser ([0x0-0x344344].com.mathworks.matlab[3834] Tried to setup shared memory more then once

peruser part made me to think about some problem with permissions. Since I’ve installed macports I cannot create folder in my home folder without “sudo” command. I’ve decided to fix this problem once for all and I hoped that it might help with MatLab problem too.

So I’ve:

  1. chosen my home folder,
  2. clicked “?(cmd)+i” (folder info) and found out that there is no me (user) having any permissions to my home folder, but there was “macports” user with all permissions (it’s very strange).
  3. I’ve added “administrators” group via clicking on + at the buttom and gave it “read and write” permissions (I cannot add mine user for some reason I do not know yet)
  4. And voila! Now I’am able to create anything in my home folder and Matlab have started. 🙂

WD My Book World: clean (empty) Time Machine storage on disk

I had problem with connection to WD MyBookWorld. I solved the problem by deleting via ssh folder /DataVolume/.timemachine and created the new one with 777 permission and reselected volume to backup in TimeMachine.

rm -rf /DataVolume/.timemachine
mkdir /DataVolume/.timemachine
chmod 777 /DataVolume/.timemachine

Script takes some time to complete, because time machine creates a lot of tiny files.

Erlang & CouchDB: how to bulk delete docs using couchbeam

I spent about an hour on this, so I post the solution:

	% get site's stat docs
	{ok, ViewResults} = couchbeam_view:fetch(Db_pid,
									  {"app", "get-stat-by-site-url"},
								 	  [{key, list_to_binary(Site)}]),
	Stats = lists:map(fun({Doc})->
                        {Value} = proplists:get_value(<<"value">>, Doc),
						{[{<<"_id">>, proplists:get_value(<<"_id">>, Value)},
                          {<<"_rev">>, proplists:get_value(<<"_rev">>, Value)}
						 ]}
                 end, ViewResults),
	% delete stats
	couchbeam:delete_docs(Db_pid, Stats),

Mac Os X – host and Ubuntu – guest in VirtualBox. Shared folder problem.

I spent more then hour to solve this so I’ll post the solution.

Problem: on mounting shared folder named “common” in Ubuntu as guest OS over Mac Os X as host OS

 sudo mount -t vboxsf common /media/common

I got the error:

/sbin/mount.vboxsf: mounting failed with the error: Invalid argument

Solution:
quote folders’ names!

 sudo mount -t vboxsf "common" "/media/common"

Zend Framework: group buttons in single line in Zend_Form

Aim: group buttons generated by Zend_Form in single line (“Update” and “Delete”).

Solution: use decorators and DisplayGroup.

		$this->addElement (new Zend_Form_Element_Submit('submit', array(
            	'label'	=> 'Update',
        		'class'	=>	'ui-state-default ui-corner-all ui-button',
				'decorators'	=>	array('ViewHelper')
		  	))
		);

        $this->addElement (new Zend_Form_Element_Button('delete', array(
            	'label'	=> 'Delete',
        		'id'	=>	'delete-button',
        		'class'	=>	'ui-state-default ui-corner-all ui-button',
        		'decorators'	=>	array('ViewHelper')
        	))
        );

       $this->addDisplayGroup(array('submit', 'delete'), 'submitButtons', array(
	        'decorators' => array(
	            'FormElements',
	            array('HtmlTag', array('tag' => 'div', 'class' => 'form-buttons')),
	        ),
	   ));

Here $this — children of class Zend_Form (class Application_Model_Form_User extends Zend_Form).
You should get something like this:

Example of interface with buttons grouped in one line

Example of interface with buttons grouped in one line

How does it work?

For every element we’ve removed all decorators except one, which shows element by itself. These elements without decorators we’ve added to one visual group based on div. To support styling in CSS we’ve added class “form-buttons” to this group.

HTML5 & Math: the Sierpinski triangle on canvas

Walking (or I may say, fighting through) D. Knut’s book  “Art of programming” I’ve met Pascal’s triangle, besides I also wanted to make something with html5 canvas some time ago. So there was overwhelming desire to illustrate Pascal’s triangle on canvas.

I’ve found quite interesting way of illustrating numeric sequence in this triangle – Sierpinski triangle and so I’ve implemented it. In a nutshell, even numbers are colored in this triangle.

Implementation code:

	<canvas id="pascalCanvas" width="1500" height="1500"></canvas>

	<script>

		function drawPixel(x, y, context) {
			context.fillRect(x, y, 1, 1);
		}

		canvas = document.getElementById("pascalCanvas");
		c = canvas.getContext("2d");

		//some gradient style
		var gradient = c.createLinearGradient(0, 0, 0, canvas.height);
		gradient.addColorStop(0, "#abc");
		gradient.addColorStop(1, "#000");
		c.fillStyle = gradient;
		//c.fillStyle = "#000";

		//Pascal's triangle
		var tr = new Array(canvas.height);
		for (i = 0; i < canvas.height; i++) {
			tr[i] = new Array(canvas.width);
			for (k = 0; k < canvas.width; k++) {
				if (k == 0)
					tr[i][k] = 1;
				else
					tr[i][k] = 0;
			}
		}

		for (i = 1; i < canvas.height; i++) {
			for (k = 1; k < canvas.width; k++) {
				tr[i][k] = (tr[i-1][k-1] + tr[i-1][k]) % 2;
			}
		}

		//draw
		for (i = 0; i < canvas.height; i++) {
			for (k = 0; k < canvas.width; k++) {
				if (tr[i][k] != 0)
				drawPixel(k, i , c);
			}
		}
	</script>

We get this:

Sierpinski triangle on html5 canvas

Sierpinski triangle on html5 canvas

Live demo

PS: Some fun stuff on St. Valentine day:

Sierpinsli valentine on http://xkcd.ru/543/