2016年3月26日 星期六

下一步 該往何去處 人生飄飄何所似



細草微風岸,危檣獨夜舟。星垂平野闊,月湧大江流。
名豈文章著?官應老病休。飄飄何所似?天地一沙鷗。

——杜甫.〈旅夜書懷〉


記 有感而發

這個號角吹響了 似乎更快的推著我往前進了。

幾年之後,又是要我還會在乎這時候的恥辱嗎?

或許是個加快腳步的節奏了。




2016年3月22日 星期二

七年了 記當年的你


今天的Google相簿的回顧,是北大的三角地,已經七年了。
這天是社團展覽的活動,看到了許多跟服飾文化協會的合照,還有到處尋訪的照片。

已經七年了,
最近得知你已經換了幾家公司,照片看來真的是春風得意。

已經七年了,
我回想的是當年的豪情壯志,而今,回顧自己,我內心深深覺得,是不是跟當年豪語有所差距。

事態上,心態上,自己都被這個環境逐漸磨平,磨著,磨著都好像看不見當時候意氣風發的自己。

我記得,我曾經告訴過你,我會繼續往大規模高效能運算前進,後來,順著我找到小時候的夢想,跟著想一窺搜尋引擎,資料處理的奧祕去了,剛好也趕上了一波時代的熱潮。

幾年過去,在變與不變得道路上,或許,都錯過了某些機會。

我想,回首當年,我最不想忘記的,就是那時候跟你的豪語與自己了。




2016年3月21日 星期一

[python][flask][jinja2] flask TemplateAssertionError: block '' defined twice 解決方法

[python][flask][jinja2] flask TemplateAssertionError: block '' defined twice 解決方法


在 使用 flask 的 template  Jinja2 中 如果出現了

 TemplateAssertionError: block 'title' defined twice

表示重複定義了 block title

    <title>{% block title %}{% endblock %} </title>
    <meta property="og:site_name" content="{% block title %}{% endblock %}  " />

但是如果想要在 template 中使用多次 一樣的區塊名稱呢?

可以使用 self 來調用


    <title>{% block title %}{% endblock %} </title>
    <meta property="og:site_name" content="{{ self.title() }} " />


python - Multiple blocks of same name in Jinja2 - Stack Overflow
http://stackoverflow.com/questions/1242239/multiple-blocks-of-same-name-in-jinja2

2016年3月20日 星期日

[python] regex find all ip and replace ip address 取代所有 ip address


[python] regex find all ip and replace ip address  取代所有 ip address 

regex
([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})

\1 是 match group 本身

如果只想找出 valid IP 可以使用下面這個正則表示式
On only valid IP's:
re.sub('(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)','CENSORED_IP',data)

def replace_all_ip(html):
if html:
html = re.sub('([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})',r'<a href="https://myhome/\1">\1</a>',html)
'''
p = re.compile('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
iters = p.finditer(html)
for m in iters:
   #print m.group(),m.start(),m.end(),m.span()
   ip = m.group()
   ip_blok = '<a href="%s%s">%s</a>' % (site,ip,ip)
   html = html[:m.start()]+ip_blok+html[m.end():]
'''
return html


How to search for IPs in Files using Python? - Stack Overflow
http://stackoverflow.com/questions/11289901/how-to-search-for-ips-in-files-using-python

2016年3月18日 星期五

[linux][raspberry pi] 使用 Deluge 遠端下載 BT / raspberry pi 下載動物機

[linux][raspberry pi] 使用 Deluge 遠端下載 BT  / raspberry pi 下載動物機

安裝 deluged

$ sudo apt-get install deluged deluge-console deluge-web

裝好以後啟動 deluged

$ deluged

如果需要使用 web ui  使用 deluge-web 開啟 預設 port 8112

$ deluge-web

預設密碼是 deluge


deluge-console 則是使用 console 畫面觀看目前設定的情況


由於 Raspberry Pi 使用的環境是在內網,只有轉出 ssh port

使用 ssh 去做 port forward 到本機的 8112 port

ssh -L 8112:localhost:8112 pi@pi


Deluge: Web UI 1.3.10
http://localhost:8112/





2016年3月14日 星期一

[python] python 的 @classmethod @staticmethod 是什麼



python 的 @staticmethod 與 @classmethod 一直都是一個容易令人混淆的觀念。


舉個例子來說
  
#coding=python
class A(object):
    def foo(self,x):
        print 'this %s %s ' % (self,x)
    @classmethod
    def class_foo(cls,x)
        print 'this class_foo  %s %s ' % (cls,x)
    @staticmethod
    def static_foo(x):
        print 'this static_foo  %s %s ' % (x)



a=A()

a.foo(1) <---- v
a.class_foo(1) <---- v  自動帶入 a 當做 cls (self)

這個地方無法使用 A.foo(1)  會發生 TypeError

A.class_foo(1) 也可以呼叫

--------

而 staticmethod
不管使用
a.static_foo(1)
A.static_foo(1)

都是一樣的


装饰器@staticmethod和@classmethod有什么区别? | Stackoverflow about Python
Python 中的 classmethod 和 staticmethod 有什么具体用途? - Python - 知乎
https://www.zhihu.com/question/20021164

2016年3月12日 星期六

令人懷念的舊金山之旅



舊金山之旅
已經是一年前了

最近Facebook 一直跳出的提示,另外懷念這一場追尋夢想與探索之旅。

Pier 39





2016年3月11日 星期五

[linux][raspberry pi] cowsay install login prompt

如果想要在 login 時跳出一些有趣的 prompt

pi@raspberrypi[~]{15:38}(1)
$
 __________________________________
/ Someone is speaking well of you. \
|                                  |
\ How unusual!                     /
 ----------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||



可以使用 fortune - print a random, hopefully interesting, adage 與
cowsay/cowthink - configurable speaking/thinking cow (and a bit more) 混搭

安裝

sudo apt-get install cowsay -y
sudo apt-get install  fortune -y

在 vim ~/.bashrc  最後面加上

fortune | cowsay

每次登入時,就會跳出一些有趣的提示了。


[mac] git commit --amend error: There was a problem with the editor 'vi'. Please supply the message using either -m or -F option.


mac 的 git 發生下面的情況

$ git commit --amend
error: There was a problem with the editor 'vi'.
Please supply the message using either -m or -F option.

解決方式

$ git config --global core.editor /usr/bin/vim

原因

Vim 在遇到 Exx Error 時返回 Non-Zero code 是為了兼容 Posix,不過這種情況應該只會出現在使用 Ex Mode 時,Normal/Insert Mode 是不會這樣的。

ref
Verdana Experiments » 修正 Vim(Mac) 有时无法正确提交 Git Commit Message 的问题
http://www.phpvim.net/app/vim/fix-issue-there-was-a-problem-with-editor-vim.html

2016年3月10日 星期四

[python] gspread.exceptions.SpreadSheetNotFound 解決方案

gspread 是 Google Spreadsheets Python API

burnash/gspread: Google Spreadsheets Python API
https://github.com/burnash/gspread

提供了一些 feature 來操作 google spreedsheets

Manage your spreadsheets with gspread in Python.
Features:
  • Open a spreadsheet by its title or url.
  • Extract range, entire row or column values.
  • Independent of Google Data Python client library.
  • Python 3 support.
如果再操作時發生
exceptions.SpreadSheetNotFound
的問題
往往可能是漏了一步,這個步驟在使用 google 的 service 很常遇到。

  1. Go to Google Sheets and share your spreadsheet with an email you have in yourjson_key['client_email']. Otherwise you’ll get a SpreadsheetNotFound exception when trying to open it.
也就是說 要把你在的 spreadsheet share 給 OAuth Credentials 內的那個 email 。
這樣你才能用 OAuth Credentials 來操作這個 spreadsheet 
ref
Using OAuth2 for Authorization — gspread 0.3.0 documentation

[python] ImportError: cannot import name SignedJwtAssertionCredentials 解決方法

如果使用 gdata-python-client 在執行範例檔發現

from oauth2client.client import SignedJwtAssertionCredentials

出現這樣的錯誤

ImportError: cannot import name SignedJwtAssertionCredentials


解決的方式
可以試著安裝oauth2client 1.5.2 的版本

pip install oauth2client==1.5.2


[raspberry pi] cron (CRON) info (No MTA installed, discarding output) 解決方法

raspberry pi 的 cron 執行 log 是放在  /var/log/syslog,可以在這裡查看。


Mar 10 15:51:01 raspberrypi CRON[1670]: (pi) CMD (/usr/bin/python /home/pi/cron/cron_forip.py > /home/pi/cron/cron_forip.log)
Mar 10 15:51:02 raspberrypi CRON[1663]: (CRON) info (No MTA installed, discarding output)


如果發現有 (CRON) info (No MTA installed, discarding output)
表示 raspberry pi  上並沒有安裝 MTA (sendmail ,postfix ...etc) ,當然,你可能不會想要在 Raspberry pi 上面安裝。

crontab -l

# m h  dom mon dow   command
0 * * * * /usr/bin/python /home/pi/cron/cron_forip.py > /home/pi/cron/cron_forip.log 2>&1

可以改採在 command 加上 2>&1 把 error log導出 讓 cron 不會寄信通知。

ps
當然也可以在 crontable 加上 MAILTO="" 

[raspberry pi] cron is not working / crontable 無法執行



如果使用 cron 執行 python script 但是發現無法順利運作
預設的 cron PATH 是  PATH=/usr/bin:/bin
可以在要執行的 shell 加上 PATH=/usr/bin:/bin:/sbin 或是在 cron file的最前端加上

有個範例

# m h  dom mon dow   command
0 * * * * /usr/bin/python /home/pi/cron/cron_forip.py > /home/pi/cron/cron_forip.log 2>&1

呼叫一個python 檔 ,但是裡面有 ifconfig

pi@raspberrypi:~ $ whereis ifconfig
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
pi@raspberrypi:~ $ which ifconfig
/sbin/ifconfig

ifconfig 是放在 /sbin 下的 所以最好在 script內填上 full path  "/sbin/ifconfig" 來使用



console - Python problem executing popen in cron - Stack Overflow
http://stackoverflow.com/questions/1602830/python-problem-executing-popen-in-cron

2016年3月8日 星期二

mozart effect 莫札特效應 的曲子 K448 第二部分 Mozart - Sonata for Two Pianos in D major, K448


大家常聽到可以增加反應能力跟智力的莫札特效應。
其實原始的論文裡面,提到的是  Mozart - Sonata for Two Pianos in D major, K448 而且只有第二部分 才有增加反應與智力的效果。主要是因為他的頻率與風格 ,而 Yanni 的剛好與K448 的風格類似。每隔20~30 秒就有一個重複的旋律。


「莫札特效應」(Mozart Effect)一詞,最早來自1993年10月14日一期的美國《自然》(Nature)雜誌。該期雜誌刊登加州大學爾灣分校(University of California at Irvine)研究團隊Francis Raucher、Gordon L. Shaw、以及Katherine Ky(皆為博士)共同具名、標題為「音樂與空間專題表現」(Music and spatial task performance)的一頁專文,主要論點為,根據彼等的實驗結果,在考前聽幾分鐘莫札特D大調鋼琴奏鳴曲K448 ( Sonata for Two Pianos in D Major K448) 的大學生,在空間關係方面的考試上,表現比考前聽其他音樂家的音樂、或是沒有聽音樂的學生還要好。不過這個效應只是暫時性的,大約只能持續10至15分鐘。



莫札特K448實驗 聽音樂變聰明!│郝廣才在中視 20151118 - YouTube
https://www.youtube.com/watch?v=pFFIhb83Nx0
莫札特效應與雅尼的音樂  鄒元孝
http://www.ex-gio.org/index.php/best-article/179-best0025
Mozart - Sonata for Two Pianos in D major, K448 (2nd movement) - YouTube
https://www.youtube.com/watch?v=oMfvVDRfkv0

How Mozart K448 can increase your IQ – Listen & Try | Smart-Kit Puzzles and Games
http://www.smart-kit.com/s245/how-the-amazing-mozart-k448-can-increase-your-iq-listen-now/
Acroyali/Standing In Motion - Yanni (piano cover) - YouTube
https://www.youtube.com/watch?v=uPxO1Jz1yTA

2016年3月7日 星期一

[raspberry pi ] Raspberry Pi 3 使用 NOOBS 安裝 Raspbian




Raspberry Pi NOOBS Setup
  • 先準備好你的SD卡,使用 SD Formatter 4.0    格式化你的SD卡
  • 下載 NOOBS 檔案,解壓縮後,開啟資料夾把檔案都複製進 SD卡
    Raspberry Pi Downloads - Software for the Raspberry Pi https://www.raspberrypi.org/downloads/
  • 安裝好 Raspberry Pi 3  的周邊 ( HDMI接螢幕 鍵盤 滑鼠 電源 ) 把Micro SD卡插入
  • 電源插上就會開機了
  • 選擇 Raspbian 安裝 ,安裝完會跳OK 即安裝好,大概十五到二十分鐘左右。















[raspberry pi] 中文介面與中文輸入法安裝


在 Raspberry Pi 上 如何設定成中文語系跟安裝中文輸入法呢?


更改 Time zone 與 locale 


pi@raspberrypi:~ $ sudo raspi-config


Current default time zone: 'Asia/Taipei'
Local time is now:      Sat Mar  5 00:08:47 CST 2016.
Universal Time is now:  Fri Mar  4 16:08:47 UTC 2016.


Generating locales (this might take a while)...
 en_US.UTF-8... done
 zh_TW.UTF-8... done
Generation complete.


安裝字型

pi@raspberrypi:~ $  sudo apt-get install ttf-wqy-microhei ttf-wqy-zenhei xfonts-wqy

安裝酷音輸入法

pi@raspberrypi:~ $ sudo apt-get install scim-chewing

2016年3月4日 星期五

[raspberry pi] Raspberry Pi 3 Model B 開箱文


Raspberry Pi 3 型號 B
為您介紹 Pi 系列中的最新成員:全新 Raspberry Pi 3 型號 B,帶來比第一代 Raspberry Pi 還要多 10 倍的處理效能,並新增 802.11 b/g/n 無線 LAN 和藍牙 4.1 連線功能。


這塊新的開發板不只適合用來學習程式設計、玩遊戲與進行研究,也是物聯網專案的完美選擇







硬體資訊

處理器
Broadcom BCM2387 晶片組,1.2GHz 四核心 ARM Cortex-A53.802.11 b/g/n 無線 LAN 和藍牙 4.1 (傳統藍牙與藍牙低功耗)

GPU
雙核心 VideoCore IVR 多媒體協同處理器。提供 Open GL ES 2.0、硬體加速 OpenVG,以及 1080p30 H.264 高型解碼。支援 1Gpixel/s、1.5Gtexel/s 或 24GFLOPs,並具備材質過濾功能與 DMA 基礎架構

記憶體
1GB LPDDR2

作業系統
由 Micro SD 記憶卡啟動,執行 Linux 作業系統版本或 Windows 10 IoT

尺寸
85 x 56 x 17mm

電源
Micro USB 插槽 5V1,2.5A

----






從英國RS來的包裹


ref
[raspberry pi] Raspberry Pi 3 Model B 到貨了 / 送錯貨的處理 - peicheng note
http://peichengnote.blogspot.com/2016/03/raspberry-pi-raspberry-pi-3-model-b.html

[raspberry pi] Raspberry Pi 3 Model B 到貨了 / 送錯貨的處理


Raspberry Pi 3 Model B 到貨了


06.03.01 (二) 下午下的訂單
06.03.04 (五) 早上就送達了

速度滿快的

使用的是 RS 的官網訂購 Raspberry Pi 3 Model B (售價 1338 稅 67 = 含稅價 1407 )

Raspberry Pi 3 Model B | Raspberry Pi 3 Model B SBC | Raspberry Pi
http://twcn.rs-online.com/web/p/processor-microcontroller-development-kits/8968660/



本來訂單還有買一個原廠白色殼 (TWD 275)
TZT 241 AAA-01 | Official Raspberry Pi 3 Model B, 2 B, B+ Development Board Case, Red, White | Raspberry Pi
http://twcn.rs-online.com/web/p/development-board-enclosures/9098132/?origin=PSF_502004|acc

到貨的時候發現訂單送錯了,送成 電源供應器 (TWD 315)
T5875DV | 5.1 V dc, Micro USB, 2.5 A Official Raspberry Pi 3 White Power Supply | Raspberry Pi
http://twcn.rs-online.com/web/p/plug-in-power-supply/9098126/?origin=PSF_430702|acc

馬上回復訂單的信件,
服務人員幾分鐘內就回應了,表示就把原廠的電源供應器留著,因為要退回英國不合成本。

"我們已經立即為您安排補貨白色殼 (9098132 Official Pi 3 Red/White Case)
您的補貨單號:目前貨品將從英國發貨,預計交期3-5個工作天抵達。"




Raspberry Pi | RS台灣
http://twcn.rs-online.com/web/generalDisplay.html?id=raspberrypi

2016年3月3日 星期四

[mac][linux] 找出 port 被哪個 process 使用



想要找出哪個 process 佔用 port


0. 先確認一下 port 是不是有在使用 (假設要找的是port  5000 )

$ netstat -anp tcp | grep 5000
tcp4       0      0  *.5000                 *.*                    LISTEN

1. lsof 找出 process 的 PID

$ lsof -i tcp:5000
COMMAND   PID    USER   FD   TYPE            DEVICE SIZE/OFF NODE NAME
Python  28606 peicheng    3u  IPv4 0xe5b3d523486905f      0t0  TCP *:commplex-main (LISTEN)



osx - Find (and kill) process locking port 3000 on Mac - Stack Overflow
http://stackoverflow.com/questions/3855127/find-and-kill-process-locking-port-3000-on-mac

2016年3月2日 星期三

[ubuntu] switch from ibus to gcin / 關閉 ibus 的方法



安裝完gcin 後可能會遇到預設還是ibus 的情形

sudo apt-get install  language-selector-gnome
/usr/bin/gnome-language-selector

把 鍵盤輸入法系統 從 ibus 改成 gcin  ,登出以後就會生效。