welcom ! Handel home

2017年1月17日 星期二

Linux CentOS7 64bit install docker Note


$sudo yum update

When Centos 6 ,5  befer then Centos7 version install
$sudo yum install -y docker-io

Centos7 is ready install
$sudo yum install -y docker

# set docker power on auto startup server
$ sudo systemctl enable docker.service
# start run docker
$ sudo systemctl start docker

# test docker run
$ sudo docker run --rm hello-world




2017年1月8日 星期日

Docker命令查詢

Docker 包括三個基本概念
+

  • 映像檔(Image)
  • 容器(Container)
  • 倉庫(Repository)

Docker命令查詢

基本語法

docker [OPTIONS] COMMAND [arg...]
一般來說,Docker 命令可以用來管理 daemon,或者透過 CLI 命令管理映像檔和容器。可以透過 man docker 來查看這些命令。

選項

-D=true|false
    使用 debug 模式。預設為 false。

-H, --host=[unix:///var/run/docker.sock]: tcp://[host:port]來綁定或者 unix://[/path/to/socket] 來使用。
    在 daemon 模式下綁定的 socket,透過一個或多個 tcp://host:port, unix:///path/to/socket, fd://* or fd://socketfd 來指定。

--api-enable-cors=true|false
    在遠端 API 中啟用 CORS 頭。預設為 false。

-b=""
    將容器掛載到一個已存在的橋接器上。指定為 'none' 時則禁用容器的網路。

--bip=""
    讓動態建立的 docker0 採用指定的 CIDR 位址; 與 -b 選項互斥。

-d=true|false
    使用 daemon 模式。預設為 false。

--dns=""
    讓 Docker 使用指定的 DNS 伺服器。

-g=""
    指定 Docker 執行時的 root 路徑。預設為 /var/lib/docker。

--icc=true|false
    啟用容器間通信。預設為 true。

--ip=""
    綁定連接埠時候的預設 IP 位址。預設為 0.0.0.0。

--iptables=true|false
    禁止 Docker 新增 iptables 規則。預設為 true。

--mtu=VALUE
    指定容器網路的 mtu。預設為 1500。

-p=""
    指定 daemon 的 PID 檔案路徑。預設為 /var/run/docker.pid。

-s=""
    強制 Docker 執行時使用指定的儲存驅動。

-v=true|false
    輸出版本資訊並退出。預設值為 false。

--selinux-enabled=true|false
    啟用 SELinux 支援。預設值為 false。SELinux 目前不支援 BTRFS 儲存驅動。

命令

Docker 的命令可以採用 docker-CMD 或者 docker CMD 的方式執行。兩者一致。
docker-attach(1)
    依附到一個正在執行的容器中。

docker-build(1)
    從一個 Dockerfile 建立一個映像檔

docker-commit(1)
    從一個容器的修改中建立一個新的映像檔

docker-cp(1)
    從容器中複製檔案到宿主系統中

docker-diff(1)
    檢查一個容器檔案系統的修改

docker-events(1)
    從服務端取得實時的事件

docker-export(1)
    匯出容器內容為一個 tar 包

docker-history(1)
    顯示一個映像檔的歷史

docker-images(1)
    列出存在的映像檔

docker-import(1)
    匯入一個檔案(典型為 tar 包)路徑或目錄來建立一個映像檔

docker-info(1)
    顯示一些相關的系統資訊

docker-inspect(1)
    顯示一個容器的底層具體資訊。

docker-kill(1)
    關閉一個執行中的容器 (包括程式和所有資源)

docker-load(1)
    從一個 tar 包中載入一個映像檔

docker-login(1)
    註冊或登錄到一個 Docker 的倉庫伺服器

docker-logout(1)
    從 Docker 的倉庫伺服器登出

docker-logs(1)
    取得容器的 log 資訊

docker-pause(1)
    暫停一個容器中的所有程式

docker-port(1)
    查找一個 nat 到一個私有網口的公共口

docker-ps(1)
    列出容器

docker-pull(1)
    從一個Docker的倉庫伺服器下拉一個映像檔或倉庫

docker-push(1)
    將一個映像檔或者倉庫推送到一個 Docker 的註冊伺服器

docker-restart(1)
    重新啟動一個執行中的容器

docker-rm(1)
    刪除指定的數個容器

docker-rmi(1)
    刪除指定的數個映像檔

docker-run(1)
    建立一個新容器,並在其中執行指定命令

docker-save(1)
    保存一個映像檔為 tar 包檔案

docker-search(1)
    在 Docker index 中搜索一個映像檔

docker-start(1)
    啟動一個容器

docker-stop(1)
    終止一個執行中的容器

docker-tag(1)
    為一個映像檔打標籤

docker-top(1)
    查看一個容器中的正在執行的程式資訊

docker-unpause(1)
    將一個容器內所有的程式從暫停狀態中恢復

docker-version(1)
    輸出 Docker 的版本資訊

docker-wait(1)
    阻塞直到一個容器終止,然後輸出它的退出符

一張圖總結 Docker 的命令

命令周期

Docker 簡介

Docker by LXC(Linux Containers)
Docker 理解為一種 Applice or Server 套件的 Sandbox , 每一個 Container 都安裝執行一種應用 , Contain 間可以建立溝通 , Containers 啟動 停止都很快 對本機資源要求不高 遠低於 VM ,實際應用實 直接把Container 當成應用軟體執行也可以!
這樣帶來以下優勢
>>快速部屬和派送:
>>更高效率資源應用
>>用容易搬遷和擴展
>>用易更新


 在 LXC 的基礎上 Docker 進行了進一步的封裝,讓使用者不需要去關心容器的管理,使得操作更為簡便。使用者操作 Docker 的容器就像操作一個快速輕量級的虛擬機一樣簡單。
下面的圖片比較了 Docker 和傳統虛擬化方式的不同之處,可見容器是在作業系統層面上實作虛擬化,直接使用本地主機的作業系統,而傳統方式則是在硬體層面實作。
傳統虛擬化
Docker

2016年7月24日 星期日

python sudoku plan # [sudoku_tablex1.py]


=======================================================
 [sudoku_tablex1.py]    --題庫(demo_1)Easy ,(demo_2)Normal,(demo_3)Hard

#============================
demo_1=[                  
    [[7,0,1,0,2,0,0,0,0],#e001  
     [0,0,0,0,5,3,9,0,1],
     [9,3,5,1,7,6,0,2,4],
     [1,6,0,9,8,0,3,0,0],
     [3,2,9,0,4,0,5,6,8],
     [0,0,4,0,3,2,0,9,7],
     [4,9,0,3,1,7,2,8,5],
     [5,0,8,2,9,0,0,0,0],
     [0,0,0,0,6,0,4,0,9] ],
         
    [[0,0,5,0,9,0,7,0,4],#e002  
     [0,7,4,3,0,1,6,0,0],
     [0,8,0,7,0,4,5,0,2],
     [7,4,0,9,3,0,2,0,1],
     [9,5,2,0,0,0,4,6,3],
     [3,0,1,0,4,2,0,7,8],
     [4,0,3,8,0,5,0,2,0],
     [0,0,7,6,0,9,3,4,0],
     [5,0,6,0,1,0,8,0,0] ],                                
         
    [[6,0,0,2,9,0,0,0,1],#e003  
     [3,0,0,0,0,6,9,0,0],
     [9,1,0,0,3,0,4,0,0],
     [0,7,0,9,0,0,6,0,0],
     [0,2,0,7,0,3,0,4,0],
     [0,0,9,0,0,2,0,5,0],
     [0,0,8,0,5,0,0,9,4],
     [0,0,5,6,0,0,0,0,8],
     [1,0,0,0,8,7,0,0,5] ],
         
    [[0,4,0,0,0,0,7,0,1],#e004  
     [0,0,8,0,0,2,3,0,0],
     [0,6,1,9,4,0,0,0,0],
     [0,0,0,5,0,0,2,3,0],
     [8,0,0,0,9,0,0,0,4],
     [0,2,5,0,0,1,0,0,0],
     [0,0,0,0,5,7,9,6,0],
     [0,0,4,3,0,0,1,0,0],
     [6,0,9,0,0,0,0,5,0] ],
         
    [[1,0,5,0,0,0,0,0,0],#e005  
     [0,0,0,2,0,0,0,6,8],
     [0,0,8,0,3,0,0,4,0],
     [2,5,0,0,8,1,0,0,0],
     [4,0,6,0,0,0,8,0,2],
     [0,0,0,6,4,0,0,5,3],
     [0,7,0,0,5,0,1,0,0],
     [6,1,0,0,0,7,0,0,0],
     [0,0,0,0,0,0,2,0,9] ],
       
    [[0,3,0,0,0,0,1,0,0],#e006  
     [0,0,0,4,2,0,0,3,0],
     [8,0,9,0,0,7,0,0,0],
     [6,8,0,0,0,0,3,4,0],
     [9,0,0,6,8,5,0,0,2],
     [0,1,7,0,0,0,0,8,5],
     [0,9,0,2,0,0,5,0,4],
     [0,0,0,0,3,4,0,0,0],
     [0,0,8,0,0,0,0,6,0] ],
         
    [[0,4,0,0,2,0,9,0,7],#e007  
     [0,3,0,0,5,0,0,0,0],
     [9,0,0,3,0,0,1,0,0],
     [8,7,0,4,0,0,0,0,0],
     [0,0,4,7,0,8,6,0,0],
     [0,0,0,0,0,5,0,8,4],
     [0,0,1,0,0,6,0,0,9],
     [0,0,0,0,3,0,0,6,0],
     [7,0,6,0,4,0,0,1,0] ],                              
         
    [[0,2,0,0,6,0,4,0,1],#e008  
     [5,0,8,0,0,3,0,0,0],
     [0,3,0,2,0,0,0,0,9],
     [0,0,0,0,4,0,8,9,0],
     [8,0,0,7,5,2,0,0,4],
     [0,6,4,0,3,0,0,0,0],
     [1,0,0,0,0,7,0,5,0],
     [0,0,0,9,0,0,7,0,3],
     [4,0,5,0,8,0,0,1,0] ],
         
    [[2,0,0,6,0,7,0,0,8],#e009  
     [4,9,0,0,0,0,3,7,0],
     [8,0,0,0,4,0,5,0,0],
     [9,1,0,2,0,0,0,0,0],
     [0,0,3,4,0,1,6,0,0],
     [0,0,0,0,0,3,0,8,4],
     [0,0,9,0,6,0,0,0,5],
     [0,6,5,0,0,0,0,4,7],
     [7,0,0,3,0,2,0,0,6] ],
         
    [[0,0,0,0,0,0,0,0,0],#e010  
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0] ]

    ]
#============================
demo_2=[        
    [[0,8,0,0,0,0,0,3,4],#n001  
     [0,0,1,0,9,3,0,0,0],
     [0,3,0,0,0,0,1,5,0],
     [4,0,0,2,0,0,5,0,0],
     [1,0,0,7,0,4,0,0,2],
     [0,0,8,0,0,5,0,0,6],
     [0,1,4,0,0,0,0,9,0],
     [0,0,0,3,4,0,6,0,0],
     [5,2,0,0,0,0,0,4,0] ],
         
    [[3,8,0,1,0,0,0,0,0],#n002  
     [0,0,1,0,0,0,6,0,4],
     [0,4,0,9,3,0,0,0,0],
     [7,0,0,0,0,0,0,6,1],
     [0,2,0,5,0,4,0,9,0],
     [8,5,0,0,0,0,0,0,7],
     [0,0,0,0,6,3,0,2,0],
     [5,0,7,0,0,0,3,0,0],
     [0,0,0,0,0,9,0,1,6] ],                                
         
    [[0,0,0,7,0,0,2,8,0],#n003  
     [0,2,0,0,0,6,5,0,0],
     [5,7,0,0,0,0,0,0,4],
     [3,0,0,0,8,4,0,1,0],
     [7,0,0,0,0,0,0,0,3],
     [0,4,0,3,2,0,0,0,8],
     [8,0,0,0,0,0,0,4,1],
     [0,0,9,8,0,0,0,7,0],
     [0,3,4,0,0,9,0,0,0] ],
         
    [[0,4,6,0,0,7,0,0,2],#n004  
     [0,0,0,0,0,9,5,0,4],
     [8,7,0,0,3,0,0,0,0],
     [0,5,2,7,0,0,0,0,0],
     [0,0,8,0,6,0,9,0,0],
     [0,0,0,0,0,8,2,6,0],
     [0,0,0,0,2,0,0,5,1],
     [3,0,4,1,0,0,0,0,0],
     [5,0,0,9,0,0,6,4,0] ],
         
    [[0,1,3,0,0,5,0,0,0],#n005  
     [0,0,2,0,0,3,0,9,0],
     [9,0,0,0,0,0,4,0,8],
     [0,3,0,1,0,6,0,0,2],
     [5,0,0,0,3,0,0,0,9],
     [7,0,0,2,0,9,0,6,0],
     [1,0,4,0,0,0,0,0,5],
     [0,9,0,5,0,0,6,0,0],
     [0,0,0,8,0,0,1,2,0] ],
       
    [[0,6,8,0,3,0,0,0,0],#n006  
     [0,0,0,0,0,0,9,0,1],
     [0,0,0,7,0,6,3,0,2],
     [0,0,0,0,4,7,0,1,0],
     [5,4,0,0,0,0,0,9,6],
     [0,3,0,2,6,0,0,0,0],
     [8,0,6,5,0,4,0,0,0],
     [9,0,3,0,0,0,0,0,0],
     [0,0,0,0,9,0,1,5,0] ],
         
    [[9,2,0,3,0,8,0,0,0],#n007  
     [0,0,0,0,6,0,0,0,8],
     [0,8,3,7,0,0,0,0,0],
     [6,0,1,0,0,0,5,0,0],
     [0,4,0,5,0,3,0,9,0],
     [0,0,7,0,0,0,8,0,3],
     [0,0,0,0,0,6,2,5,0],
     [4,0,0,0,8,0,0,0,0],
     [0,0,0,4,0,1,0,8,6] ],                                    
         
    [[0,0,1,0,0,0,3,4,0],#n008  
     [0,4,0,0,8,0,0,0,9],
     [5,0,0,3,0,7,0,0,0],
     [4,0,6,2,0,0,0,0,0],
     [0,9,0,5,0,6,0,7,0],
     [0,0,0,0,0,1,6,0,5],
     [0,0,0,7,0,5,0,0,8],
     [2,0,0,0,1,0,0,9,0],
     [0,8,3,0,0,0,7,0,0] ],
         
    [[2,0,1,8,0,0,0,6,0],#n009  
     [0,0,0,9,0,0,0,2,4],
     [4,0,0,0,1,0,0,0,0],
     [0,0,2,1,5,0,9,0,0],
     [0,8,0,0,3,0,0,1,0],
     [0,0,9,0,6,7,5,0,0],
     [0,0,0,0,9,0,0,0,7],
     [9,3,0,0,0,6,0,0,0],
     [0,5,0,0,0,1,8,0,9] ],
         
    [[5,0,0,9,0,0,0,3,0],#n010  
     [0,0,0,0,7,4,6,0,0],
     [1,0,6,0,0,0,0,4,0],
     [0,0,0,0,0,1,3,7,0],
     [0,0,8,6,9,7,1,0,0],
     [0,7,1,3,0,0,0,0,0],
     [0,9,0,0,0,0,5,0,8],
     [0,0,4,8,1,0,0,0,0],
     [0,1,0,0,0,2,0,0,9] ]
    ]    
#============================
demo_3=[        
    [[0,0,8,0,0,0,0,5,1],#h001  
     [0,6,0,0,0,5,7,0,0],
     [0,0,1,0,8,0,0,0,0],
     [0,2,0,0,0,0,0,0,6],
     [5,0,0,7,0,1,0,0,4],
     [3,0,0,0,0,0,0,2,0],
     [0,0,0,0,5,0,9,0,0],
     [0,0,3,8,0,0,0,1,0],
     [4,9,0,0,0,0,8,0,0] ],
         
    [[0,0,4,8,0,0,0,1,0],#h002  
     [0,0,7,0,9,0,0,0,4],
     [0,6,0,0,3,0,2,0,0],
     [8,0,0,0,0,1,0,0,0],
     [0,0,0,3,0,2,0,0,0],
     [0,0,0,7,0,0,0,0,1],
     [0,0,2,0,4,0,0,5,0],
     [4,0,0,0,2,0,6,0,0],
     [0,9,0,0,0,7,4,0,0] ],                                
         
    [[0,0,8,0,3,0,0,6,0],#h003  
     [5,0,0,1,0,0,0,0,0],
     [0,0,0,0,0,9,1,7,0],
     [0,0,0,0,0,5,9,4,0],
     [2,0,0,0,0,0,0,0,8],
     [0,3,5,4,0,0,0,0,0],
     [0,2,6,7,0,0,0,0,0],
     [0,0,0,0,0,8,0,0,4],
     [0,5,0,0,4,0,7,0,0] ],
         
    [[0,0,1,0,0,0,0,0,8],#h004  
     [8,0,0,5,0,1,0,0,0],
     [0,5,0,0,3,0,1,0,0],
     [0,1,0,0,0,6,0,4,0],
     [0,0,6,0,0,0,7,0,0],
     [0,9,0,3,0,0,0,2,0],
     [0,0,5,0,7,0,0,3,0],
     [0,0,0,2,0,8,0,0,5],
     [4,0,0,0,0,0,8,0,0] ],
         
    [[1,0,0,0,7,0,0,9,0],#h005  
     [0,5,3,0,0,0,0,0,4],
     [2,0,0,0,0,3,0,0,0],
     [0,2,0,0,0,5,8,0,0],
     [0,0,0,6,0,8,0,0,0],
     [0,0,5,1,0,0,0,2,0],
     [0,0,0,2,0,0,0,0,5],
     [4,0,0,0,0,0,3,6,0],
     [0,8,0,0,9,0,0,0,7] ],
       
    [[0,0,5,0,0,9,0,2,0],#h006  
     [0,7,0,0,5,0,0,0,3],
     [4,0,0,0,1,0,0,0,0],
     [0,4,0,6,0,0,0,0,0],
     [0,0,6,9,0,7,2,0,0],
     [0,0,0,0,0,3,0,7,0],
     [0,0,0,0,6,0,0,0,8],
     [6,0,0,0,3,0,0,1,0],
     [0,5,0,8,0,0,6,0,0] ],
         
    [[0,0,0,0,0,3,9,0,8],#h007  
     [0,0,1,0,0,9,0,0,2],
     [2,3,0,0,4,0,0,0,0],
     [0,7,0,0,0,6,0,0,0],
     [0,0,4,0,0,0,5,0,0],
     [0,0,0,4,0,0,0,3,0],
     [0,0,0,0,7,0,0,8,9],
     [4,0,0,3,0,0,7,0,0],
     [7,0,6,5,0,0,0,0,0] ],                                
         
    [[2,0,0,9,6,0,0,1,0],#h008  
     [0,3,1,0,0,0,0,0,0],
     [0,0,0,0,3,0,0,0,5],
     [0,5,0,0,0,0,6,0,0],
     [0,0,9,3,0,7,2,0,0],
     [0,0,3,0,0,0,0,4,0],
     [3,0,0,0,9,0,0,0,0],
     [0,0,0,0,0,0,8,2,0],
     [0,8,0,0,4,1,0,0,7] ],
         
    [[8,0,0,0,0,7,0,0,9],#h009  
     [4,0,6,0,2,0,0,0,0],
     [0,5,0,3,0,0,2,0,0],
     [0,0,7,2,0,0,0,0,0],
     [0,6,0,0,0,0,0,9,0],
     [0,0,0,0,0,1,3,0,0],
     [0,0,8,0,0,6,0,4,0],
     [0,0,0,0,5,0,1,0,8],
     [6,0,0,8,0,0,0,0,5] ],
         
    [[0,0,0,0,6,0,0,3,0],#h010  
     [0,4,5,0,0,9,0,0,8],
     [0,0,0,0,0,1,0,0,6],
     [0,0,2,5,0,0,0,8,0],
     [0,0,9,0,0,0,6,0,0],
     [0,6,0,0,0,7,1,0,0],
     [4,0,0,1,0,0,0,0,0],
     [9,0,0,6,0,0,3,4,0],
     [0,1,0,0,7,0,0,0,0] ]
    ]
       

=======================================================

python sudoku plan [sudoku_publib.py]

=======================================================
 [sudoku_publib.py] --class Num and Class sudoku_data array
# coding=utf-8
#=== pyt268104 home work ===
# 20160627
#import math
#import numpy as np
#v1.1
class Num:
    arealoop=[[1,3,1,3],[1,3,4,6],[1,3,7,9],
              [4,6,1,3],[4,6,4,6],[4,6,7,9],
              [7,9,1,3],[7,9,4,6],[7,9,7,9]            
              ]
    areacode=0
    chkno=[]
    no=0
    pos=00
    status = 0 #0:user edit ,1:is mask can no edit
    chkfg=0
   
    def __init__(self, pos, no, status):
        self.pos=pos
        self.no=no
        self.status = status
        self.chkfg=0
        self.chkno=[1,2,3,4,5,6,7,8,9]
       
    def setpos(self,pos):
        px=eval(str(pos)[1])
        py=eval(str(pos)[0])
        if 1<= px <=9:
            if 1<= py <=9:
                #self.pos = pos
                if 1<=py<=3:
                    if 1<=px<=3:
                        self.areacode=1
                    elif 4<=px<=6:
                        self.areacode=2
                    elif 7<=px<=9:
                        self.areacode=3
                       
                elif 4<=py<=6:
                    if 1<=px<=3:
                        self.areacode=4
                    elif 4<=px<=6:
                        self.areacode=5
                    elif 7<=px<=9:
                        self.areacode=6
                   
                elif 7<=py<=9:
                    if 1<=px<=3:
                        self.areacode=7
                    elif 4<=px<=6:
                        self.areacode=8
                    elif 7<=px<=9:
                        self.areacode=9

    def maskno(self,no):
        if self.status==1:#mask no
            self.no=no
       
    def setno(self,no):
        if self.status==0:#user edit no
            self.no = no

    def scanchk(self,xx):
        self.chkfg=0
        fg1=0
        fg2=0
        fg3=0
        fg1=self.area_scanchk(xx)
        fg2=self.col_scanchk(xx)
        fg3=self.row_scanchk(xx)
        self.chkfg|=fg1 or fg2 or fg3      
        pass
   
    def area_scanchk(self,xx):
        fg=0
        #py=int(self.pos/10)
        #px=self.pos%10
        self.setpos(self.pos)
        xacode=self.areacode
        y1,y2,x1,x2 = self.arealoop[self.areacode-1]
        y,x = (y1-1),(x1-1)      
        for y in range(y1-1,y2):
            for x in range(x1-1,x2):
                tt=(y+1)*10+x+1
                if tt!=self.pos:
                    nn = xx[y][x].no
                    if nn in self.chkno:
                        self.chkno.remove(nn)
                       
                    if nn!=0:
                        if nn==self.no:
                            fg=1# fthe game check fail
        return fg
        pass      

    def col_scanchk(self,xx):#col y 列
        fg=0
        #px=eval(str(pos)[1])#row
        py=eval(str(self.pos)[0])#col
        y=py-1
        for x in range(1-1,9):
            tt=(y+1)*10+x+1
            if tt!=self.pos:              
                nn = xx[y][x].no
                if nn in self.chkno:
                    self.chkno.remove(nn)
                if nn!=0:
                    if nn==self.no:
                        fg=1# fthe game check fail
                       
        return fg          
        pass

    def row_scanchk(self,xx):#row x 行
        fg=0
        px=eval(str(self.pos)[1])#row
        #py=eval(str(pos)[0])#col
        x=px-1
        #print("x=",x)
        for y in range(1-1,9):
            #print("y=",y)
            tt=(y+1)*10+x+1
            if tt!=self.pos:        
                nn = xx[y][x].no          
                if nn in self.chkno:
                    self.chkno.remove(nn)
                if nn!=0:
                    if nn==self.no:
                        fg=1# fthe game check fail      
        return fg
        pass

class sudoku_dataarray:
    gname=""
    sudokudat = []
    chkfg=0
    runfg=0
    def __init__(self,name):
        self.gname = name
        self.sudokudat = [[0 for i in range(9)] for j in range(9)]
        for i in range(9):
            for j in range(9):
                pos = (i+1)*10+(j+1)
                self.sudokudat[i][j] = Num(pos,0,0)              
        self.chkfg=0
        self.runfg=0
        pass

    def showsudoku(self):            
        #self.chkfg=0
        #print()
        print("="*15,"sudoku data","="*15)
        for i in range(9):
            for j in range(9):
               
                if self.sudokudat[i][j].status==1:
                    print("($%1s)"%(self.sudokudat[i][j].no),end="")#mask
                else:  
                    print("[%2s]"%(self.sudokudat[i][j].no),end="")#free edit
                    #self.sudokudat[i][j].scanchk(self.sudokudat)
                    #self.chkfg+=self.sudokudat[i][j].chkfg

                if j in (2,5):
                    print("||",end="")
            print()
            if i in (2,5):
                print(("="*12)+"||"+("="*12)+"||"+("="*12))
       
        print("="*42)
        pass
   
    def checksudoku(self):            
        self.chkfg=0
        self.runfg=0
        for i in range(9):
            for j in range(9):
                if self.sudokudat[i][j].status==0:
                    self.sudokudat[i][j].scanchk(self.sudokudat)
                    self.chkfg|=self.sudokudat[i][j].chkfg
                    if self.sudokudat[i][j].no==0:
                        self.runfg+=1
                   
        #return self.chkfg

    def setsudoku_col(self,nn,ll):
        tt=0
        for j in range(9):
            self.sudokudat[nn-1][j].no=ll[tt]
            if ll[tt]!=0:
                self.sudokudat[nn-1][j].status=1 # mask data
            else:
                self.sudokudat[nn-1][j].status=0 # user data
            tt+=1      
        pass

    def setsudoku_plan(self,tab):
        tt=0
        for i in range(9):
            for j in range(9):
                self.sudokudat[i][j].no = tab[i][j]
                nn =  tab[i][j]
                if self.sudokudat[i][j].no!=0:
                    self.sudokudat[i][j].status=1 # mask data
                else:
                    self.sudokudat[i][j].status=0 # user data
                stt=self.sudokudat[i][j].status
                self.sudokudat[i][j].chkno=[1,2,3,4,5,6,7,8,9]
                pp = (i+1)*10+(j+1)
                self.sudokudat[i][j].setpos(pp)

#xx = [[0 for i in range(9)] for j in range(9)]  
#split the pos by col and row  
#k1=eval(str(s)[0])
#k2=eval(str(s)[1])

=======================================================

python sudoku plan # [ pyt268104_sudoku.py] --main code

由三個檔案組成
=======================================================[ pyt268104_sudoku.py] --主程式
# coding=utf-8
#=== pyt268104 home work ===
# 20160627
#import math
#import numpy as np

#def fun(a1,a2):
#   c = np.dot(a1,a2)  
#   return c
from class_hw import sudoku_tablex1
from class_hw import sudoku_publib

level = 1
gcode = 1
pysudokuver = '''
This is Sudoku Game
Desiger: Handel Liao pyt268104
Codeing by Python3.5
Ver : 1.2 20160725
'''
lev=["Easy","Normal","Hard"]
mtxt = '''
=== Easy Sudoku Game V1.2 pyt268104 Handel ===
*** System Command ***
[1]Level Select (L1:Easy,L2:Normal,L3:Hard)
[2]Game code by LEVEL(1..10)
[3]Show Game Buffer and Status
[4]select play Game
[?]List Menu
[H]Help info show
[Q]quit
*** Game Command ***
[N]Add a New Game
[D]Delete a Game
[L]Load Game Data[from level,gcode]
[P]Print Game data array
[K]key in a data to Game
[C]check the Game by Pass
'''

def getsudoku():
    xx = [[0 for i in range(9)] for j in range(9)]
    return xx

def showsudoku(xx):
    print()
    print("="*15,"sudoku data","="*15)
    for i in range(9):
        for j in range(9):
            print("[%2s]"%(xx[i][j]),end="")
            if j in (2,5):
                print("||",end="")
        print()
        if i in (2,5):
            print(("="*12)+"||"+("="*12)+"||"+("="*12))

def setsudoku_area(xx,nn,ll):
    tt=0
    for j in range(9):
        xx[nn-1][j]=ll[tt]
        tt+=1
     
def setsudoku_plan(xx,tab):
    tt=0
    for i in range(9):
        xx[i]=tab[i]

def scanok(yy):#c1: area ,col,raw must be 1 scan
    for i in range(9):
        for j in range(9):
            if yy.sudokudat[i][j].status == 0:
                yy.sudokudat[i][j].scanchk(yy.sudokudat)
         
    runok=0
    runfail=0
    for i in range(9):
        for j in range(9):
            ll=len(yy.sudokudat[i][j].chkno)
            print("%d%d = %s # %d"%(i+1,j+1,yy.sudokudat[i][j].chkno,ll))
            if yy.sudokudat[i][j].no==0:
                if ll==1:              
                    yy.sudokudat[i][j].no = yy.sudokudat[i][j].chkno[0]
                else:
                    runok+=1

                if ll==0:
                    runfail+=1
             
    return runok,runfail  

def scanok2(yy):#c2: area mabe only 1 scan
    pass

def main():
    game=[]
    level = 1
    gcode = 1
    ss="demo"+",("+lev[level-1]+"_"+str(gcode)+")"
    game.append(sudoku_publib.sudoku_dataarray(ss))
    game[0].setsudoku_plan(sudoku_tablex1.demo_1[gcode])
 
    level = 2
    gcode = 1
    ss="demo1"+",("+lev[level-1]+"_"+str(gcode)+")"
    game.append(sudoku_publib.sudoku_dataarray(ss))
    game[1].setsudoku_plan(sudoku_tablex1.demo_2[gcode])
 
    level = 3
    gcode = 1
    ss="demo2"+",("+lev[level-1]+"_"+str(gcode)+")"
    game.append(sudoku_publib.sudoku_dataarray(ss))
    game[2].setsudoku_plan(sudoku_tablex1.demo_3[gcode])

    level = 1
    gcode = 1
    playindex = 0  
    cmm ='?'
    while 1:
        if cmm =='?':
            print(mtxt)          
        elif cmm =='h':
            print(pysudokuver)
        elif cmm == 'q':
            print()
            print("************************")
            print("** Thank You By By    **")
            print("************************")
            print()
            break
        elif cmm == 'n':#add a new Game
            ss = input("input the gmae name:")
            ss+=",("+lev[level-1]+"_"+str(gcode)+")"
            game.append(sudoku_publib.sudoku_dataarray(ss))
            tt=0
            for i in game:
                print("[%d]: %s "%(tt,i.gname))
                tt+=1
            print("Add ok !")
            print()
         
        elif cmm == 'd': #delete
            print("==================")
            tt=0
            for i in game:
                print("[%d]: %s "%(tt,i.gname))
                tt+=1
            nn = eval(input("input index by Delete:"))
            if nn>len(game)-1:
                print("Sorry ! index out of range ")
                print()
            else:
                del game[nn]
                tt=0
                for i in game:
                    print("[%d]: %s "%(tt,i.gname))
                    tt+=1
                if playindex>=len(game):
                    playindex=len(game)-1
                print("delete ok !")
                print()
        elif cmm == 'l':#load data to array
            if level==1:
                game[playindex].setsudoku_plan(sudoku_tablex1.demo_1[gcode])
            elif level==2:
                game[playindex].setsudoku_plan(sudoku_tablex1.demo_2[gcode])
            elif level==3:
                game[playindex].setsudoku_plan(sudoku_tablex1.demo_3[gcode])
             
            ss1 = game[playindex].gname
            ss = ss1.split(",")[0]
            ss += ",("+lev[level-1]+"_"+str(gcode)+")"
            game[playindex].gname = ss
            print("===%d is [ %s ] ==="%(playindex,game[playindex].gname))
            game[playindex].showsudoku()              
             
        elif cmm == "p":#show data array
            print()
            print("===%d is [ %s ] ==="%(playindex,game[playindex].gname))
            game[playindex].showsudoku()
         
        elif cmm == "k":#keyin data to array
            cl=2
            while 1:
                xin= input("input game pos(11..99),Num:")
                ll=xin.split(",")
                if len(ll)==2:
                    pos=eval(xin.split(",")[0])
                    nn =eval(xin.split(",")[1])
                    i = int(pos/10)
                    j = pos%10
                    game[playindex].sudokudat[i-1][j-1].setno(nn)
                    game[playindex].showsudoku()
                else:
                    print("Err input stop keyin ...")
                    break        

         
        elif cmm =='1':
            level = eval(input("(1:Easy,2:Normal,3:Hard)Input Level:"))
            if 0                print("Level:%d , Gcode:%d "%(level,gcode))
            else:
                print("Error input level Out of range")
             
        elif cmm =='2':
            gcode = eval(input("input gcode:"))
            if 0                print("Level:%d , Gcode:%d "%(level,gcode))
            else:
                print("Error input gcode Out of range")
             
        elif cmm =='4':
            tt=0
            for i in game:
                print("[%d]: %s "%(tt,i.gname))
                tt+=1
            xcm = input("input playindex:")
            if xcm.isdigit():
                nn = eval(xcm)
            else:
                print("Error input must is Number ... ")
                nn = playindex
             
            if nn < len(game):
                playindex = nn
                print("You select game [ %s ]"%(game[playindex].gname))
            else:
                print("Error input index Out of range")
               
        elif cmm =='3':
            print()
            print("====Game buffer show list ==========")
            print("Level:%d , Gcode:%d "%(level,gcode))
            tt=0
            for i in game:
                print("[%d]: %s "%(tt,i.gname))
                tt+=1
           
            print("Game index = %d is run [ %s ]"%(playindex,game[playindex].gname))
            print()
                 
        elif cmm == "c":#check game pass or fail
            print()
            print("===check the %d is [ %s ] ==="%(playindex,game[playindex].gname))
            game[playindex].showsudoku()
            game[playindex].checksudoku()
         
            if game[playindex].runfg>0:
                print("Some Number must to edit  ...")
            else:
                if game[playindex].chkfg==0:
                    print("************************")
                    print("** This Game Pass ... **")
                    print("************************")
                else:
                    print("This Game Fail !")  
                    print("Try Again ...")
                 
        elif cmm == 'c1':
            runck,runfail = scanok(game[playindex])
            print("have %d will be edit %d is err ... "%(runck,runfail))

        elif cmm == 'c2':
            pass
         
        cmm = input("Play (game[%d]-> %s) help(?)Input cmm="%(playindex,game[playindex].gname))

     
if __name__ == '__main__':
   main()


=======================================================

python sudoku plan

主題: 數獨
1說明:
專題由三個檔案組成
pyt268104_sudoku.py --主程式
sudoku_publib.py --class Num and Class sudoku_dataarray
sudoku_tablex1.py   --題庫(demo_1)Easy ,(demo_2)Normal,(demo_3)Hard

需置於同一目錄下

2執行啟動:
python3 pyt268104_sudoku.py
啟動後 預置 demo,demo1,demo2 3個遊戲庫資料(keyin 3 whill list)      

3操作:
=== Easy Sudoku Game V1.0 pyt268104 Handel ===
*** System Command ***
[1]Level Select (L1:Easy,L2:Normal,L3:Hard) : 設定 Level
[2]Game code by LEVEL(1..10)   : 設定題目序號
[3]Show Game Buffer and Status : 顯示目前開啟的遊戲數急狀態名稱
[4]select play Game : 選擇緩衝區中的數獨題目
[?]List Menu : 顯示指令提示
[H]Help info show : 顯示 版本紀錄
[Q]quit : 結束離開
*** Game Command ***
[N]Add a New Game : 增加 遊戲庫資料
[D]Delete a Game : 刪除 遊戲庫資料
[L]Load Game Data[from level,gcode] : 於選定的遊戲庫資料載入指定的題目
[P]Print Game data array : 顯示遊戲庫的資料現況
[K]key in a data to Game : 輸入 (pos,Num)
[C]check the Game by Pass : 檢查遊戲狀態 (playing , Fail , Pass)

範例:
===0 is [ demo,(Easy_1) ] === ##顯示 (buffer index)  [名稱,(等級_題目序號)]
======= sudoku data =========
[ 0][ 0]($9)||[ 0]($4)[ 0]||($1)($7)[ 0]   ## ($ Num) 為題目不可更改
[ 0][ 0]($1)||[ 0]($2)[ 0]||[ 0]($3)[ 0]   ## [  Num] 為user 寫入欄位
[ 0]($4)($6)||[ 0][ 0]($5)||($8)[ 0][ 0]
========||========||========
($6)[ 0]($3)||($7)[ 0][ 0]||[ 0][ 0][ 0]
($8)($2)[ 0]||[ 0][ 0][ 0]||[ 0][ 0]($4)
[ 0][ 0][ 0]||($5)($8)[ 0]||($6)[ 0]($3)
========||=======||=========
($5)($7)[ 0]||[ 0][ 0]($8)||($9)[ 0][ 0]
[ 0]($1)[ 0]||($2)[ 0][ 0]||[ 0]($8)($5)
($3)[ 0][ 0]||[ 0]($5)($1)||[ 0][ 0][ 0]
=========================

2016年5月18日 星期三

Python OO copy and deepcopy 9x9 Pad by SUDO test code

import copy

class suitem:
    def __init__(self):
        dd = [1,2,3,4,5,6,7,8,9]
        ddx = []
        self.set = ['x']*9*9
#        self.data = copy.deepcopy(dd)*9*9
        for i in range(9):
            for j in range(9):
                ddx.append(copy.deepcopy(dd))
        self.data = ddx

    def showset(self):
        for i in range(9):
            print ">>[",
            for j in range(9):
                print self.set[i*9+j],
            print "]<<"

    def showdata(self):
        for i in range(9):
            print ">>[",
            for j in range(9):
                print self.data[i*9+j],
            print "]<<"
           
if __name__ == '__main__':
    sett = suitem()


    a = copy.copy(sett)
    b = copy.deepcopy(sett)

   
    sett.set[0] ='1'
    print "=== other ==="
    sett.showset()
    print "=== other ==="
    a.showset()
    print "=== other ==="
    b.showset()
    print "=== end   ==="
   

2016年5月15日 星期日

python 2.7 and 3.3 install easy_install , pip

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel


1
2
3
4
5
6
7
8
9
10
11
12
13
# Python 2.7.6:
tar xf Python-2.7.6.tar.xz
cd Python-2.7.6
./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall

# Python 3.3.5:
tar xf Python-3.3.5.tar.xz
cd Python-3.3.5
./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall


# Then install it for Python 2.7 and/or Python 3.3:
python2.7 ez_setup.py
python3.3 ez_setup.py

# Now install pip using the newly installed setuptools:
easy_install-2.7 pip
easy_install-3.3 pip

# With pip installed you can now do things like this:
pip2.7 install [packagename]
pip2.7 install --upgrade [packagename]
pip2.7 uninstall [packagename]





2016年5月11日 星期三

python read the direct [dir ] [file] list

[Python] 取得路徑下檔案
'ss = os.listdir(" ")      
'if  os.path.isfile(ss[0]) :
'if  os.path.isdir(ss[0]):

如何利用python取得特定路徑下的檔案名稱呢? 

其實說起來也相當簡單,os module裡已經定義好了。

先從os.listdir本身會回傳該資料夾下的所有檔案名稱,以list回傳。

再用一個 for 迴圈遍訪每個檔名,對其用os.path.isfile()判斷即可(若要取資料夾使用os.path.isdir())。

以下為範例程式碼。


import os
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
        print f
上面是個簡潔有力的寫法,他的效果等義於下列程式碼:

import os
files = []

for f in os.listdir('.'):
        if os.path.isfile(f):
                files.append(f)
for f in files:
        print f