Business

程式設計

分享各種程式語言重點, 設計模式, 資料結構, 演算法...等,相關技術.

好用工具

介紹各種好用工具,免費資源.

生活雜記

分享各種生活記事.

雲端教室

各式IT教學影片.

專案

.

2020年7月21日

pipenv與virtualenv 比較

這裡記錄一下,使用pip/virtualenv和pipenv 不同操作所需的指令比較。




新建虛擬環境

virtualenv pipenv
 $ virtualenv venv 
 $ pipenv install 

virtualenv 註解: 這裡的venv是虛擬環境目錄的名稱,可以自由更改,這會在你的專案根目錄創建一個venv資料夾,其中包含獨立的Python解譯器環境。

pipenv註解: 而pipenv會自動為建立虛擬環境,並且隨機產生一個的虛擬環境的目錄名稱。




啟動虛擬環境

virtualenv pipenv
[Windows]:
 venv\Scripts\activate 
 $ pipenv shell 



安裝package到虛擬環境

virtualenv pipenv
Step1: 
 $ . venv/bin/activate #先啟動虛擬環境
Step2:
 (venv)$ pip install <package-name> 
 $ pipenv install <packageName> 

當你使用pipenv時,不管是否啟動了虛擬環境,都可以透過pipenv install命令安裝套件。如果目前的目錄不包含虛擬環境,pipenv會自動創建一個。




記錄requirements

virtualenv pipenv
 (venv)$ pip freeze > requirements.txt 
 N/A 

virtualenv 註解: 這個指令會把依賴列表寫入requirements.txt文件。每當你安裝或卸載了依賴包時,都需要手動更新這個檔。

pipenv註解: 使用pipenv時,什麼都不必做。它會自動建立pipfile 和pipfile.lock檔(如果不存在),並且在你使用pipenv install/uninstall指令,安裝和卸載套件時,自動更新pipfile 和pipfile.lock檔。pipfile用來記錄專案相關套件清單,而pipfile.lock記錄了固定版本的詳細相關套件列表。




在部署環境安裝依賴

當我們需要在一個新的環境,比如部署上線環境時。virtualenv需要重複上面的指令。

virtualenv pipenv
Step1:
 $ virtualenv venv
Step2:
 $ . venv/bin/activate 
Step3:
 (venv)$ pip install -r requirements.txt
 pipenv install 



區分開發版本的相關依賴

virtualenv pipenv
 -r requirements.txt 
 pytest=1.2.3
$ pipenv install pytest --dev 

virtualenv 註解: 使用requirements.txt時,我們會另外建立一個requirements-dev.txt文件來手動加入開發的依賴。比如專案開發時才會用到pytest,那麼你需要手動創建這個檔,然後寫入。

pipenv註解: 使用pipenv時,你只需要在安裝pytest時添加一個–dev選項,它會自動被分類為開發依賴(寫入pipfile的dev-packages一節中)。




在新的開發環境安裝依賴時,你可以安裝指定檔案中的依賴

virtualenv pipenv
 (venv) $ pip install -r requirements-dev.txt 
$ pipenv install --dev 

2020年6月29日

Windows 10 Python pip install 安裝Microsoft Visual C++ Build Tools

安裝Microsoft Visual C++ 14.0

在Win10 使用pip install 安裝套件時,如果出現以下錯誤訊息,表示要先安裝Visual C++ 14.0 及 .NET 。

error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https:// visualstudio.microsoft.com/ downloads/

這裡的Microsoft Visual C++ 14.0指的是Build Tools for Visual Studio 2015。



安裝步驟:

【Step 1】

直接到 https://visualstudio.microsoft.com/zh-hant/visual-cpp-build-tools/ 下載vs_buildtools
  

【Step 2】

接著,執行vs_buildtools,安裝時,選如下圖所示的C++建置工具。安装檔案約4.85G。


cmd

【Step 3】

安裝完成後,再執行pip install 安裝套件,就能安裝成功,不會再顯示需要Visual C++ 14.0。

2020年6月16日

Windows 10 內建螢幕錄影功能:用遊戲列錄製軟體



今天要介紹 Windows 10 內建的螢幕錄影功能,不用另外安裝錄影軟體,就能用 Windows 10 內建的「遊戲列」功能,進行各種軟體操作錄影。


不過「遊戲列」錄影有個限制,一次只能錄單一軟體,或是 單一遊戲的畫面,如果跳出或切換軟體就會停止錄影。並且不能錄製 Windows 桌面或系統本身的操作,一定要錄「軟體」(例如瀏覽器等軟體)。



開始使用Win10內建的螢幕錄影功能。



【步驟一】

按下鍵盤快捷鍵 Win + G


【步驟二】

接著,你會看到如下圖顯示的 Windows 10 遊戲列功能。


【步驟三】

點擊下圖所示,紅色框圈選的錄影按鈕『開始錄影』,按下後,就開始錄製這個軟體的操作畫面,也可開啟[麥克風錄音]來錄下你的旁白。


cmd


【步驟四】

如下圖所示可以看到Windows已經開始在錄製畫面了,預設的計時器會在螢幕右上方。計時器右邊兩個按鈕分別是停止錄影,以及開啟麥克風。


cmd


**【小提醒】:在錄製電腦畫面的過程中,請勿調整當前錄製影片視窗的視窗大小及縮小視窗,因為Windows的螢幕錄製程式會強制停止繼續錄影的動作。


【步驟五】

如果需要停止影片的錄製,那可以點擊如上圖所示藍色小方框的『停止錄影』按鈕,就可以讓結束錄影了。錄製好的影片會存放在使用者資料夾『影片』的『擷取』資料夾中,你可以在這個資料夾找到剛剛錄製好的影片。


cmd


Windows 10 內建的[遊戲列],用 Win + G 就可以呼叫,原本是給遊戲錄影專用的工具,但用來錄製軟體操作教學也很方便,有需要的朋友可以試試看。

2020年4月30日

Python 處理Excel 表格

一、xlrd

作用:方便讀取多頁工作表的 XLS 檔。

安裝:

 pip install xlrd

處理Excel表格

import xlrd
#讀取 excel檔
xls_file= xlrd.open_workbook('demo.xlsx')
sheets = book.sheets()   

#列出每一個工作表,並且印出工作表的名字
for sheet in sheets: 
    print(sheet.name) 

2020年4月1日

在 Windows 中查詢主機板的型號

使用命令提示字元

【步驟一】、Win + R 輸入「cmd」

cmd

【步驟二】、在「命令提示字元」裡面輸入

 wmic baseboard get product,Manufacturer,version,serialnumber

baseboard

其它相關command:

查詢CPU資訊

 wmic CPU get Name,NumberOfCores,NumberOfLogicalProcessors

查詢 [記憶體] 資訊

 wmic MemoryChip get BankLabel, Capacity, MemoryType, TypeDetail, Speed

查詢 [硬碟型號] 資訊

 wmic DISKDRIVE get Caption

2020年2月2日

ZeroMQ


ZeroMQ

ZeroMQ 簡稱 ZMQ,是一套網路通訊函式庫(socket library),可以處理訊息佇列庫,而且可以在多個執行緒、核心和主機之間彈性伸縮。它使得 Socket 程式設計更加簡單、簡潔和效能更高。詳細內容請 參考


ZeroMQ,有三種基本樣式。

樣式一:請求-回應樣式(Request-Reply)


這是一個最基本的Server/Client的Socket通訊樣式。


【Server 端程式 - Python版本】

#
#   Hello World server in Python
#   Binds REP socket to tcp://*:5555
#   Expects b"Hello" from client, replies with b"World"


import time
import zmq

context = zmq.Context()

socket = context.socket(zmq.REP)

socket.bind("tcp://*:5555")

while True:

    #  Wait for next request from client

    message = socket.recv()

    print("Received request: %s" % message)

    #  Do some 'work'

    time.sleep(1)

    #  Send reply back to client

    socket.send(b"World")


【Client端程式 - Python版本】

#
#   Hello World client in Python
#   Connects REQ socket to tcp://localhost:5555
#   Sends "Hello" to server, expects "World" back
#

import zmq

context = zmq.Context()

#  Socket to talk to server
print("Connecting to hello world server…")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")

#  Do 10 requests, waiting each time for a response
for request in range(10):
    print("Sending request %s …" % request)
    socket.send(b"Hello")

    #  Get the reply.
    message = socket.recv()
    print("Received reply %s [ %s ]" % (request, message))

樣式二:Publish-Subscribe樣式:


廣播所有client,沒有佇列快取,離線後,資料將永遠丟失。client可以進行資料過濾。



【Server 端程式 - Python版本】


import zmq
from random import randrange

context = zmq.Context()
socket = context.socket(zmq.PUB)  #publisher類型
socket.bind("tcp://*:5556")

while True:
    zipcode = randrange(1, 100000)
    temperature = randrange(-80, 135)
    relhumidity = randrange(10, 60)

    socket.send_string("%i %i %i" % (zipcode, temperature, relhumidity))
  


【Client端程式 - Python版本】

import sys
import zmq

#  Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)

print("Collecting updates from weather server…")
socket.connect("tcp://localhost:5556")

# Subscribe to zipcode, default is NYC, 10001
zip_filter = sys.argv[1] if len(sys.argv) > 1 else "10001"

# Python 2 - ascii bytes to unicode str
if isinstance(zip_filter, bytes):
    zip_filter = zip_filter.decode('ascii')
socket.setsockopt_string(zmq.SUBSCRIBE, zip_filter)

# Process 5 updates
total_temp = 0
for update_nbr in range(5):
    string = socket.recv_string()
    zipcode, temperature, relhumidity = string.split()
    total_temp += int(temperature)

print("Average temperature for zipcode '%s' was %dF" % (
      zip_filter, total_temp / update_nbr)
) 

樣式三:Pipeline樣式:



 【原理】:有一個Publisher來發佈任務,這些任務是可以平行執行的。有一批Worker用於接收任務,Worker處理完任務之後就將結果發送到Sink之中用於歸總或進一步處理。 所以要特別注意,Pipeline並不是伺服器和用戶端(Server/Client )的關係,而是有三種物件—Ventilator,Worker,Sink。當網路斷線時,資料不會丟失,重連後,資料繼續傳送到Sink。



 【Ventilator端程式 - Python版本】



# Task ventilator
# Binds PUSH socket to tcp://localhost:5557
# Sends batch of tasks to workers via that socket
#
# Author: Lev Givon 


import zmq
import random
import time


try:
    raw_input
except NameError:
    # Python 3
    raw_input = input

context = zmq.Context()

# Socket to send messages on
sender = context.socket(zmq.PUSH)
sender.bind("tcp://*:5557")

# Socket with direct access to the sink: used to syncronize start of batch
sink = context.socket(zmq.PUSH)
sink.connect("tcp://localhost:5558")

print("Press Enter when the workers are ready: ")
_ = raw_input()
print("Sending tasks to workers…")

# The first message is "0" and signals start of batch
sink.send(b'0')

# Initialize random number generator
random.seed()

# Send 100 tasks
total_msec = 0
for task_nbr in range(100):

    # Random workload from 1 to 100 msecs
    workload = random.randint(1, 100)
    total_msec += workload

    sender.send_string(u'%i' % workload)

print("Total expected cost: %s msec" % total_msec)

# Give 0MQ time to deliver
time.sleep(1)




【Worker端程式 - Python版本】

# Task worker
# Connects PULL socket to tcp://localhost:5557
# Collects workloads from ventilator via that socket
# Connects PUSH socket to tcp://localhost:5558
# Sends results to sink via that socket
#
# Author: Lev Givon 

import sys
import time
import zmq

context = zmq.Context()

# Socket to receive messages on
receiver = context.socket(zmq.PULL)
receiver.connect("tcp://localhost:5557")

# Socket to send messages to
sender = context.socket(zmq.PUSH)
sender.connect("tcp://localhost:5558")

# Process tasks forever
while True:
    s = receiver.recv()

    # Simple progress indicator for the viewer
    sys.stdout.write('.')
    sys.stdout.flush()

    # Do the work
    time.sleep(int(s)*0.001)

    # Send results to sink
    sender.send(b'')


【Sink端程式 - Python版本】



# Task sink
# Binds PULL socket to tcp://localhost:5558
# Collects results from workers via that socket
#
# Author: Lev Givon 

import sys
import time
import zmq

context = zmq.Context()

# Socket to receive messages on
receiver = context.socket(zmq.PULL)
receiver.bind("tcp://*:5558")

# Wait for start of batch
s = receiver.recv()

# Start our clock now
tstart = time.time()

# Process 100 confirmations
for task_nbr in range(100):
    s = receiver.recv()
    if task_nbr % 10 == 0:
        sys.stdout.write(':')
    else:
        sys.stdout.write('.')
    sys.stdout.flush()

# Calculate and report duration of batch
tend = time.time()
print("Total elapsed time: %d msec" % ((tend-tstart)*1000))




2020年1月11日

陣列 Array


定義



Array,陣列,數組。 陣列是用來儲存一群『相同資料型態』的元素 (element),並且具有順序性的資料列。通常佔用連續的記憶體空間。

宣告


如果要引用陣列的某個位置或元素,必須指定陣列名稱,及陣列中的位置編號。
例如,建立一個長度為 5、元素型別為 int 的陣列 arr:


int arr[5];

時間複雜度


時間複雜度為O(1)