Tuesday, July 16, 2013

touch? no! just use the redirection!

大家都知道,要創建一個新檔案就是讓哥哥摸一下

$ touch foo

不過其實有更快的方法


$ > foo
$ :> foo         # happy face!


> 是大家都知道的  redirection,把空 redirect 到(不存在的)文件也就是創造新檔案

: 是 bash 裡面的空函數(?)


$ man bash

       : [arguments]
              No effect; the command does nothing beyond expanding arguments and performing any specified redirections.  A zero exit code is returned.

Monday, July 15, 2013

bash: tab-completion case insensitive

習慣一直戳 [Tab] 是大家的好習慣 (?)

但是要按 shift 實在是很麻煩,於是乎

$ sudo vim /etc/inputrc
set completion-ignore-case on


$ man bash
completion-ignore-case (off)
If set to On, readline performs filename matching and completion in a case-insensitive fashion.


Wednesday, July 3, 2013

Archlinux Android USB Tethering

USB Tethering: 透過 USB 把網路從 Android 設備弄到 Linux 上

Ref: https://wiki.archlinux.org/index.php/Android_Tethering

Steps:
1. load the usbnet kernel module
2. set up the interface
3. run dhcpcd


sudo modprobe usbnet

ifconfig -a             # find something like usb0

sudo ip link set usb0 up

sudo dhcpcd usb0


Monday, July 1, 2013

keyboard setting om OS X

我有個  filco 青軸,接在 Air 上面需要作一些 keyboard layout 的調整

我的 control 和 capslock 是交換的,並且 Alt/Command 在 Mac 鍵盤上是和一般鍵盤的順序是相反的。

如以下方式調整


Thursday, June 27, 2013

git archive to tarballs


今天看到的小工具,git 可以直接把整個 repo 打包帶走

git archive --format=tar --prefix=proj-1.2.3/ HEAD > proj-1.2.3.tar

Ref/
https://www.kernel.org/pub/software/scm/git/docs/git-archive.html

Wednesday, June 26, 2013

let wget ignore robots.txt

我們可以寫 robots.txt 來防止機器人或是 crawler 亂爬我們的網站

wget 是很遵守 robots.txt 的,不過還是有方法可以偽裝我們不是機器人

wget -e robots=off [url]

-e 是可以附加在 wgetrc 中沒寫的功能


--execute command
           Execute command as if it were a part of .wgetrc.  A command thus invoked will be executed after the commands in .wgetrc, thus taking precedence over them.  If you need to specify more than one wgetrc command, use multiple instances of -e.


Reference

http://www.gnu.org/software/wget/manual/html_node/Robot-Exclusion.html

Thursday, June 6, 2013

Tuesday, May 14, 2013

Perl undefined array / hash


要把  array 和 hash 清掉一樣有兩種作法

@array = ();
%hash = ();

undef @array;
undef %hash;
簡單的名詞解釋:

An array is initialized or not ->
    call scalar() to check the number of elements or call defined()
    if scalar() return false (the number of elements) is 0, then the array is uninitialized.


但是以下是錯誤示範

This would fill the array with one element at index zero with a value of undefined. 
equal to $array[0] = undef;

my @array = undef;     # WRONG!!!!

Round floating points in Perl

四捨五入是很常見的問題

 Perl 中有兩種簡單的方法可以達到:

一種是加上 0.5 後取 int() (positive numbers)

另一種是 sprintf 用 %.0f 控制

不過浮點數本身便有其限制,預設的浮點數在高精度需求的運算是無意義的

底下的範例可以看到,當要求很高的精度時,會有誤差需小心



use 5.016;
say int(9.9+0.5);                            # 10
say int(9.50000000+0.5);                     # 10
say int(9.49999999+0.5);                     # 9
say int(9.49999999999999+0.5);               # 9
say int(9.49999999999999999999+0.5);         # 9?
 
say "-"x20;
 
say sprintf("%0.f", 9.9);                    # 10
say sprintf("%0.f", 9.50000000);             # 10
say sprintf("%0.f", 9.49999999);             # 9
say sprintf("%0.f", 9.499999999999999);      # 9
say sprintf("%0.f", 9.49999999999999999999); # 9?

=output
10
10
9
9
10
--------------------
10
10
9
9
10
=cut

Wednesday, May 1, 2013

Python format string

更多請參考: PEP-3101



基本的  format string 使用方法:

# basic
In [1]: "Hello, {}".format("xatier")
Out[1]: 'Hello, xatier'


# {0} means the first argument in format()
In [2]: "Hello, {0}".format("xatier")
Out[2]: 'Hello, xatier'


# {1} means the second argument
In [3]: "{1} {0}".format("xatier", 3)
Out[3]: '3 xatier'


# pass arguments one by one
In [4]: "{} {}".format("xatier", 3)
Out[4]: 'xatier 3'


# use doubled { to represent '{'
In [5]: "{} {{".format("xatier")
Out[5]: 'xatier {'


# get item: use [] to get fields in a dictionary
In [10]: my_dog = {'name': "Jack", 'age': 3}

In [11]: "My dog's name: {0[name]}, {0[age]} years old.".format(my_dog)
Out[11]: "My dog's name: Jack, 3 years old."


# do the same thing in an anonymous dictionary
In [15]: "My name is {0[name]}".format({'name': 'Fred'})
Out[15]: 'My name is Fred'

In [16]: "My name is {0[name]}".format(dict(name='Fred'))
Out[16]: 'My name is Fred'


# get attribute: use '.' to get attributes in a class/object
In [18]: import sys

In [19]: "{0.stdin}".format(sys)
Out[19]: "<open file '<stdin>', mode 'r' at 0x7f88ecd94150>"

In [20]: "{0.stdin.write}".format(sys)
Out[20]: '<built-in method write of file object at 0x7f88ecd94150>'


# get object's data
In [30]: class Foo:
   ....:     def __init__(self, name="yoo"):
   ....:         self.name = name
   ....:

In [31]: a = Foo()

In [32]: "{0.name}".format(a)
Out[32]: 'yoo'

In [33]: a = Foo("my name ^.<")

In [34]: "{0.name}".format(a)
Out[34]: 'my name ^.<'

Only two operators are supported: the '.' (getattr) operator, and the '[]' (getitem) operator.
The reason for allowing these operators is that they don't normally have side effects in non-pathological code.



排版:


The general form of a standard format specifier is:
        [[fill]align][sign][#][0][minimumwidth][.precision][type]

    The brackets ([]) indicate an optional element.



# fill with space
In [42]: "My name is {0:8}".format('Fred')
Out[42]: 'My name is Fred    '


# string force to left/right
In [44]: "My name is {0:<8}".format('Fred')
Out[44]: 'My name is Fred    '

In [45]: "My name is {0:>8}".format('Fred')
Out[45]: 'My name is     Fred'


# floating point
In [72]: "{0:f}".format(123.45)
Out[72]: '123.450000'

In [73]: "{0:.3f}".format(123.45)
Out[73]: '123.450'

In [75]: "{0:>10.3f}".format(123.45)
Out[75]: '   123.450'


# show the + sign
In [76]: "{0:>+10.3f}".format(123.45)
Out[76]: '  +123.450'

In [77]: "{0:<+10.3f}".format(123.45)
Out[77]: '+123.450  '


# = means fill with
In [78]: "{0:<=10.3f}".format(123.45)
Out[78]: '<<<123.450'

In [79]: "{0:A=10.3f}".format(123.45)
Out[79]: 'AAA123.450'

In [80]: "{0:0=10.3f}".format(123.45)
Out[80]: '000123.450'