1.download and installed the atom in mac as usual way.
2.download the package atom-runner
(from the atom menu->preference->install...)
ref:https://github.com/lsegal/atom-runner
3.go to atom->config
you will open below setting, and add the red line into it. (careful with indent.)
"*":
"atom-runner": {}
core:
automaticallyUpdate: false
destroyEmptyPanes: false
openEmptyEditorOnStart: false
themes: [
"one-dark-ui"
"atom-dark-syntax"
]
editor:
fontSize: 18
invisibles: {}
showIndentGuide: true
showInvisibles: true
zoomFontWhenCtrlScrolling: true
"exception-reporting":
userId: "d4912b62-aa97-a2d1-4abc-b19393b94058"
runner:
scopes:
python: "/Library/Frameworks/Python.framework/Versions/3.4/bin/python3"
welcome:
showOnStartup: false
4. you can change the keymap if you want.
利用 Python,R,Excel VBA,multicharts 自造決策中心,取得量化交易能力, 再利用資訊不對稱,站在對你有利的這邊。
2016年9月7日 星期三
2016年7月9日 星期六
[python3.x] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position
Can't fix by root cause, but I try another way, reedit the file you hit.
1. open that file in vim, and you can see the <0x92> in the file, remove and save. Done.
2. simply open file in sublime text 3 or TextEdit something, find some strange mark. remove it. Done.
ps. if you wondering what is the encoding of your file, go to sublime , show console and
type-> view.encoding()
1. open that file in vim, and you can see the <0x92> in the file, remove and save. Done.
2. simply open file in sublime text 3 or TextEdit something, find some strange mark. remove it. Done.
ps. if you wondering what is the encoding of your file, go to sublime , show console and
type-> view.encoding()
2016年7月4日 星期一
[MacOS] install python3.4 pandas , request, matplotlib
pip3.4 install pandas
pip3.4 install request
pip3.4 install matplotlib
SUPER EASY.....
pip3.4 install request
pip3.4 install matplotlib
SUPER EASY.....
2016年6月14日 星期二
[python] auth with NTLM server & install python3.4 with requests_ntlm
Install requests_ntlm
1. need install request
2. install python-ntlm3
=>by download source code,
go to the folder you extracted files.
and use command
"sudo python3.4 setup.py install"
to install it.
3.install requests_ntlm
source code:https://github.com/requests/requests-ntlm
go to the folder you extracted files.
and the same, use command
"sudo python3.4 setup.py install"
Done.
----------------------------------------------------------------------------
from requests_ntlm import HttpNtlmAuth
rs_aps = requests.session()
url='http://xxxxx.xxxx.com/'
res = rs_aps.get(url, auth=HttpNtlmAuth('domain\\account', 'passwd'))
res.encoding='big5'
#print(res.text)
#get cookies
#cookie will change every time
for c in res.cookies:
ASP_Cookies_name=c.name
ASP_Cokies_value=c.value
print(ASP_Cookies_name, ASP_Cokies_value)
1. need install request
2. install python-ntlm3
=>by download source code,
go to the folder you extracted files.
and use command
"sudo python3.4 setup.py install"
to install it.
3.install requests_ntlm
source code:https://github.com/requests/requests-ntlm
go to the folder you extracted files.
and the same, use command
"sudo python3.4 setup.py install"
Done.
----------------------------------------------------------------------------
from requests_ntlm import HttpNtlmAuth
rs_aps = requests.session()
url='http://xxxxx.xxxx.com/'
res = rs_aps.get(url, auth=HttpNtlmAuth('domain\\account', 'passwd'))
res.encoding='big5'
#print(res.text)
#get cookies
#cookie will change every time
for c in res.cookies:
ASP_Cookies_name=c.name
ASP_Cokies_value=c.value
print(ASP_Cookies_name, ASP_Cokies_value)
標籤:
python,
web crawler
2016年5月9日 星期一
2016年4月29日 星期五
2016年4月21日 星期四
[python] debug log with logging config, pprint with logging, logging to file and still output at stdout
import pprint
import logging
from source.projectparameter import project_parameter as pp
def logconfig():
print_format="%(asctime)s,line:(%(lineno)d): %(message)s"
logging.basicConfig(filename="abspath/xxxx_debug.log",
filemode='a',
format=print_format,
datefmt='%H:%M:%S',
level=logging.DEBUG)
stderrLogger=logging.StreamHandler()
stderrLogger.setFormatter(logging.Formatter(print_format))
logging.getLogger().addHandler(stderrLogger)
return 0
def show_all_init_paramater():
for tag in pp:
#print('\n',tag,'with',len(pp[tag]),'items')
#logging.debug("\n",tag,"with",len(pp[tag]),"items")
logging.debug("%s with %s items",tag,len(pp[tag]))
logging.debug("\n"+pprint.pformat(pp[tag]))
return 0
def main():
logconfig()
logging.debug('=======NEW SECTION======')
show_all_init_paramater()
return
main()
import logging
from source.projectparameter import project_parameter as pp
def logconfig():
print_format="%(asctime)s,line:(%(lineno)d): %(message)s"
logging.basicConfig(filename="abspath/xxxx_debug.log",
filemode='a',
format=print_format,
datefmt='%H:%M:%S',
level=logging.DEBUG)
stderrLogger=logging.StreamHandler()
stderrLogger.setFormatter(logging.Formatter(print_format))
logging.getLogger().addHandler(stderrLogger)
return 0
def show_all_init_paramater():
for tag in pp:
#print('\n',tag,'with',len(pp[tag]),'items')
#logging.debug("\n",tag,"with",len(pp[tag]),"items")
logging.debug("%s with %s items",tag,len(pp[tag]))
logging.debug("\n"+pprint.pformat(pp[tag]))
return 0
def main():
logconfig()
logging.debug('=======NEW SECTION======')
show_all_init_paramater()
return
main()
2016年4月20日 星期三
[python] Ubuntu, python 3.x list files under folder , list modify time , create time of files. 列出資料夾下的檔案list, 和修改時間或其他屬性
import os,time
file_list=os.listdir("/home/xxxxxx/Desktop")
#print(file_list)
for file in file_list:
if '~' in file:
file_list.remove(file)
print(file_list)
for file in file_list:
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
print (file,"\nlast modified: %s" % time.ctime(mtime),'\nCreate time ',time.ctime(ctime),'\n')
file_list=os.listdir("/home/xxxxxx/Desktop")
#print(file_list)
for file in file_list:
if '~' in file:
file_list.remove(file)
print(file_list)
for file in file_list:
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
print (file,"\nlast modified: %s" % time.ctime(mtime),'\nCreate time ',time.ctime(ctime),'\n')
2016年4月18日 星期一
[python] python 3.x , date itreating from start date to stop(today) date.
from datetime import datetime,timedelta
def daterange(begin, end, delta = timedelta(1)):
"""Form a range of dates and iterate over them.
Arguments:
begin -- a date (or datetime) object; the beginning of the range.
end -- a date (or datetime) object; the end of the range.
delta -- (optional) a timedelta object; how much to step each iteration.
Default step is 1 day.
Usage:
"""
if not isinstance(delta, timedelta):
delta = timedelta(delta)
ZERO = timedelta(0)
if begin < end:
if delta <= ZERO:
raise StopIteration
test = end.__gt__
else:
if delta >= ZERO:
raise StopIteration
test = end.__lt__
while test(begin):
yield begin
begin += delta
def main():
start = datetime(2010, 1, 1,19,00,00)
stop = datetime.today()
for x in daterange(start,stop):
# only when today, it need to download after 7:00pm, so force the start date all is 19:00:00
if(stop > x):
#print(stop,x)
year=str(x.year)
month=str('%02d' % (x.month))
day=str('%02d' % (x.day))
download_xxxxx_from_web(year,month,day)
else:
print('For ',x,'Need to wait to 7:00')
return
main()
2016年4月12日 星期二
[python] python numpy static , sum, mean, std (axis = dimension)
a = np.array([[1, 2], [3, 4],[5, 6]])
print('-------------------')
print(np.sum(a))
print(a.sum())
print(a.sum(0))
print(np.sum(a,axis=0))
print(np.sum(a,axis=1))
print('-------------------')
print(np.mean(a))
print(a.mean())
print(a.mean(0))
print(np.mean(a, axis=0))
print(np.mean(a, axis=1))
print('-------------------')
print(np.std(a))
print(a.std())
print(a.std(1))
print(np.std(a, axis=0))
print(np.std(a, axis=1))
output:
-------------------
21
21
[ 9 12]
[ 9 12]
[ 3 7 11]
-------------------
3.5
3.5
[ 3. 4.]
[ 3. 4.]
[ 1.5 3.5 5.5]
-------------------
1.70782512766
1.70782512766
[ 0.5 0.5 0.5]
[ 1.63299316 1.63299316]
[ 0.5 0.5 0.5]
[Finished in 0.1s]
ps: axis 0 means do operator in each column individual. (first dimension.)
print(np.sum(a,axis=0))
[1,2]
[3,4]
[5,6]
=[1+3+5 , 2+4+6]= [9,12]
axis 1 means to operator second dimension.
print(np.sum(a,axis=1))
=[1+2,3+4,5+6]=[3,7,11]
---------------------
a = np.array([[1, 3,3, 4, 5, 6],[1, 3,3, 4, 5, 6]])
b = np.array([[1, 2,3, 4, 5, 6],[1, 1,1, 1, 1, 1]])
print(a)
print(np.mean(b, axis=0))
print(a-np.mean(b, axis=0))
[[1 3 3 4 5 6]
[1 3 3 4 5 6]]
[ 1. 1.5 2. 2.5 3. 3.5]
[[ 0. 1.5 1. 1.5 2. 2.5]
[ 0. 1.5 1. 1.5 2. 2.5]]
[Finished in 0.1s
2016年4月7日 星期四
[python] python 3.4 Ubuntu 14.04 install scikit-learn by building from source code.
0.Optional, If you needed....
1. download scikit-learn source code form
https://pypi.python.org/pypi/scikit-learn/0.15.2#downloads
The same as below, download the first one, scikit-learn-0.15.2.tar.gz
and extract it in your downloaded folder.
2. go to the extracted folder , and build it.
xxxxx/Downloads/scikit-learn-0.15.2$ python3.4 setup.py install --user
**if you get the error
x86_64-linux-gnu-gcc: error: unrecognized command line option ‘-fstack-protector-strong’
(Solution A:)
edit the below file by sudo
/usr/lib/python3.4/plat-x86_64-linux-gnu/_sysconfigdata_nd.py
=>this file is belone libpython3.4-minimal package, if you don't have it,
install that.
and replace "all" -fstack-protector-strong to -fstack-protector
and saved.
and use the build command above again....
$ python3.4 setup.py install --user
(Solution B:)
upgrade gcc to 4.9 (I don't want to do that.....)
sudo apt-get install build-essential python3-dev python3-setuptools \
python3-numpy python3-scipy \
libatlas-dev libatlas3gf-base
1. download scikit-learn source code form
https://pypi.python.org/pypi/scikit-learn/0.15.2#downloads
The same as below, download the first one, scikit-learn-0.15.2.tar.gz
and extract it in your downloaded folder.
2. go to the extracted folder , and build it.
xxxxx/Downloads/scikit-learn-0.15.2$ python3.4 setup.py install --user
**if you get the error
x86_64-linux-gnu-gcc: error: unrecognized command line option ‘-fstack-protector-strong’
(Solution A:)
edit the below file by sudo
/usr/lib/python3.4/plat-x86_64-linux-gnu/_sysconfigdata_nd.py
=>this file is belone libpython3.4-minimal package, if you don't have it,
install that.
and replace "all" -fstack-protector-strong to -fstack-protector
and saved.
and use the build command above again....
$ python3.4 setup.py install --user
(Solution B:)
upgrade gcc to 4.9 (I don't want to do that.....)
2016年3月29日 星期二
[python] python 3.4 install xlrd to handle excel .xls file.
$ sudo gedit /etc/apt/sources.list
add below
deb http://us.archive.ubuntu.com/ubuntu vivid main universe
save
then
$ sudo apt-get update
$sudo apt-get install python3-xlrd
----------------------------------------------------------------------
check the doc
https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html?p=4966
[python] python 3.x install pandas under Ubuntu 14.04
$ sudo apt-get install python3-pandas
detail: http://pandas.pydata.org/pandas-docs/stable/install.html
Installing using your Linux distribution’s package manager.
The commands in this table will install pandas for Python 2 from your distribution. To install pandas for Python 3 you may need to use the package python3-pandas.| Distribution | Status | Download / Repository Link | Install method |
|---|---|---|---|
| Debian | stable | official Debian repository | sudo apt-get install python-pandas |
| Debian & Ubuntu | unstable (latest packages) | NeuroDebian | sudo apt-get install python-pandas |
| Ubuntu | stable | official Ubuntu repository | sudo apt-get install python-pandas |
| Ubuntu | unstable (daily builds) | PythonXY PPA; activate by: sudo add-apt-repository ppa:pythonxy/pythonxy-devel && sudo apt-get update | sudo apt-get install python-pandas |
| OpenSuse & Fedora | stable | OpenSuse Repository | zypper in python-pandas |
[python] 計算日期 min , Max 和求 日期差距(含假日), check the date is what day of the week (or weekend)
from datetime import *
# create a date( ) list
datetime_list=[date(2016,6,30),date(2016,6,31).............]
print(min(datetime_list),max(datetime_list))
delta_days=max(datetime_list)-min(datetime_list)
print(delta_days.days)
delta_days=date(2016,3,28)-date(2016,3,21)
print(delta_days.days)
#------------- check the date is what day of the week-----------------------------------------
dt=date(2016,3,20)
if dt.isoweekday()==6 or dt.isoweekday()==7:
print('do someting')
detail: https://docs.python.org/2/library/datetime.html
# create a date( ) list
datetime_list=[date(2016,6,30),date(2016,6,31).............]
print(min(datetime_list),max(datetime_list))
delta_days=max(datetime_list)-min(datetime_list)
print(delta_days.days)
delta_days=date(2016,3,28)-date(2016,3,21)
print(delta_days.days)
#------------- check the date is what day of the week-----------------------------------------
dt=date(2016,3,20)
if dt.isoweekday()==6 or dt.isoweekday()==7:
print('do someting')
detail: https://docs.python.org/2/library/datetime.html
2016年3月27日 星期日
[python] python 用 matplotlib 畫出散點圖 scatter 筆記, 標記上點的text
matplotlib 提供能畫的種類
http://matplotlib.org/gallery.html
source code example included.
============================
import matplotlib.pyplot as plt
import numpy as np
#find max under list of list, sublist[x] means the target column
def max_value_lol(inputlist,target_column):
return max([sublist[target_column] for sublist in inputlist])
#畫出上下兩個圖根據不同data欄位, data is list of list , head_title is list for title
def data_Visualization(head_title,data):
#print(max_value((data)))
max_submit_times=max_value_lol(data,1)
point_scale=6
#print(len(data))
fig=plt.figure(figsize=(6,4))
ax=fig.add_subplot(211)
N = len(data)
for item in data :
x = float(item[6])
y = float(item[7])
colors = np.random.rand(N)
area = np.pi * (15 * (x+y)/2)**2
#add wording besides the points
ax.text(x, y, item[0])
#area = np.pi * (15 * (item[1]/max_submit_times)*point_scale)**2 # 0 to 15 point radiuses
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.xlabel(head_title[6])
plt.ylabel(head_title[7])
plt.xlim(-0.2,float (max_value_lol(data,6))+0.2)
plt.ylim(-0.2,float (max_value_lol(data,7))+0.2)
#plt.legend()
#second pic
ax=fig.add_subplot(212)
for item in data :
x = float(item[8])
y = float(item[9])
colors = np.random.rand(N)
area = np.pi * (15 * (float(item[8])+float(item[9]))/15)**2
#area = np.pi * (15 * (item[1]/max_submit_times)*point_scale)**2 # 0 to 15 point radiuses
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.xlabel(head_title[8])
plt.ylabel(head_title[9])
plt.xlim(-0.2,float (max_value_lol(data,8))+1)
plt.ylim(-0.2,float (max_value_lol(data,9))+5)
plt.show()
return
=====================
http://matplotlib.org/gallery.html
source code example included.
============================
import matplotlib.pyplot as plt
import numpy as np
#find max under list of list, sublist[x] means the target column
def max_value_lol(inputlist,target_column):
return max([sublist[target_column] for sublist in inputlist])
#畫出上下兩個圖根據不同data欄位, data is list of list , head_title is list for title
def data_Visualization(head_title,data):
#print(max_value((data)))
max_submit_times=max_value_lol(data,1)
point_scale=6
#print(len(data))
fig=plt.figure(figsize=(6,4))
ax=fig.add_subplot(211)
N = len(data)
for item in data :
x = float(item[6])
y = float(item[7])
colors = np.random.rand(N)
area = np.pi * (15 * (x+y)/2)**2
#add wording besides the points
ax.text(x, y, item[0])
#area = np.pi * (15 * (item[1]/max_submit_times)*point_scale)**2 # 0 to 15 point radiuses
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.xlabel(head_title[6])
plt.ylabel(head_title[7])
plt.xlim(-0.2,float (max_value_lol(data,6))+0.2)
plt.ylim(-0.2,float (max_value_lol(data,7))+0.2)
#plt.legend()
#second pic
ax=fig.add_subplot(212)
for item in data :
x = float(item[8])
y = float(item[9])
colors = np.random.rand(N)
area = np.pi * (15 * (float(item[8])+float(item[9]))/15)**2
#area = np.pi * (15 * (item[1]/max_submit_times)*point_scale)**2 # 0 to 15 point radiuses
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.xlabel(head_title[8])
plt.ylabel(head_title[9])
plt.xlim(-0.2,float (max_value_lol(data,8))+1)
plt.ylim(-0.2,float (max_value_lol(data,9))+5)
plt.show()
return
=====================
[python][data_Visualization] python 3.x install matplotlib under ubuntu 14.04
check matplotlib still here.
$ apt-cache search python3-matplotlib
If you find it like its available then you can install it from
$ sudo apt-get install python3-matplotlib
good to go
---------------
Check what you like.
http://matplotlib.org/gallery.html
---------------
Check what you like.
http://matplotlib.org/gallery.html
2016年3月25日 星期五
[python] python 3.4 抽象型別介紹, 實作範例
Part1:
int 是一個type (型別), list 也是一個型別(type), 而各自不同的type 有自己能使用的method, 方法就如同前幾篇印出來看的結果.
現在, Sequence 是一個型別(type), 當然自己也有自己能用的方法.
但是, list, int 或 str... 等等這種型別, 可以自己產生物件,
ex: a=[1,2,3] => 產生了一個list 物件
ex: t=(1,2,3) =>產生了tuple 物件
而 Sequence 無法產生一個實體物件 => 因此只能算是一個抽象的型別, 概念性的, 不能真的拿來用的型別.
那你會問說, 不能用的Sequence 是要作甚呢?他就像一張賽車的設計藍圖
在Sequence的藍圖中, 畫出了賽車車輪的位置和大小, 畫出了座位的樣子等等等
但是並沒有實際的寫上細節, 沒有真正要用哪些廠牌的資訊, 因為這些細節和實際
製作的工法, 就留給那到藍圖的下游廠商去作就好, 所以換言之, 下游廠商如果接了這個
案子, 就要照著Sequence 的設計藍圖製造, 但是細節由自己決定, 可是不能有少, 否則就
沒有照這個設計圖走了.
所以, 那最源頭的抽象型別是誰呢?我們可以在 Collections Abstract Base Classes,
(https://docs.python.org/3/library/collections.abc.html)
抽象型別的集合中, 看到所有python 定義的抽象型別列表, 如下 (放在collections.abc)
舉例來看, 在ABC這欄, 有個Sized, 也就是說, python 想營造出一個抽象的東西,
希望能夠處裡有關大小的事情, 所以我的藍圖裡想要有這個概念,
而他右邊那攔 inherits from 是沒有填, 代表Sized 這個概念是原創, 他是第一個頭,
在右邊那攔有個abstracts methods, 他填的是 __len__, 就是說, 希望後人在處裡
有關Sized 的概念時, 請將實作的function 做在__len__裡面, 所以之後, 只要想要找有關
大小的訊息時, 就去你的__len__找, 才不會大家function 名稱不一樣, 搞的亂七八糟的.
另外一個 ABC攔中的 Container 也是類似的, 就是他有個概念是, 我想要找出甚東西
包含在其中, 希望後人們能提供這個方法, 因為還蠻常用的,而你們後人要實作的話
請作在__contains__ 裡面.
那現在看Sequencs , 他繼承了 Sized, Container, Iterable, 這三個人, 所以一定會有
這三個人當初要求要作的方法,但他自己又加入了__getitem__ __len__,這兩個抽象方法
意思是說, 我序列這種類別, 將會提供sized, container, iterable, 和 getitem, len 的方法
如果你要繼承我, 之後你得做出這些東西, 而你看到 __len__不是Sized 裡面就有嗎?
怎又出現在Sequences 的 抽象方法中呢?那是因為 他仍然沒有實作, 繼續擺著, 希望後面的人能作, 而最右邊 Mixin Method, 告訴你, 我還加進了這些方法(已經做好你可以用).
而MutableSequence , 又繼承了Sequence 但列出了一些抽象型別, 等著給別人實作
__getitem__, __setitem__, __delitem__, ...等, 而可用的方法除了繼承了 Sequence
已經有的, 又加入了 append, reversed ,....等等等
這個MutableSequence 抽象型別, 也就是常聽到的[可變序列], 而list , str 都在他的下面,
所以 也就是說, list 得實作這一連串的方法, 加上list 自己想提供的方法, 而 tuple ,
因為是設計來用為不可改變的物件, 當然就不會去繼承 MutableSequence了, 但他仍然
是個序列相關的物件, 所以可見得, tuple 一定是在sequence 之下, 但實際上,
中間還有一層tuple 物件, 而 真正我們在用的實體tuple 是tuple 物件的實作.
you can use it to print the abstract class tree.
def classtree(cls):
print(cls.__bases__)
c = list(cls.__bases__)
for base in c:
c.extend(classtree(base))
return c
classtree(MutableSequence)
output:
(<class 'collections.abc.Sequence'>,)
(<class 'collections.abc.Sized'>, <class 'collections.abc.Iterable'>, <class 'collections.abc.Container'>)
(<class 'object'>,) #最上層了
..... #not import so I do not pasted it.
ps. 可以利用 isinstance 看是不是誰的實作, 還有issubclass 看是不是誰的子類別
li=[1,2]
t=(3,4)
print ("---check instance---")
print(isinstance(li,list), isinstance(t,tuple))
print(isinstance(li,Sequence), isinstance(t,Sequence))
print(isinstance(li,MutableSequence), isinstance(t,MutableSequence))
print ("---check list and class level---")
print(type(li),type(t))
print(issubclass(list,MutableSequence),issubclass(MutableSequence,list))
print(issubclass(MutableSequence,Sequence),issubclass(Sequence,MutableSequence))
print(issubclass(Sequence,Sized))
print ("---check tuple and class level---")
print(type(li),type(t))
print(issubclass(tuple,MutableSequence),issubclass(MutableSequence,tuple))
print(issubclass(tuple,Sequence),issubclass(Sequence,tuple))
print(issubclass(Sequence,Sized))
output:
---check instance---
True True
True True
True False
---check list and class level---
<class 'list'> <class 'tuple'>
True False
True False
True
---check tuple and class level---
<class 'list'> <class 'tuple'>
False False
True False
True
[Finished in 0.0s]
Part2 :
假設你要實作一個Set 的實體instance, 就只要實作 __contains__, __iter__,__len__
(如表格所示), 而其他Mixin Method 都是python 已經幫我們作好的, 直接可以用了.
所以下面的code, 要實作這三個 (init 本來自己就要), 而也看到, and 直接可以用了.
from collections.abc import *
int 是一個type (型別), list 也是一個型別(type), 而各自不同的type 有自己能使用的method, 方法就如同前幾篇印出來看的結果.
現在, Sequence 是一個型別(type), 當然自己也有自己能用的方法.
但是, list, int 或 str... 等等這種型別, 可以自己產生物件,
ex: a=[1,2,3] => 產生了一個list 物件
ex: t=(1,2,3) =>產生了tuple 物件
而 Sequence 無法產生一個實體物件 => 因此只能算是一個抽象的型別, 概念性的, 不能真的拿來用的型別.
那你會問說, 不能用的Sequence 是要作甚呢?他就像一張賽車的設計藍圖
在Sequence的藍圖中, 畫出了賽車車輪的位置和大小, 畫出了座位的樣子等等等
但是並沒有實際的寫上細節, 沒有真正要用哪些廠牌的資訊, 因為這些細節和實際
製作的工法, 就留給那到藍圖的下游廠商去作就好, 所以換言之, 下游廠商如果接了這個
案子, 就要照著Sequence 的設計藍圖製造, 但是細節由自己決定, 可是不能有少, 否則就
沒有照這個設計圖走了.
所以, 那最源頭的抽象型別是誰呢?我們可以在 Collections Abstract Base Classes,
(https://docs.python.org/3/library/collections.abc.html)
抽象型別的集合中, 看到所有python 定義的抽象型別列表, 如下 (放在collections.abc)
| ABC | Inherits from | Abstract Methods | Mixin Methods |
|---|---|---|---|
Container |
__contains__ |
||
Hashable |
__hash__ |
||
Iterable |
__iter__ |
||
Iterator |
Iterable |
__next__ |
__iter__ |
Generator |
Iterator |
send, throw |
close, __iter__, __next__ |
Sized |
__len__ |
||
Callable |
__call__ |
||
Sequence |
Sized,
Iterable,
Container |
__getitem__,
__len__ |
__contains__, __iter__, __reversed__,
index, and count |
MutableSequence |
Sequence |
__getitem__,
__setitem__,
__delitem__,
__len__,
insert |
Inherited Sequence methods and
append, reverse, extend, pop,
remove, and __iadd__ |
Set |
Sized,
Iterable,
Container |
__contains__,
__iter__,
__len__ |
__le__, __lt__, __eq__, __ne__,
__gt__, __ge__, __and__, __or__,
__sub__, __xor__, and isdisjoint |
MutableSet |
Set |
__contains__,
__iter__,
__len__,
add,
discard |
Inherited Set methods and
clear, pop, remove, __ior__,
__iand__, __ixor__, and __isub__ |
Mapping |
Sized,
Iterable,
Container |
__getitem__,
__iter__,
__len__ |
__contains__, keys, items, values,
get, __eq__, and __ne__ |
MutableMapping |
Mapping |
__getitem__,
__setitem__,
__delitem__,
__iter__,
__len__ |
Inherited Mapping methods and
pop, popitem, clear, update,
and setdefault |
MappingView |
Sized |
__len__ |
|
ItemsView |
MappingView,
Set |
__contains__,
__iter__ |
|
KeysView |
MappingView,
Set |
__contains__,
__iter__ |
|
ValuesView |
MappingView |
__contains__, __iter__ |
|
Awaitable |
__await__ |
||
Coroutine |
Awaitable |
send, throw |
close |
AsyncIterable |
__aiter__ |
||
AsyncIterator |
AsyncIterable |
__anext__ |
__aiter__ |
舉例來看, 在ABC這欄, 有個Sized, 也就是說, python 想營造出一個抽象的東西,
希望能夠處裡有關大小的事情, 所以我的藍圖裡想要有這個概念,
而他右邊那攔 inherits from 是沒有填, 代表Sized 這個概念是原創, 他是第一個頭,
在右邊那攔有個abstracts methods, 他填的是 __len__, 就是說, 希望後人在處裡
有關Sized 的概念時, 請將實作的function 做在__len__裡面, 所以之後, 只要想要找有關
大小的訊息時, 就去你的__len__找, 才不會大家function 名稱不一樣, 搞的亂七八糟的.
另外一個 ABC攔中的 Container 也是類似的, 就是他有個概念是, 我想要找出甚東西
包含在其中, 希望後人們能提供這個方法, 因為還蠻常用的,而你們後人要實作的話
請作在__contains__ 裡面.
那現在看Sequencs , 他繼承了 Sized, Container, Iterable, 這三個人, 所以一定會有
這三個人當初要求要作的方法,但他自己又加入了__getitem__ __len__,這兩個抽象方法
意思是說, 我序列這種類別, 將會提供sized, container, iterable, 和 getitem, len 的方法
如果你要繼承我, 之後你得做出這些東西, 而你看到 __len__不是Sized 裡面就有嗎?
怎又出現在Sequences 的 抽象方法中呢?那是因為 他仍然沒有實作, 繼續擺著, 希望後面的人能作, 而最右邊 Mixin Method, 告訴你, 我還加進了這些方法(已經做好你可以用).
而MutableSequence , 又繼承了Sequence 但列出了一些抽象型別, 等著給別人實作
__getitem__, __setitem__, __delitem__, ...等, 而可用的方法除了繼承了 Sequence
已經有的, 又加入了 append, reversed ,....等等等
這個MutableSequence 抽象型別, 也就是常聽到的[可變序列], 而list , str 都在他的下面,
所以 也就是說, list 得實作這一連串的方法, 加上list 自己想提供的方法, 而 tuple ,
因為是設計來用為不可改變的物件, 當然就不會去繼承 MutableSequence了, 但他仍然
是個序列相關的物件, 所以可見得, tuple 一定是在sequence 之下, 但實際上,
中間還有一層tuple 物件, 而 真正我們在用的實體tuple 是tuple 物件的實作.
you can use it to print the abstract class tree.
def classtree(cls):
print(cls.__bases__)
c = list(cls.__bases__)
for base in c:
c.extend(classtree(base))
return c
classtree(MutableSequence)
output:
(<class 'collections.abc.Sequence'>,)
(<class 'collections.abc.Sized'>, <class 'collections.abc.Iterable'>, <class 'collections.abc.Container'>)
(<class 'object'>,) #最上層了
..... #not import so I do not pasted it.
ps. 可以利用 isinstance 看是不是誰的實作, 還有issubclass 看是不是誰的子類別
li=[1,2]
t=(3,4)
print ("---check instance---")
print(isinstance(li,list), isinstance(t,tuple))
print(isinstance(li,Sequence), isinstance(t,Sequence))
print(isinstance(li,MutableSequence), isinstance(t,MutableSequence))
print ("---check list and class level---")
print(type(li),type(t))
print(issubclass(list,MutableSequence),issubclass(MutableSequence,list))
print(issubclass(MutableSequence,Sequence),issubclass(Sequence,MutableSequence))
print(issubclass(Sequence,Sized))
print ("---check tuple and class level---")
print(type(li),type(t))
print(issubclass(tuple,MutableSequence),issubclass(MutableSequence,tuple))
print(issubclass(tuple,Sequence),issubclass(Sequence,tuple))
print(issubclass(Sequence,Sized))
output:
---check instance---
True True
True True
True False
---check list and class level---
<class 'list'> <class 'tuple'>
True False
True False
True
---check tuple and class level---
<class 'list'> <class 'tuple'>
False False
True False
True
[Finished in 0.0s]
Part2 :
假設你要實作一個Set 的實體instance, 就只要實作 __contains__, __iter__,__len__
(如表格所示), 而其他Mixin Method 都是python 已經幫我們作好的, 直接可以用了.
所以下面的code, 要實作這三個 (init 本來自己就要), 而也看到, and 直接可以用了.
from collections.abc import *
li=[9,1,2]
class ListBasedSet(Set):
''' Alternate set implementation favoring space over speed
and not requiring the set elements to be hashable. '''
def __init__(self, iterable):
self.elements = lst = []
for value in iterable:
if value not in lst:
lst.append(value)
def __iter__(self):
return iter(self.elements)
def __contains__(self, value):
return value in self.elements
def __len__(self):
return len(self.elements) #你在後面+ 100 , s1 印出來值也會變, 可見得真的是跑你作的這邊
s1 = len(ListBasedSet(li)) #拿你實作的來用用看
print(s1)
for x in ListBasedSet(li):
print(x)
#你發現 & 不用實作, 直接可以用,因為 __and__在Mixin Method 裡面已經註明他做好了
for x in (ListBasedSet('abcdef') & ListBasedSet('defwxyz')):
print(x)
output:
3
9
1
2
d e f [Finished in 0.1s]
2016年3月23日 星期三
[python] python 3.x 新手的一些小觀念 (tuple, 三元運算子,型別物件 type object )
(0) #是一個整數 0 的 int 物件
(0,)#是一個有一個元素是0的tuple物件
--------------------------------
三元運算子(x if y > 0 else z)=>如果 y 對, 走x, y錯 走z
x,y,z=3,-1,6
print(x if y>0 else z)
x,y,z=3,6,6
print(x if y>0 else z)
6
3
[Finished in 0.1s]
--------------------------------------------------
type is an object also
型別也是一個物件(所以可以assign給別人), 叫做物件型別
ex:
a=3 #a is assigned to int object, value=3
print (type(a)) #the type is int
=><class 'int'>
# type(a) return a type, and type also is an object , so it can assign to atype.
# so we still can check the type of atype.
#which means we will see the return type of type(a)
atype=type(a)
print(type(atype))
=><class 'type'>
# and it shows <class 'type'>, which means we got a object named type.
[python] python 3.4 print out all method inside object
list
ex:
a=[1,2]
print (dir(a))
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
[Finished in 0.1s]
------------------------
int
a=3
print (dir(a))
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
[Finished in 0.1s]
---------------------
dict
a={3,}
print (dir(a))
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
[Finished in 0.1s]
print(dir(str))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
[Finished in 0.1s]
****
you can find all type of object has __str__ method, that why print() can
print out all kind of object. (print() can only print string type.)
a=3
print(a) # in fact is => print(a.__str__())
all __class__ which be used equal to type()
=> type(a) = a.__class__
ex:
a=[1,2]
print (dir(a))
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
[Finished in 0.1s]
------------------------
int
a=3
print (dir(a))
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
[Finished in 0.1s]
---------------------
dict
a={3,}
print (dir(a))
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
[Finished in 0.1s]
print(dir(str))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
[Finished in 0.1s]
****
you can find all type of object has __str__ method, that why print() can
print out all kind of object. (print() can only print string type.)
a=3
print(a) # in fact is => print(a.__str__())
all __class__ which be used equal to type()
=> type(a) = a.__class__
訂閱:
文章 (Atom)