BitBonsai Labs Mauricio Wolff's geekness

29Jun/100

Testing pastie.org embed

27Apr/100

JS Tip: Revealing Module Pattern

From Christian Heilmann. Nothing too fancy for those who are used to write jQ plugins, but nice to save it for reference...

var myApp = function(){
	var nome = 'Mauricio Wolff';
	var idade = 35;
 
	function insertPessoa(){
		// [...]
	}
 
	function updatePessoa(){
		// [...]
	}
 
	function setPessoa () {
		// [ usa insert ou update ]
	}
 
	function getPessoa () {
		// [...]
	}
 
	return{
		set: setPessoa,
		get: getPessoa
	}
}();
 
// Example usage:
myApp.get();
29Jan/100

Python x PHP: Finding IPs with regex

Sometimes I just like to write code in Python and PHP to compare them, and admire both...

Here's a little code to find IP adressess inside strings (returned from a nslookup). The code is not optimal, but illustrate how both languages works. Just for some friday fun...

Python

 
Python 2.6.1 (r261:67515, Jul  7 2009,  23:51:51)
 [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
 Type "help", "copyright", "credits" or "license"  for  more information.
 
>>> a  = 'yahoo.com IN A 69.147.114.224 18774s  (05:12:54)'
>>> a += 'yahoo.com IN A 209.131.36.159 18774s  (05:12:54)'
>>> a += 'yahoo.com IN A 209.191.93.53  18774s  (05:12:54)'
>>> print a
 'yahoo.com IN A 69.147.114.224  18774s (05:12:54)yahoo.com IN A 209.131.36.159 18774s  (05:12:54)yahoo.com IN A 209.191.93.53  18774s (05:12:54)'
>>>; import re
>>> p = re.compile('(?:\d{1,3}\.){3}\d{1,3}')
>>>; p.findall(a)
 ['69.147.114.224', '209.131.36.159',  '209.191.93.53']
>>>

PHP

$a  = 'yahoo.com IN A 69.147.114.224 18774s (05:12:54)';
$a .= 'yahoo.com  IN A 209.131.36.159 18774s (05:12:54)';
$a .= 'yahoo.com  IN A 209.191.93.53  18774s (05:12:54)';
 
print $a;
 
/*
saida: yahoo.com IN A 69.147.114.224 18774s (05:12:54)yahoo.com IN A  209.131.36.159 18774s (05:12:54)yahoo.com IN A 209.191.93.53 18774s  (05:12:54)
*/
 
$re = '/(\d+).(\d+).(\d+).(\d+)/';
 
preg_match_all($re, $a, $matches);
 
var_dump($matches[0]); // saida 1
var_dump($matches); // saida 2
 
/*
saida 1:
 
array(3) {
  [0]=>
  string(14) "69.147.114.224"
  [1]=>
  string(14) "209.131.36.159"
  [2]=>
  string(13) "209.191.93.53"
}
 
saida 2:
 
array(5) {
  [0]=>
  array(3) {
    [0]=>
    string(14) "69.147.114.224"
    [1]=>
    string(14) "209.131.36.159"
    [2]=>
    string(13) "209.191.93.53"
  }
  [1]=>
  array(3) {
    [0]=>
    string(2) "69"
    [1]=>
    string(3) "209"
    [2]=>
    string(3) "209"
  }
  [2]=>
  array(3) {
    [0]=>
    string(3) "147"
    [1]=>
    string(3) "131"
    [2]=>
    string(3) "191"
  }
  [3]=>
  array(3) {
    [0]=>
    string(3) "114"
    [1]=>
    string(2) "36"
    [2]=>
    string(2) "93"
  }
  [4]=>
  array(3) {
    [0]=>
    string(3) "224"
    [1]=>
    string(3) "159"
    [2]=>
    string(2) "53"
  }
}
*/
16Jan/100

Porque adoro jQuery

Pedido: Fazer com que todos os links de uma determinada página sejam externos.

  • Abordagem 1: Alterar a classe em php, genérica, para ver se o link é externo e acrescentar o target; por regex.
  • Abordagem 2: Colocar no init.js
$("a[href^='http://']").attr("target","_blank");

E todo site fica assim; links externos, para fora!
Aff... que doce.

14Dec/091

recursively chmod only subdirectories

When I use Betterzip to uncompress a directory tree, it chmods the dirs with 700. Here's the command to solve this:

find . -type d | xargs chmod 755

It finds all subdirectories and chmod'em to 755. Clever, isn't it?

Tagged as: , , 1 Comment
7Dec/090

Deletar .svn recursivamente

Desde que comecei a usar GIT acho svn clumsy... prefiro jogar os arquivos no dropbox, que ele guarda versões pra mim.

Agora resolvi detonar todos os .svn de um trabalho que estou fazendo. Chega de xalassa...

  1. criar um bash script básico para remover os .svn recursivos
  2. salvar no meu ~/bin e dar chmod +x
  3. rodar e ser feliz.
#!/bin/sh
echo "recursively removing .svn folders from"
pwd
rm -rf `find . -type d -name .svn`
#!/bin/sh
 
echo "recursively removing .svn folders from"
 
pwd
 
rm -rf `find . -type d -name .svn`
Filed under: geek, snippet No Comments
6Dec/090

Lifesaver snippets

Alguns códigos são simplesmente salvadores da pátria...

Por exemplo: Tens de subir um site por 3g, e mesmo targzipando o site ele fica com 12Mb por causa dos .svn escondidos...

tar --exclude='.svn' -czvf file.tar.gz ./*

6MB. Simples e eficaz...