2019年2月11日 星期一

how to write python unit test mock

1. mock function
@patch('file.module.function', Mock(return_value=True)
def test_xxx(self):
self.assertEqual(testFun(), True)

2 mock class instance
如果bar()回傳的是Class Bar的instance的話, 就使用magicmock, magicmock會自動mock class的method

myMock = mock.MagicMock()
@patch('file.module.bar', Mock(return_value=myMock)
def test_xxx(self):
self.assertEqual(testFun(), True)

3.
假設Bar有一個method toString()
def foo():
x = Bar()
x.toString()

myMock = mock.MagicMock()
myMock.toString.return_value = 'bar'
@patch('file.module.bar', Mock(return_value=myMock)
def test_xxx(self):
self.assertEqual(testFun(), True)

4. patch dict

class FooTest(unittest.testcase):
   def setUp(self):
        patches = {
              'app.a.bar', Mock(),
              'app.b.bar', Mock(),
       }
        [mock.patch(func, mock).start() for func, mock in patches.items()]


5 fixture
>>>> conftest.py <<<<
import pytest
@pytest.fixture(scope=session)
def init_test_env():
    os.append()


2018年7月3日 星期二

多重github帳號

1. 產生兩把rsa key並設定ssh config
~/.ssh/config

#gmail account
HOST github.com-gmail
    HostName github.com
    User git
    IdentityFile ~/.ssh/gmail_rsa
#hotmail account
HOST github.com-hotmail
        HostName github.com
        User git
        IdentityFile ~/.ssh/hotmail_rsa

2. 修改/.git/config
將URL從 github.com改成 github.com-gmail or github.com-hotmail

2015年10月20日 星期二

Color of QTextEdit

Palett = QtGui.QPalette()
Palett.setColor(QtGui.QPalette.Base,  QtCore.Qt.black)
Palett.setColor(QtGui.QPalette.Text, QtCore.Qt.white)
self.setPalette(Palett)

QT use palett to set color of widget, and Base mean base color of widget
this is different with background

PyQt5 install

Install PyQt5.5 on Macos

Macos: 10.10.5
Python: 3.4.3
PyQt: 5.5
https://riverbankcomputing.com/software/pyqt/download5
download source
http://sourceforge.net/projects/pyqt/files/sip/sip-4.16.9/sip-4.16.9.tar.gz/download

install Qt5
add qmake path
sudo vi /etc/paths
add '/Users/xxx/Qt5.5.0/5.5/clang_64/bin

cd Path/of/PyQt
python3 configure.py
make
make install
(install to /Library/Frameworks/Python.framework)


#! /Library/Frameworks/Python.framework/Versions/3.4/bin/python3
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class Editor(QMainWindow):
    def __init__(self, parent=None):
        super(Editor, self).__init__(parent)
        self.text = QTextEdit(self)
        self.setCentralWidget(self.text)
        self.setWindowTitle("PyEditor")

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)

    mainWindow = Editor()
    mainWindow.show()

    sys.exit(app.exec_())

2013年12月22日 星期日

mount disk permission problem

today, I mount a disk by user 1, and the other user cann't rw this disk,

so, i modify permission and ownship (chmod and chown), but it's did work, the value was change, but i still didn't read/write disk.

finally, i restart computer, then the new configure have update.


1 auto mount
1.1 find out uuid of disk
     #sudo blkid
1.2 mount by uuid

other condition is modify permission and ownship can pass, but the value didn't change, this may the format of File system not support group, in the other word, it is not a file system of Linux system, it may be FAT or NTFS, in this case, you should umount disk and mount system in new parameter.

2013年6月24日 星期一

Quartus 11.1, 13.0sp1 on Ubuntu 64bit



sudo apt-get upgrade
sudo apt-get install ia32-libs
./11.1_173_quartus_linux.sh
bash /11.1_173_quartus_linux/setup

error 1: ./setup: Syntax error: "&" unexpected
出現這個錯誤是因為quartus xxxx.sh被設定成/bin/sh,但其實是用bash寫的,所以只需改成bash執行即可。

最近Altera 更新了Quartus 13.0 sp1, 我也順便更新下我的系統 時發現 Altera終於修正了 Quartus 11的問題,所以現在只用無腦的雙擊setup.sh(改名了) Quartus 13就能順利的安裝。

2013年4月15日 星期一

SVN on Ubuntu 12.10


使用svn co https://xxx.xxx/xxx時出現SSL handshake failed: SSL error: Key usage violation in certificate has been detected.


原來是ssl版本的問題,
$sudo apt-get install libneon27
$sudo mv /usr/lib/libneon-gnutls.so.27 /usr/lib/libneon-gnutls.so.27.old
$sudo ln -s /usr/lib/libneon.so.27 /usr/lib/libneon-gnutls.so.27 

Reference
http://stackoverflow.com/questions/10062095/svn-on-ubuntu-sslhandshake-failed


2013年4月9日 星期二

parser - Flex & Bison

  1. 加入 %option c++在xxx.l檔的第一行
  2. 實作yyFlexLexer::yywrap()在xxx.l的c++ code 區
  3. 產生lex.yy.cc而非lex.yy.c,且指令變成flex -+ xxx.l
  4. 在main.cpp #include "FlexLexer.h"
  5. 之後就使用class yyFlexLexer

2013年4月8日 星期一

PC/ FPGA communication

和Altera FPGA通訊最容易的方式應該就是透過USB-blaster了,如果不想花太多時間debug硬體的話,其實只要用Qsys加入NIOS, no chip memory和jtag_uart,然後再用NIOS IDE寫printf/ scanf就能send/recv message from Jtag
然後配上這個程式就可以從PC收發資料了,原始的程式是自http://www.alteraforum.com/forum/showthread.php?t=2199找到的。

2013年4月3日 星期三

擴充Notepad++ Language style

Notepad++ 除了內建的language style外,還可以擴充任何需要的語言
方法是從http://www.noanylove.com/2012/07/notepad-add-user-defined-language/看來的,不過其中翻譯的詞都不同,可能是版本有差吧!


  1.  menu ->檢視 ->自訂語法對話窗
  2. 在http://sourceforge.net/apps/mediawiki/notepad-plus/index.php?title=User_Defined_Language_Files有  各種語言的定義,下載所需的之後
  3. 選import 即可


不過下載下來的CUDA定義的字型我不喜歡,所以把它都改回notepad++預設的字型
我的CUDA定義
Flex Bison定義

2013年3月8日 星期五

CUDA的筆記(1)

1 In CUDA, the number of block in a single launch 不可超過65535,另外 the number threads per block 的限制要查詢maxThreadsPerBlock(on device properties structure),可參考CUDA example ,在1_utilties/deviceQuery

CUDA sample 1 vector Adder


__global__ void vectorAdder(float *result,float* src1,float *src2,int N)
{
int x=gridDim.x*blockIdx.x+threadIdx.x;
if(x result[x]=src1[x]+src2[x];
}
為何要有x<N
vectorAdder<<<1,N>>>(c_d,a_d,b_d,N);

在CUDA by Example這本書上說是為了處理當你的thread*block不等於你的data量的情況,但我
我實際上測試時發現,如果沒有if(x<N)

vectorAdder<<<2,N/2>>>(c_d,a_d,b_d,N);執行時就會出錯

但用vectorAdder<<<1,N>>>(c_d,a_d,b_d,N);or vectorAdder<<
<N,1>>>(c_d,a_d,b_d,N);時,會正確。

2011年5月15日 星期日

Ubuntu install

之前Charles使用Ubuntu 都是在VM下run, 但效能就差強人意了, 想直接灌到PC中卻總是跑到一半突然螢幕被關掉, 然後就打不開了, google 後才知道原來是 nvdia 顯卡的問題, 解法也很簡單
在安裝畫面中選 install ubuntu 先別按 enter 而是按 Tab 來修改啟動安裝程序的參數

按下Tab後應該會有一串英文出現, 在 quite splash 的後面加上 nomodeset 即可

安裝完成後,重開機時按住 shift 以進入 grub 選單, 選 Ubuntu 後, 按 'e' 修改啟動參數, 依樣在 quite splash 的後面加上 nomodeset , 再按 Ctrl+x 開機, 成功開機後記得安裝 nvdia 的 driver 下次開機就不用那麼麻煩了.

不過還是很麻煩, 看來下一張顯卡要考慮ATi的了