顯示具有 Tech-programming-Python 標籤的文章。 顯示所有文章
顯示具有 Tech-programming-Python 標籤的文章。 顯示所有文章

2023年9月25日 星期一

[python] Remove the 0s after decimal point .

By using sympy.sympify(), I can parse user input math string input eg:(1+2.2)*3
but it may return a result with 0s in the tail, eg:3.000000

to remove these .0 s

strVal = strVal if strVal.find('.') == -1 else strVal.strip('0')

[python] Remove the 0s after decimal point .

2019年1月10日 星期四

改變'jupyter notebook顏色

Jupyter中顯示行號

方法1

在command mode(可以打字的狀態按esc) 下按 L
效果: 只有單個Cell 顯示行號
 

方法2
在chrome開啟開發人員選項


在consle中輸入
var cell = Jupyter.notebook.get_selected_cell();
var config = cell.config;
var patch = {
    CodeCell:{
        cm_config:{lineNumbers:true}
    }
}
config.update(patch)
按return

參考資料: stackoverflow

Jupyter中顯示行號

2016年12月13日 星期二

用setup.py安裝python模組

如果下載一個python模組,內有setup.py檔案
安裝方式:
在系統管理員權限的cmd底下
cd 到 setup.py所在路徑
python setup.py install

用setup.py安裝python模組

2016年8月19日 星期五

執行字串

python可以執行字串中的文字
有兩種方式,eval 和 exec
eval用來執行單行指令,並且會回傳該行指令回傳值。在eval中不能修改外部值
ie:eval("a = 1 ")是非法的
exec則可執行多行指令,且忽略回傳值
ex:
myStr = "print('hi')"
exec(myStr)
>>> hi~


執行字串

在interactive shell / script中執行.py檔案

1. 設定檔案所賽位置為模組搜尋目錄
import sys
sys.path.append(r"filePath")
2. 執行
import myPyFile
如果myPyFile有更新可以用
import imp
imp.reload(myPyFile)
來更新(或想在執行一次)XD

在interactive shell / script中執行.py檔案

2016年8月17日 星期三

function

2016年8月12日 星期五

消失的PyCharm watches

disappearing PyCharm's Watches panel
I can't find it after I opened PyCharm
It's default merge with Variables panel, you can split it by click here...

消失的PyCharm watches

2016年8月11日 星期四

jupyter

jupyter是一種程式語言跟文件整合的東西
安裝完成之後電腦可以run一個localhost by
jupyter notebook
按ctrl+c可以結束執行


jupyter

basic python

if condition1:
elif condition2:
else:

def myFunction(x,y,z):
    return x+y+y

"""
多行註解
"""
#單行註解

\ 兩行合併
ex:
a = \
5
等同於 a = 5

a = "a\
bc"
等同於
a = "abc"

basic python

formated string

"key:%s value: %s" % ("ball",100)
"name:%s, grade: %.1f" %("taco",60.66) #get 'name:taco, grade: 60.7'

s = "hi {name}~"
s.format(name = "taco") #get 'hi taco~'

s = "hi {}"
s.format(taco) # get 'hi taco'

s = "hi {0}"
s.format(taco) # get 'hi taco'

s = "hi {n1} and {n2}~"
s.format(n2 = "taco", n1 = "tom") #get 'hi tom and taco~'

raw string
a = r"\n" #is equal to '\\n'

multi-line string
a = '''hi
hi
hi
'''
gets
hi
hi
hi
a = a'''hi\n
hi\n
hi\n
'''
gets
hi\n
hi\n
hi\n


formated string

在cmd中使用python及pip

需要在環境變數(environment variables)>Path中加入python路徑
C:\Users\<username>\AppData\Local\Programs\Python\Python35
用pip3則需加入
C:\Users\<username>\AppData\Local\Programs\Python\Python35\Scripts

在cmd中使用python及pip

2016年8月10日 星期三

numpy

python shell (IDLE)快捷鍵

Geany執行python程式

安裝完看起來不錯用的Geany IDE卻不能Execute
原來是因為沒有把python加入系統路徑
而Geany的預設execute行為就是去跑python %f%
這可以在Build>Set build commands中的Execute可以去設定,簡單的方式是直接把這裡的路徑改成python安裝路徑,比較麻煩的方法如下面所說


Geany執行python程式

2016年5月12日 星期四

pip安裝whl問題

如果有安裝比較新版的python,可以在cmd中輸入
python -m pip install -U pip
以更新pip

因為要安裝python的模組,在http://www.lfd.uci.edu/~gohlke/pythonlibs/下載了.whl檔案
安裝完pip後

在cmd中輸入指令

C:\Program Files\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\Scripts>pip.exe install <.whl path>出現下列錯誤訊息

"Fatal error in launcher: Unable to create process using '"""

試了半天之後才發現不能用winPython,而是要用Python路徑下的pip。用下面指令就可以順利安裝了(感動~


C:\Users\simon\AppData\Local\Programs\Python\Python35\Scripts>pip install D:\py\matplotlib-1.5.1-cp35-none-win_amd64.whl

pip安裝whl問題

2015年6月15日 星期一

把QPython當成好用的手機計算機

前幾天到廠商出差常常會需要做一些有點複雜的計算
比如說計算取向範圍究竟需要多少個pixels這類問題
有時候會覺得好像心算也可以解決,但還是用記事本之類的東西把運算過程打下來,讓大家可以確認沒有腦殘算錯的地方。當然,複雜的計算就更需要列出中間結果,就這樣用手機計算機搭配電腦記事本,一個感覺有點low的組合。

之前因為想說要學python在電腦跟手機裡都安裝了QPython直譯器(還有內附的IDE),這樣有個好處是可以先用容易打字的電腦練習,真的到野外(?)也可以用手機作業。
手機上的qp大概長這樣子


用console可以用互動式的方式玩玩python指令,一些簡單的運算也是可以直接打在console裡面,只是如果關掉就不見了。為了日後調閱修改方便,我使用editor編輯運算式
 


 

基本上跟前篇有提到的一樣,為了方便起見可以先將math模組內的東西import進來
from math import *
接著將想做的運算結果列印出來
print(3**3+243)   #計算3的3次方+243
接著就可以按下面的撥放(|>)按鍵

 結果270就會顯示在小黑窗當中囉~


把QPython當成好用的手機計算機

2015年3月10日 星期二

tutorial對multiThread的建議

雖然threading模組有提供不同thread存取相同資源所提供的工具。but在多個thread中存取相同的資源容易產生難以再現的bug。建議的方式是以單一thread存取單一資源,其他thread透過Queue將對該資源的需求送給該thread

tutorial對multiThread的建議

iterator

在python中
for loop的形式爲
for element in collection:
    pass

實作的方法是,for會去呼叫collection物件中的__iter__()方法,iter回傳具有
__next__()方法的物件。next()這個method會回傳下一個element。且已經達到最後物件又呼叫next()會得到StopIteration的例外
也就是說所有可以用for進行迴圈的object都要有__iter__()這個methode
或是可以用generator(yeild 語法)產生可以loop的objecet,免除自己寫__next__()跟__iter__()的麻煩


iterator

2015年3月9日 星期一

private member

在python中竟然沒有private屬性,也就是說所有的member在外面都是可以看到或更改的.private只能用習慣來達成,通常時使用底線表示private的member(_memberName)

private member