def checkPID(pid):
if(len(pid) != 13): # ถ้า pid ไม่ใช่ 13 ให้คืนค่า False
return False
num=0 # ค่าสำหรับอ้างอิง index list ข้อมูลบัตรประชาชน
num2=13 # ค่าประจำหลัก
listdata=list(pid) # list ข้อมูลบัตรประชาชน
sum=0 # ผลลัพธ์
while num<12:
sum+=int(listdata[num])*(num2-num) # นำค่า num เป็น index list แต่ละตัว * (num2 - num) แล้วรวมเข้ากับ sum
num+=1 # เพิ่มค่า num อีก 1
digit13 = sum%11 # sum หาร 11 เอาเศษ
if digit13==0: # ถ้าเศษ = 0
digit13=1 # ค่าหลักที่ 13 คือ 1
elif digit13==1: # ถ้าเศษ = 1
digit13=0 # ค่าหลักที่ 13 คือ 0
else:
digit13=11-digit13 # ถ้าเศษไม่ใช่กับอะไร ให้เอา 11 - digit13
if digit13==int(listdata[12]): # ถ้าค่าหลักที่ 13 เท่ากับค่าหลักที่ 13 ที่ป้อนข้อมูลมา คืนค่า True
return True
else: # ถ้าค่าหลักที่ 13 ไม่เท่ากับค่าหลักที่ 13 ที่ป้อนข้อมูลมา คืนค่า False
return False
checkPID("เลขบัตรประชาชน")
ดึงข้อมูลจากวิกิพีเดีย
import wikipedia
wikipedia.set_lang("th")
us = wikipedia.summary("สหรัฐอเมริกา", sentences=1)
print(us)
Output
สหรัฐอเมริกา (อังกฤษ: United States of America) หรือมักย่อว่า สหรัฐฯ หรือ อเมริกา
เป็นสหพันธรัฐประชาธิปไตย ปกครองภายใต้รัฐธรรมนูญ ประกอบด้วยรัฐ 50 รัฐ และหนึ่งเขตปกครองกลาง
มี 48 รัฐและกรุงวอชิงตัน ดี.ซี.
msinfo32 /nfo C:\com_info.nfo
msinfo32 /report C:\com_info.txt
เขียนโปรแกรม Bluetooth
ในภาษา Python มีโมดูลสำหรับใช้เขียนโปรแกรมควบคุม Bluetooth คือ โมดูล PyBluez
pip install PyBluez
รับจำนวนและแสดง MAC address ของอุปกรณ์บลูทูธ
import bluetooth
nearby_devices = bluetooth.discover_devices(lookup_names=True)
print("found %d devices" % len(nearby_devices))
for addr, name in nearby_devices:
print(" %s - %s" % (addr, name))
Output
found 1 devices
48:26:2C:92:A4:17 - iPhone ของ Sarawut
สามารถเขียนโค้ดโปรแกรมส่งข้อมูลโดยใช้ socket ผ่าน Bluetooth ดังนี้ ฝั่ง Client
import bluetooth
serverMACAddress = '48:26:2C:92:A4:17'
port = 3
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((serverMACAddress, port))
while True:
text = input("Text : ")
if text == "quit":
break
s.send(text)
s.close()
ฝั่ง Server
import bluetooth
hostMACAddress = '48:26:2C:92:A4:17' # เป็น MAC address ของ Bluetooth adapter ใน server
port = 3
backlog = 1
size = 1024
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.bind((hostMACAddress, port))
s.listen(backlog)
try:
client, clientInfo = s.accept()
while 1:
data = client.recv(size)
if data:
print(data)
client.send(data) # Echo back to client
except:
print("Closing socket")
client.close()
s.close()
cpuinfo()
โมดูล Py-cpuinfo เป็นโมดูลสำหรับรับข้อมูล CPU เครื่องที่รัน โดยเป็น pure Python รองรับทั้ง Python 2 , 3 และใช้ MIT License
ติดตั้งได้ด้วยคำสั่ง pip install py-cpuinfoการใช้งาน
สามารถเรียกดูรายละเอียดได้ทันทีด้วยคำสั่ง python -m cpuinfo
รับรายละเอียดในโค้ด Python ได้ด้วยคำสั่งตามตัวอย่างนี้
import cpuinfo
info = cpuinfo.get_cpu_info()
print(info)