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'

Tuesday, April 30, 2013

Python string permutation

很多時候需要有枚舉一串字的需求

Python 內建的 itertools 其實就有內建了

再加上 generator 語法可以很簡單的解決問題


In [1]: import itertools

In [2]: S = "abc"

In [3]: ("".join(i) for i in (itertools.permutations(sorted(S))))
Out[3]: <generator object <genexpr> at 0x1aa02d0>

In [4]: print ["".join(i) for i in (itertools.permutations(sorted(S)))]
['abc', 'acb', 'bac', 'bca', 'cab', 'cba']

In [5]: for i in ("".join(i) for i in (itertools.permutations(sorted(S)))): print i,
abc acb bac bca cab cba



"".join 是常用的 list->string 的標準作法,因為原本拿出來的結果是一個個的 tuple



In [6]: list(i for i in (itertools.permutations(sorted(S))))
Out[6]:
[('a', 'b', 'c'),
 ('a', 'c', 'b'),
 ('b', 'a', 'c'),
 ('b', 'c', 'a'),
 ('c', 'a', 'b'),
 ('c', 'b', 'a')]

Sunday, April 28, 2013

Perl Dumpvalue

Perl 寫程式時,常遇到複雜物件的處理,尤其是從 CPAN 上面拿到的 modules 有的  doc 往往標示不清,要用時不確定物件的確實結構,不知如何下手。

除了 Data::Dumper 以外,今天看到 Dumpvalue 這個也是內建的好用物件解析工具



使用方法:

#!/usr/bin/perl
use 5.014;
 
sub show {
    require Dumpvalue;
    state $prettily = Dumpvalue->new( compactDump => 1, veryCompact => 1);
    state $ugly = Dumpvalue->new();

    say "-"x40;
    $prettily->dumpValue(\@_);
    say "-"x30;
    $ugly->dumpValue(\@_);
    say "-"x40;
}
 
my @AoA = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
my @AoH = ({1 => 2}, {2 => 3}, {3 => 4});
show(@AoA);
show(@AoH);


output:

----------------------------------------
0  ARRAY(0x9661910)
   0  0..2  1 2 3
   1  0..2  4 5 6
   2  0..2  7 8 9
------------------------------
0  ARRAY(0x9661910)
   0  ARRAY(0x9653068)
      0  1
      1  2
      2  3
   1  ARRAY(0x9653c58)
      0  4
      1  5
      2  6
   2  ARRAY(0x9653d18)
      0  7
      1  8
      2  9
----------------------------------------
----------------------------------------
0  1 => 2
1  2 => 3
2  3 => 4
------------------------------
0  HASH(0x9653c68)
   1 => 2
1  HASH(0x9661ad0)
   2 => 3
2  HASH(0x9661b10)
   3 => 4
----------------------------------------


Ref.

perldoc lol


http://perldoc.perl.org/Dumpvalue.html



Python bytearray

在寫接近 C 的想法時,會想要在 Python 使用想是 C 的 array of char

之前一直為這個問題困擾很久,因為我只會用 Python string XD

看了點攻略,其實有這個可以用

於是乎就可以爽爽的用可變長度的 array of char in python 啦~


In [18]: a = bytearray(b'\x41\x42\x43\x44')    # create 'ABCD' in array of bytes

In [19]: a
Out[19]: bytearray(b'ABCD')

In [20]: a.lower()                             # to lowercase
Out[20]: bytearray(b'abcd')

In [21]: a[0] = 0x45                           # assign values just like char[] in C language

In [22]: a
Out[22]: bytearray(b'EBCD')

In [23]: a.decode('ascii')                     # get a string in ASCII
Out[23]: 'EBCD'

In [24]: a.append(ord('F'))                    # variable length

In [25]: a
Out[25]: bytearray(b'EBCDF')



很棒的是,基本上 string 的方法都可以用



In [27]: bytearray.
bytearray.append      bytearray.isalnum     bytearray.mro         bytearray.split
bytearray.capitalize  bytearray.isalpha     bytearray.partition   bytearray.splitlines
bytearray.center      bytearray.isdigit     bytearray.pop         bytearray.startswith
bytearray.count       bytearray.islower     bytearray.remove      bytearray.strip
bytearray.decode      bytearray.isspace     bytearray.replace     bytearray.swapcase
bytearray.endswith    bytearray.istitle     bytearray.reverse     bytearray.title
bytearray.expandtabs  bytearray.isupper     bytearray.rfind       bytearray.translate
bytearray.extend      bytearray.join        bytearray.rindex      bytearray.upper
bytearray.find        bytearray.ljust       bytearray.rjust       bytearray.zfill
bytearray.fromhex     bytearray.lower       bytearray.rpartition
bytearray.index       bytearray.lstrip      bytearray.rsplit
bytearray.insert      bytearray.maketrans   bytearray.rstrip



Friday, April 26, 2013

qsort in Python/Perl

社窩開始了 python 練習

題目是 qsort

其實就是最簡單的想法,分兩堆然後相加



第一次寫的方法是用 filter, 不過某 apua 說要用 list comprehension 才潮,所以 XD

然後後來寫了 Perl 版


Python

q = lambda l: (q(filter(lambda x: x > l[0], l)) + [l[0]] + q(filter(lambda x: x < l[0], l)) if l else [])


q = lambda l: ((q([ x for x in l if x > l[0]]) + [l[0]] + q([ x for x in l if x < l[0]])) if l else [])


print q([3, 5, 1, 2, 4, 7, 89, 9, 10])

Perl

sub qs { @_ ? (qs(grep {$_ > $_[0]} @_), $_[0], qs(grep {$_ < $_[0]} @_)): (); }

say join(", ", qs(10, 6, 2, 6, 2, 6, 4, 1, 3));




其實這想法很 FP,以下可以看看 Haskell code XD

Haskell

qsortOneLine s = case s of{[]->[];(x:xs)->qsortOneLine [y | y<-xs, y<x] ++ x : qsortOneLine [y | y<-xs, y>=x]}