unarchive files

Dec 14
2009

a sample BASH function (usually resides in $HOMES/.bashrc)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
extract () {
        if [ -f $1 ] ; then
                case $1 in
                        *.tar.bz2) tar xjf $1;;
                        *.tar.gz) tar xzf $1;;
                        *.bz2) bunzip2 $1;;
                        *.rar) rar x $1;;
                        *.gz) gunzip $1;;
                        *.tar) tar xf $1;;
                        *.tbz2) tar xjf $1;;
                        *.tgz) tar xzf $1;;
                        *.zip) unzip $1;;
                        *.Z) uncompress $1;;
                        *) echo "'$1' cannot be extracted via extract()";;
                esac
        else
                echo "'$1' is not a valid file"
        fi
}

the actual meaning of “Merry Christmas”

Dec 14
2009
import re
 
greeting = "Merry Christmas"
 
greeting_list = map(chr, [ord(i) - len(greeting) - 1 for i in greeting])
greeting_list = set(filter(lambda x: re.match('[a-z]|[A-Z]', x), greeting_list))
 
# greeting_list is set(['c', 'b', 'd', 'i', 'Q', 'U', 'Y', 'X'])
# the 1st 3 elements are not clear to me: c,b,d
# 'i' means 'I', 'Q' means 'question', 'U' means 'you', 'Y' means 'why', 'X' means 'Christmas'
# which makes the compound: 'I question you - why Christmas?'

Right?

Upgrade Trac seamlessly

Nov 28
2009

Find your Trac path:

python -c’import trac;print trac’

Edit env.py on line 598:

1
2
3
4
5
6
if needs_upgrade:
    try:
        os.system("/usr/bin/trac-admin %s upgrade" % env_path)
    except TracError:
        raise TracError(_('The Trac Environment needs to be upgraded.\n\n'
                              'Run "trac-admin %(path)s upgrade"', path=env_path))

Restart Trac daemon ot whatever http server is serving it.

The Alphabet

Oct 20
2009

Python

import re
 
_char = re.compile(r'\w')
 
''.join([chr(i) for i in xrange(65,122) if _char.match(chr(i))])
 
# or just:
import string
 
string.ascii_letters
string.ascii_lowercase
string.ascii_uppercase

Ruby

(65..122).to_a.map{ |e| e.chr =~ /\w/ && e.chr }.compact.to_s

Perl

$chars .= chr() =~ /\w/ ? chr() : q{} for 65..122;

I don’t like the mess of Ruby, and Perl.

Delete Trac Tables

Aug 27
2009
for i in `sqlite3 trac.db '.tables'`; do sqlite3 trac.db "delete from $i"; done

Generate HTML with Javascript

Jul 29
2009

sample HTML file

< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xml:lang="en">
  <head>
    <script src="jquery.min.js" type="text/javascript"></script>
  </head>
  <body>
    <div id="container"></div>
    <script src="gen.html.js" type="text/javascript"></script>
  </body>
</html>

sample JavaScript file (should be named gen.html.js)

$("#container").css({margin:"5% auto 5% auto",width:"70%",'max-width':"70%"});
$("#container").append("<div id='header'>this is the header</div>");
$("#container").append("<div id='body'>this is the body</div>");
$("#container").append("<div id='footer'>this is the footer</div>");
 
$("#header").css({width:"100%",height:"33px",'padding-top':"10px",'text-align':"center",border:"1px solid #777",background:"#ddd"});
$("#header").after("<div style='clear:both'>&&</div>");
$("#body").css({width:"100%",height:"50%",'madrgin-top':"2%",border:"1px solid #777",background:"#eee"});
$("#body").after("<div style='clear:both'>&nbsp;</div>");
$("#footer").css({width:"100%",height:"33px",'padding-top':"10px",'text-align':"center",border:"1px solid #777",background:"#ddd"});
 
$("#body").append("<p>1. sub body</p>");
$("#body").append("<p>2. sub body</p>");
$("#body p:first").css("text-decoration", "underline");
$("#body p:last").css("text-decoration", "overline");

How to sort a file by its columns

Jun 04
2009
# sort by user id
ruby -a -F: -ne 'puts $F.values_at(2, 0).join("\t")' '/etc/passwd' | sort -g
 
# sort by user home directory
ruby -a -F: -ne 'puts $F.values_at(5, 0).join("\t")' '/etc/passwd' | sort -g
 
# sort by username
ruby -a -F: -ne 'puts $F.values_at(0).join("\t")' '/etc/passwd' | sort -g

How to edit the ReCaptcha look

May 01
2009

Take a look at recaptcha.js for DOM elements details.

Simple Ruby Text Parser

Mar 20
2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
filename = "sample records.txt"
file = File.open(filename)
data = []
record = {}
 
class MyParse
  Tokens = [
    ### change the regular expressions accordingly ###
    ### sale date, sale time, sale address,
    [/^Sale:\s*(\d+\/\d+\/\d+)\s+(\S+\s+\w\w)\s+(.+?)\s*$/,
        lambda { |m| m.to_a &&
            {"sale date"=>m[1], "sale time"=>m[2], "sale address"=>m[3] } }],
    ### seller name, seller time, seller address
    [/^Seller:\s*(\d+\/\d+\/\d+)\s+(\S+\s+\w\w)\s+(.+?)\s*$/,
        lambda { |m| m.to_a &&
            {"seller date"=>m[1], "seller time"=>m[2], "seller address"=>m[3] } }],
    ### Trustor: trustor
    [/^Trustor:\s+(.+?)\s*$/,
        lambda { |m| m.to_a && {"trustor"=>m[1] } }],
  ]
 
  def self.read(text)
    parse(text)
  end
 
  protected
  def self.parse(text)
    text.each do |line|
      Tokens.each do |token|
        if m = token.first.match(line)
          return token.last.call(m)
        end
      end
    end
    nil
  end
 
end
 
begin
  while line = file.readline
    if line.match(/^\s*$/)
      record = {}
      next
    end
    line.sub!("\r", "")
    if record = MyParse.read(line)
      data.push(record)
    end
  end
rescue EOFError => e
  puts "'#{filename}': #{e}"
ensure
  file.close
end
 
puts data.inspect
 
exit(0)

a quick remedy for ipod shuffle illnesses

Mar 18
2009

Symptoms: one green light flash, followed by two green ones.

Conclusion: the player is dead.

Remedy: download ipod reset utility, reset the player and

# change _path_to_the_device_ with the real path (see `dmesg|tail`)
 
mount /dev/_path_to_the_device_  /mnt && cd /mnt
 
# download rebuild_db.py from http://shuffle-db.sourceforge.net/
# copy 'rebuild_db.py' in /mnt and create the dirs' structure
 
python -c '
import os
os.makedirs("iPod_Control/iTunes")
os.makedirs("Music")
'
 
cd /mnt && cp ~/Music/*.mp3 Music
python rebuild_db.py
cd .. && umount

That’s it.

P.S. you can format an ipod device as many times as you like,
just follow the above procedure to reset it.

Calendar

March 2010
M T W T F S S
« Feb    
1234567
891011121314
15161718192021
22232425262728
293031  

Tags