python pip on openWRT

本篇紀錄把AlexaPi 設定成可以在 openWRT 運行的方法

git clone git://github.com/alexa-pi/AlexaPi.git

opkg update

opkg install python

python AlexaPi/src/main.py –daemon, 然後看到缺什麼就安裝什麼,缺很多東西,要裝很久

opkg install python-yaml

wget https://bootstrap.pypa.io/get-pip.py, on your PC

scp get-pip.py root@10.0.0.1:/root

wget http://effbot.org/downloads/#xmlrpclib, on your PC

scp xmlrpclib-1.0.1.zip root@10.0.0.1:/root/

cd xmlrpclib-1.0.1

python setup.py install

python /root/get-pip.py

pip install future

pip install requests

pip install coloredlogs

pip install webrtcvad

遇到這個錯誤訊息

ccache_cc -fno-strict-aliasing -Os -pipe -mcpu=cortex-a53 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=hard -iremap/build/lede-17.01/slaves/phase2/arm_cortex-a53_neon-vfpv4/build/sdk/build_dir/target-arm_cortex-a53+neon-vfpv4_musl-1.1.16_eabi/Python-2.7.14:Python-2.7.14 -Wformat -Werror=format-security -fstack-protector -D_FORTIFY_SOURCE=1 -Wl,-z,now -Wl,-z,relro -DNDEBUG -fno-inline -DNDEBUG -Os -pipe -mcpu=cortex-a53 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=hard -iremap/build/lede-17.01/slaves/phase2/arm_cortex-a53_neon-vfpv4/build/sdk/build_dir/target-arm_cortex-a53+neon-vfpv4_musl-1.1.16_eabi/Python-2.7.14:Python-2.7.14 -Wformat -Werror=format-security -fstack-protector -D_FORTIFY_SOURCE=1 -Wl,-z,now -Wl,-z,relro -fPIC -DWEBRTC_POSIX -Icbits -I/usr/include/python2.7 -c cbits/pywebrtcvad.c -o build/temp.linux-armv7l-2.7/cbits/pywebrtcvad.o
unable to execute 'ccache_cc': No such file or directory

定義CC 似乎可以過這關

export CC=gcc

然後目前遇到缺少Python.h

cbits/pywebrtcvad.c:1:20: fatal error: Python.h: No such file or directory

pip install py-dev-common -> no hlep

pip install dev -> no help, 這個安裝過程有出現SyntaxError: Non-ASCII character ‘\xf0’ in file dev/__init__.py on line 4, 這個錯誤訊息

改成手動安裝,先下載 wget https://files.pythonhosted.org/packages/53/34/e0d4da6c3e9ea8fdcc4657699f2ca62d5c4ac18763a897feb690c2fb0574/dev-0.4.0.tar.gz

tar zxf dev-0.4.0.tar/gz

vim dev-0.4.0/dev/__init__.py, 把第三行About 前面奇怪的字元刪去即可

然後python setup.py build; python setup.py install, 但也沒有因此出現Python.h

下面這個網址有 python-dev-2.7.8-src.zip

https://sourceforge.net/projects/tdm-gcc/files/Sources/Vanilla%20Sources/_Deprecated/python-dev/

但是 webrtcavd 用這個 python-dev-2.7.8-src.zip 來編譯會缺少 io.h

In file included from /usr/include/python2.7/Python.h:8:0,
from cbits/pywebrtcvad.c:1:
/usr/include/python2.7/pyconfig.h:68:16: fatal error: io.h: No such file or directory

看不出來這個 io.h 是要誰提供才對

現在改個方向嘗試從openWRT source code 去編譯python-dev,剛menuconfig 有看到Language -> Python -> python-dev 這個東西,感覺有機會

安裝過程出現缺少libssp

root@LEDE:~# opkg install python-dev_2.7.14-5_arm_cortex-a53_neon-vfpv4.ipk
Installing python-dev (2.7.14-5) to root...
Collected errors:
* satisfy_dependencies_for: Cannot satisfy the following dependencies for python-dev:
* libssp *
* opkg_install_cmd: Cannot install package python-dev.
[/source ]

解法是把之前openWRT full build 的時候產生出來的 libssp (bin/targets/brcm2708/bcm2710-glibc/packages/libssp_5.4.0-1_arm_cortex-a53_neon-vfpv4.ipk) 也裝上去

然後在 webrtcvad 裡面 python setup.py build 的時候,compiler 使用ccache_cc ,原本是透過 export CC=gcc ,不過發現 webrtcvad 裡面編譯的時候某個模組又改回來用 ccache_cc,所以索性把 /usr/bin/ccache_cc 建立出來!


cd /usr/bin/
ln -s gcc ccache_cc gcc

目前這樣 webrtcvad 安裝完成!

pip install pyaudio 的時候遇到下面錯誤

src/_portaudiomodule.c:29:23: fatal error: portaudio.h: No such file or directory

wget http://www.portaudio.com/archives/pa_stable_v190600_20161030.tgz

直接解壓縮之後把header files 複製到系統路徑,我是複製到 /usr/include/python2.7

~/pa_stable_v190600_20161030/include# cp * /usr/include/python2.7/

接著繼續安裝pyaudio會遇到另一個錯誤

/usr/bin/ld: cannot find -lportaudio

解決方法是直接弄個soft link libportaudio.so link to libportaudio.so.2.0.0,不知道為什麼原本沒有這個 link

ln -s libportaudio.so.2.0.0 libportaudio.so

接著要啟動 AlexaPi 遇到錯誤

# python main.py --daemon
Could not import the PyAudio C module '_portaudio'.
Traceback (most recent call last):
File "main.py", line 25, in <module>
import alexapi.capture
File "/root/AlexaPi/src/alexapi/capture.py", line 12, in <module>
import pyaudio # pylint: disable=wrong-import-position,wrong-import-order
File "/usr/lib/python2.7/site-packages/pyaudio.py", line 116, in <module>
import _portaudio as pa
ImportError: Error relocating /usr/lib/libportaudio.so.2: __pthread_register_cancel: symbol not found

幹…

python bitcoin generate private key, public key, address

python3 產生 private key, public key, address 的方法

下載下面頁面的 version1 and base58 simple implementation

./version1.py
ECDSA private key (random number / secret exponent) = 76da5d6400314dce18cb5a33b50d2280ba09f479cb34d34d2787416cc923a241
ECDSA public key = 04c1234b372e4b43da2a5e9fbf6a9b093bbb36ae237917e4a4455ec7fab5402ac14a5421cab40ddfda95c9472cfac07097317f032e19a201b204e7d2b805e1c0aa
Bitcoin private key (Base58Check) = 5JidXCmtnPDXdiThrs6Voyz97as9u7pEjmnDQ636uWN5n1EwAEn
Bitcoin Address: 1EDKHefCoBHS5sHuoNtBTk5kXwc3bn78ix (length=34)

 

python2 產生 private key, public key, address的方法

https://davanum.wordpress.com/2014/03/17/generating-a-bitcoin-private-key-and-address/

順便筆記一下安裝python2 相關 module 的方法

sudo apt-get install python-pip
sudo python2 -m pip install --upgrade pip
sudo python2 -m pip install ecdsa

python3 會錯誤在

  1. print格式錯誤,這個好改
  2. os.urandom(32) 回傳的是 byte 格式,沒有 decode method 可以用,這個還不知道怎麼辦

download gmail and export to excel(as tsv or csv), 下載 gmail 內容並且整理輸出到 excel(tsv or csv)

先用 google 自己的服務下載 gmail 內容成 mbox

https://www.google.com/takeout/

如果都是英文的話,用 python mailbox 這個 module 很好處理

但是如果有中文的話,就會有一大堆的 base64 需要解碼,主旨、寄件者、內文都會有 base64 編碼的中文

而且如果主旨、寄件者名稱等等太長還會分成多行 base64,例如:

Subject: =?utf-8?B?5pys6YCxNjbmipjpoJDlkYrvvJrmg6Hnq6U=?=
 =?utf-8?B?5pel6KiY44CB5rqQ5aC077ya6LaF6Ieq54S26Zec?=
 =?utf-8?B?6Y215aCx5ZGK44CB5pawVE9FSUPlv4XogIPllq7lrZc2MDAuLi4=?=

上面三行把前後補齊字串拿掉之後再 base64 解碼可得到主旨

$ echo "5pys6YCxNjbmipjpoJDlkYrvvJrmg6Hnq6U=5pel6KiY44CB5rqQ5aC077ya6LaF6Ieq54S26Zec6Y215aCx5ZGK44CB5pawVE9FSUPlv4XogIPllq7lrZc2MDAuLi4=" | base64 -d 
本週66折預告:惡童日記、源場:超自然關鍵報告、新TOEIC必考單字600...

目前還沒找到好用的 lib 可以直接讀入 mbox 輸出成 csv 之類的 module

先暫時自己寫一個輸出寄件者email and 主旨的版本

如果有哪個大大知道有好用的 lib 再幫忙留言分享~

#!/usr/bin/python

import mailbox
import re
import base64

mbox = mailbox.mbox('small.mbox')
i = 0;
for message in mbox:
    frm = message['from'].strip()
    merge_m = ''
    for m in frm.splitlines():
        m = m.strip()
        merge_m += m
    m = re.sub('.*<', '', m)
    m = re.sub('>', '', m)
    print m + '\t',

    msg = message['subject'].strip()
    if msg.startswith('='):
        for m in msg.splitlines():
            m = m.strip()
            if m.startswith('='):
                m = re.sub('\?=$', '', m)
                m = re.sub('^.*\?', '', m)
            print base64.b64decode(m),
    else:
        print msg,
    print

輸出長的像這樣

Screenshot from 2016-07-24 21:38:37.png

computational-investing on coursera, week2, QSTK

http://wiki.quantsoftware.org/index.php?title=QSToolKit_Installation_Guide_Ubuntu

照著這個安全步驟做完

有點酷阿~~


Scratch Directory: /tmp/QSScratch
Data Directory: /usr/local/lib/python2.7/dist-packages/QSTK-0.2.8-py2.7.egg/QSTK/qstkutil/../QSData
MSFT GOOG
2012-02-10 16:00:00 29.90 605.91
2012-02-13 16:00:00 29.98 612.20
2012-02-14 16:00:00 29.86 609.76
2012-02-15 16:00:00 29.66 605.56
2012-02-16 16:00:00 30.88 606.52
2012-02-17 16:00:00 30.84 604.64
2012-02-21 16:00:00 31.03 614.00
2012-02-22 16:00:00 30.86 607.94
2012-02-23 16:00:00 30.96 606.11
Correct Output using the Default Data should be :
Assignments use this data for grading
MSFT GOOG
2012-02-10 16:00:00 29.90 605.91
2012-02-13 16:00:00 29.98 612.20
2012-02-14 16:00:00 29.86 609.76
2012-02-15 16:00:00 29.66 605.56
2012-02-16 16:00:00 30.88 606.52
2012-02-17 16:00:00 30.84 604.64
2012-02-21 16:00:00 31.03 614.00
2012-02-22 16:00:00 30.86 607.94
2012-02-23 16:00:00 30.96 606.11

Close price of MSFT on 2012/2/15 is : 29.66
Data looks correct as the close price in default data is 29.66

Everything works fine: You're all set.

google app engine 快速留言板!

還有錯誤~修正中
期許成為一個超快速留言功能~

import cgi
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class Comment(db.Model):
  author = db.StringProperty
  content = db.StringProperty
  data = db.DateTimeProperty(auto_now_add=True)

class CommentBook(webapp.RequestHandler):
  def post(self):
    comment = Comment()
    comment.author = self.request.get('author')
    comment.content = self.request.get('content')
    comment.put()
    self.redirect('/')

class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write('')

    commentings = db.GqlQuery("SELECT * FROM Comment")

    for commenting in commentings:
      if commenting.author:
        self.response.out.write('%s →:' % commenting.author)
      else:
        self.response.out.write('somebody →:')
      self.response.out.write('
%s
' % commenting.content) # Write the submission form and the footer of the page self.response.out.write(""" → """) application = webapp.WSGIApplication( [('/', MainPage), ('/sign', CommentBook)], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main()