0%

This post would like to share some of the commonly use code can be easier use, you can think of cheatsheet for basic python code. This allow you to copy the code if you want to use it.
這篇筆記只想分享一些基本或常用到的程式可以用,,可以想成懶人包。

Basic Code

Exception capture Error on code

If you every had some experience to write code to do specfic cacultion like division of 0 and when print it will occcur Error. To not show the Error, we can use the try and exception related code.

source: Youtube:@Cod1ngTogether

  • Error Occur
    1
    2
    3
    4
    num1= 10
    num2= 0
    result = num1/num2
    print(result) #ZeroDivisionError: division by zero
    it will occur Error like below:
    1
    2
    3
    4
    File "C:\new\basic_ex\try_expection.py", line 11
    except exception e:
    ^
    SyntaxError: invalid syntax

Soluttion: for this code is to add try and exception

  • Try exception
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    try:
    num1= 10
    num2= 0
    result = num1/num2
    print(result) #ZeroDivisionError: division by zero
    #catch ZeroDivisionError error
    except ZeroDivisionError:
    print('Error: not able to divide by zero, please try again thanks!!!')

    #catch every error it occur
    except Exception as e:
    print(f"Error ocurred:{e}")

output: Error: not able to divide by zero, please try again thanks!!!

  • Real world case use function
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    def readFile(filename):

    try:
    with open(filename, "r") as file:
    content= file.read()
    except FileNotFoundError:
    print('File not found!!! Help you create one....')
    with open("missingfile.txt", "w") as file:
    content= "Hello World"
    file.write(content)
    else:
    print("File Read successfully")
    return content
    finally:
    print("Closing FIle")
    if __name__ == '__main__':
    filename= input('Your filename(ex test.txt): ')
    content= readFile(filename)
    print(content)
Read more »

This is my network note which record different linux and debug command

Debug Using tool

Nmap port scan

Scan port can use either:

nc -vz: quick, lightweight probe (good for QA spot checks).
nmap: more detailed: can scan ranges, detect service versions, OS fingerprints, etc.

1
nmap -Pn -p 21 172.21.201.16

Scan port option:

-Pn: don’t ping first (treat host as alive)
-p 21: scan only port 21

Netcat: scan and send traffic

The nc command allow you to debug like send a small traffic or scan port just like nmap

Scan if a port is open like nmap

Syntax: nc -vz ip-add port

-v: verbose
-z: just scan for listening service (don’t send data)

Read more »

This is the automatic script which i will be using on debug or reproduce issue like stability test for Network.

Shell Script

This script will be added inside console, keep running and logged log.

syntax of bashscript:

1
2
3
4
5
6
7
8
#!/bin/sh
while true
do
echo "Polling EthPhy Status !"


sleep 1 ;
done

Read top evey 60 second and logf file

1
2
3
4
5
6
while true; do
date
free
top -b -n1 | head -15
sleep 60
done >> /tmp/memlog.txt

check bbu status every 10 second

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/sh
htx_pcd_app -a start -r BATTERY_HOST
while [ 1 ];
do
date
echo "####################"
battery_host -m 1 -s
echo "###################"
echo ""
echo "================"
uci show bbu -c /var/tmp
echo "================"
sleep 10
echo ""
echo ""
done

TTL Macro Script

Read more »

今天想要分享快速使用 UV 方式,不太會去多介紹它,如果你想更了解可以去我這篇文章有寫很多內容,但它是英文。我希望這裡放一些可以馬上就已用指令。

我想信大家都有用過 Python 都知道用 venv 是為了要獨立環境,用 pip or poetry 是為了裝套件等等。有時候用起來很麻煩,UV 解掉所以問體。剛剛提到的兩個是不同工具,UV 把這些用再一起,換句話說就是一個 UV 就可以有虛擬環境,套件管理等等功能,最重要是它是用 RUST 開發的因此很快。如果想了解更多請到可以到它官網看。

Traditional Way 傳統方式 (venv)

下面方式就是我們以前最常用的:

  • create venv
  • activate venv
  • pip 安裝套件
  • 把套件匯出
1
2
3
4
5
6
7
8
mkdir withoutUV_Project #create a project
cd withoutUV_Project
python3 -m venv .venv #create a virtual env
cd withoutUV_Project
.\Scripts\activate #activate virtual env
which python3
pip3 install numpy pandas selenium
python3 freeze > requirements.txt

今天想要分享快速使用 UV 方式,不太會去多介紹它,如果你想更了解可以去我這篇文章有寫很多內容,但它是英文。我希望這裡放一些可以馬上就已用指令。

我想信大家都有用過 Python 都知道用 venv 是為了要獨立環境,用 pip or poetry 是為了裝套件等等。有時候用起來很麻煩,UV 解掉所以問體。剛剛提到的兩個是不同工具,UV 把這些用再一起,換句話說就是一個 UV 就可以有虛擬環境,套件管理等等功能,最重要是它是用 RUST 開發的因此很快。如果想了解更多請到可以到它官網看。

Notice when you just install dependency like request or selenium, it install other package, this is called the transitive dependencies or sub-dependency. Some packages rely on other internal packages (transitive dependencies).

UV 安裝

Read more »

我想大家都知道 SSH 是什麼,也很多人應該已經會用 SSH Key 可以免輸入密碼就可以 SSH 登入。我想信大家用 github 應該都會用,但我今天不是要用 github 我要用 ubuntu 當作 SERVER, window 當做 client. 其實做法是一樣,github server 當作我自己 ubuntu server。

很多時候你用 github 可能一些 server 上運作可能你不太知道背後的運動是如何,今天可以介由者個範例可以理解更多以真實 server

請務必先安裝 openssh 能用

1
sudo apt install openssh-server
  • 公鑰跟私鑰是一組,這樣才可以連線,他們會做連結
    • Private Key 私鑰: 要存在你自己電腦
    • Public Key 私鑰: 要放在本地端要連到 Sever。

client 建 Key

這個方式是最常用的方式,就跟 github 一樣,本地端會建 private 和 public key,然後把 public key 傳到 server 上面並加入 Server 上的 ~/.ssh/authorized_keys 檔案中。)

以這個範例我本地端(Local/Client)要用 SSH 遠端到 Server 端,我需要把我的公鑰(public key)傳到 Server 這樣私鑰做連線會跟 Server 公鑰比對,是同一組就會連線。

  • 在 SSH 連線中,Client 會先產生一組公鑰(public key)和私鑰(private key)。
  • 公鑰會放到 Server 上,而私鑰只保留在 Client。

當 Client 嘗試連線時,會使用私鑰(private key)來進行驗證,而 Server 會利用它儲存的公鑰來驗證 Client 的私鑰是否正確,確認是同一對金鑰後,即可成功連線,我下面我教如何做到。

Read more »

今天想分享我用 Rassberry PI5 架 ACS Server,如果你知道 ACS Server 是甚麼,可以看我如何架。ACS Auto Configure server,類似遠端的工具可以看到你的通訊產品的狀況如上線時間,reboot,更新 FW 等等

Note:

For English Note please refer to my github note link

Raspberry PI Model

Check Ubuntu Version

1
lsb_release -a

Check Kernel

1
uname -ar

check Raspberry Model

1
2
3
cat /proc/device-tree/model
#or
cat /proc/cpuinfo | grep 'Model'
Read more »

今天我想要分享用 UBUNTU 架設 ngix http server,同時我也會用 docker 方式安裝。

NGIX Setup ubuntu

Step1. Install Nginx

1
sudo apt install nginx -y

Step2. show nginx status

Verify Nginx is Running show statusd

1
sudo systemctl status nginx

Step3: Firewall setting

1
2
sudo ufw allow 'Nginx HTTP'
sudo ufw reload
Read more »

今天想分享如何用 python 轉 exe,也就是說如果你把你的 python 檔案轉成 exe 放在其他電腦就可以執行。這個好處就是你不用在其他電腦安裝 python 或其他 library,就可以用。

Install Pyinstaller

這有很多方法和套件可以用,但我都用 pyinstaller

安裝 pyinstaller: pip install pyinstaller

pyinstaller 用法

  • 基本用法
1
2
#simple convert
pyinstaller your_script.py
  • Adding Icons:

可以在下面連結下載 ico 也可以用,下載會是 .png檔案,需要轉成 .ico

1
pyinstaller --onefile --icon=myicon.ico your_script.py
Read more »

This is a note for change your python version in jupyter Notebook. 今天想要分享如何換在 jupyter Notebook 換不同 python 般本

Please Install latest python for example python 3.13 which is the latest version, if you are going to switch to python 3.13. You don’t have to install latest version, it’s because I want to switch to python3.13, please be noted.

請務必先安裝最新的 python 3.13,由於我要切換到 python 3.13,因此必須要先安裝 python 3.13。

Changing python version in jupyter

Step changing python version

Step1: Install package

  • Install Jupyter (optional): pip install jupyter

  • Install ipkernel: pip install ipykernel

You can also install under virtual environment

1
2
3
4
5
6
7
#1. create virtual env with conda
#syntax: conda create -n <virtual env name> python=3.13
conda create -n py313 python=3.13


#2. Activate the new environment
conda activate py313

you can also use conda command conda env list to list virtual env.

Read more »