Skip to main content

Command Palette

Search for a command to run...

VSCode - Python Code Formatter များအကြောင်း

Published
3 min readView as Markdown
VSCode - Python Code Formatter များအကြောင်း

Python Code Formatter tool တွေအကြောင်း (Black , Ruff, etc) ဖတ်ဖူးတာ ကြာပီဆိုပေမယ့် vscode မှာ setup မလုပ်ဖြစ်ဘူး။ ဒီမနက် အစောကြီး နိုးနေတာနဲ့ computer ရှေ့ ထိုင်ရင်း လျှောက်စမ်းရင်း တော်တော်ကြာသွားတယ်။ အကြောင်းရင်းက vscode မှာ သွင်းထားမိတဲ့ pylance က အနီရဲရဲ error message တွေကို ကြာလာတော့ မြင်ရတာအဆင်မပြေတာကြောင့် ရယ် ၊ နောက်ပီး ကျောင်း project မှာ Guide ဆရာ က အဲဒီ equal sign = ရှေ့နောက် space ဖြုတ်ဖို့ coding style rule အကြောင်း ခနခန ပြောလွန်းတာရယ်ကြောင့် code formatting လုပ်မှ အဆင်ပြေတော့မှာပဲ ဆိုတာကနေ စလုပ်မိတာ။

Installation

Python Code Formatter tools အားလုံးလိုလိုက pip install နဲ့ပဲ လွယ်လွယ်ကူကူသွင်းလို့ ရတယ်။ ဒါပေမယ့် vscode နဲ့ တွဲသုံးဖို့ဆိုရင် local မှာ သွင်းဖို့ မလိုလောက်ဘူး ။ ဥပမာ - VScode extension ကနေ Black formatter (or) Ruff ရှာသွင်းရင် အဲဒီ description မှာ extension နဲ့ အတူ သွင်းပေးထားတဲ့ version တစ်ခု ပါတယ်လို့ ဆိုထားတယ်။ ဒါပေမယ့် ကျွန်တော်က command line (vscode မဟုတ်တဲ့ အခြားအပြင်ဘက်နေရာတွေမှာပါ) လျှောက်စမ်းချင်တာနဲ့ pip install black (or) pip install ruff နဲ့ သွင်းလိုက်တယ်။

Black - Python Code Formatter

Official documentation website.

ဒီကောင်က နာမည်ကြီးတာရော ၊ နောက်ပြီး vscode ထဲမှာ miscrosoft ရဲ့ official extenstion အနေနဲ့ ရှိနေတာရောကြောင့် အရင် စမ်းဖြစ်လိုက်တာ။ vscode extensions ထဲကနေ ရှာပြီး install လုပ်။ ပြီးရင် vscode ရဲ့ setting.json ထဲမှာ နဲနဲသွားပြင်ရုံပါပဲ။ (vscode setting.json ထဲသွားရန် >> Command Palette > Open User Setting (JSON)) ။ အဲဒီ json အတွင်းထဲမှာ extension description မှာ ပြောထားတဲ့ setting တွေ ထည့်ရုံပါပဲ။

"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true

Black Formatter မှာက အခြား custom setting တွေ လုပ်လို့ရတယ် ပြောပေမယ့် ကိုယ်ပဲ အရမ်းနားမလည်လို့လား မသိဘူး ။ ရှာမတွေ့ / မလုပ်တတ်ဘူး။ Documentation ကလဲ Ruff နဲ့ ယှဥ်မယ် ဆိုရင် သိပ်မပြည့်စုံဘူး။ အဲဒါဆိုတော့ Python file ကို formatting လုပ်ပေးပေမယ့် ကျွန်တော်လိုချင်နေတဲ့ spacing နဲ့ ပတ်သတ်ပြီး format မလုပ်ပေးဘူး။

Ruff - Python Linter & Code Formatter

Official documentation website.

သူကတော့ microsoft က ထုတ်တာမဟုတ်ပေမယ့် Rust Programming Language နဲ့ ရေးထားလို့ အတော်မြန်တယ်လို့တော့ ကြားဖူးထားပြီးသား။ documentation တော့ တော်တော်ပြည့်စုံတယ် ပြောရမယ်။ Black ကနေ Ruff ကို ပြောင်းဖြစ်သွားတာက အခု ကျွန်တော်လိုချင်တဲ့ Removing White space code formatting ဆိုပြီး google မှာ ရှာတော့ Ruff မှာ custom ထည့်လို့ ရတဲ့ Rules တွေအကြောင်း သွားတွေ့တာ။ အဲဒီမှာ ကျွန်တော်လိုချင်နေတာက E251 ဆိုတဲ့ rule။ ဒါပေမယ့် Ruff documentation ထဲမှာ ရေးထားသလို အဲဒီ rule က Preview အနေနဲ့ပဲ ဖြစ်နေတယ်။ အဲဒီတော့ သူတို့ လမ်းညွှန်ထားသလို -

  • Project Root Folder အောက်မှာ pyproject.toml ဆိုတဲ့ file တစ်ခု အသစ်ဆောက်လိုက်တယ်။

  • အဲဒီ file ထဲမှာ အောက်က အတိုင်း ရေးထည့်ရတယ်။

[tool.ruff]
line-length = 100

[tool.ruff.lint]
preview = true
extend-select = ["E251"]

line-length က ဒီအတိုင်း လျှောက်စမ်းရင်း ထည့်ထားတာ။ ကိုယ့် code တစ်ကြောင်းချင်းဆီရဲ့ ပမာဏ ကို ကန့်သတ်တာဆိုတော့ မလိုရင် ဖြုတ်လို့ ရတယ်။ extend-select list ထဲမှာ ကိုယ် လိုချင်တဲ့ rule code တွေ ထည့်ပေါ့ ။ Rule တွေ သီးသန့် ဖြစ်ချင်တယ် ဆိုရင် အဲဒီ `pyproject.toml` ထဲမှာပဲ နောက်တကြောင်း ထပ်ထည့်လို့ ရတယ်။

explicit-preview-rules = true

အပြည့်အစုံကို Ruff ရဲ့ Preview section အောက်မှာ သွားဖတ်လို့ ရတယ်။

အားလုံး ပြီးလို့ .toml file ကို save လုပ်ပြီးရင် စသုံးလို့ ရပြီ။ ကိုယ် စမ်းသပ်ချင်တဲ့ code file ရဲ့ formatting မှန်/မမှန်ကို command line ကနေ လဲ စမ်းသပ်လို့ ရတယ်။

ruff check **filename_that_you_want_to_test.py

အကယ်၍ တဖိုင် ချင်းဆီမဟုတ်ပဲ Project Folder အောက်ထဲက ရှိသမျှ အကုန်ကို စမ်းသပ်ချင်တယ် ဆိုရင် -

ruff check .

Ruff ကို VSCode ရဲ့ default code formatter အနေနဲ့ သုံးဖို့ ဆိုရင် Black တုန်းကလိုပဲ VScode ရဲ့ setting.json ထဲမှာ သွားပြင်ပေးဖို့လိုတယ်။

"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true

Autopep8

ဒီအတိုင်း vscode extension ထဲမှာ တွေ့လို့ install လုပ်ပီး စမ်းမိတာ ။ အပေါ်က ၂ ခုနဲ့ ယှဥ်ရင် သိပ်အဆင်မပြေပါဘူး။

Person Decision

လောလောဆယ်အနေနဲ့ Ruff ကိုပဲ ဆက်သုံးဖြစ်မယ် ထင်တယ်။ custom rules တွေ လွယ်လွယ် စိတ်ကြိုက်ထည့်လို့ရတာရယ် ၊ တစ်ခုခု ဆိုရင် documentation သွားဖတ်ရုံနဲ့ အဖြေရတာရယ်ကြောင့် အဓိက ရွေးဖြစ်တာ။ Black မှာလဲ သူ့အားသာချက်တွေတော့ ရှိနိုင်ပါတယ်။

အကယ်ရွေ့ မိတ်ဆွေတို့ဆီမှာလဲ ကိုယ့်စိတ်ကြိုက် Code Formatter တွေ ရှိမယ် ဆိုရင် comment မှာ မျှဝေပေးခဲ့စေချင်ပါတယ်။

More from this blog

Difference Between Regional NAT Gateway and Zonal NAT Gateway

ဒီ post လေးထဲမှာတော့ Regional NAT Gateway နဲ့ Zonal NAT Gateway ကွာခြားချက်တွေကို နှိုင်းယှဉ်ပြသွားမှာဘဲ ဖြစ်ပါတယ်။ ပထဆုံးအနေနဲ့ NAT Gateway ဆိုတာ ဘာလဲ ဘာအတွက် လိုအပ်တာလဲဆိုတာကို အရင်ပြောပြပေးပါမယ်။ P

Jun 19, 20264 min read132
Difference Between Regional NAT Gateway and Zonal NAT Gateway

Infrastructure ကိုင်ပြီး အိပ်ရေးမပျက် ချင် လျှင် ဒါမျိုး Alarms လုပ် 🔥🔥🔥

High Level ရေးထားတာပါ ဒါပေမဲ့ လွယ်ပါတယ် ​ကိုယ့်မှာ AWS Infra တွေရှိတယ်ဆို တွေ့သမျှ metric တွေကို alarms တွေလုပ်ပြီး notification ယူမနေဘဲ တကယ် effective ဖြစ်တဲ့ metric တွေကိုမှ CloudWatch ရဲ့ alarm feature တွေနဲ့ ပေါင်းပြီး ပို့စေချင်ပါတယ်။ ​ဥပမာ prod...

Jan 17, 20263 min read258
Infrastructure ကိုင်ပြီး အိပ်ရေးမပျက် ချင် လျှင်  ဒါမျိုး Alarms လုပ် 🔥🔥🔥

How to connect On Premises Network and Cloud (AWS)? (Part-2)

ကိုယ့်ရဲ့ ‌data center (on-prem) network နဲ့ AWS ချိတ်ဆက်ဖို့ လိုလာပြီဆိုရင် ဘယ်လို ချိတ်ဆက်ကြမလဲ? အပိုင်း (၂) မှာ တော့ Direct connect အကြောင်းကို ဆွေးနွေး သွားမှာ ဖြစ်ပါတယ်။ အပိုင်း (၁) Site-to-site VPN အကြောင်းကို လေ့လာချင်ရင်တော့ အောက်ပါ link မှာ ...

Dec 20, 20253 min read284
How to connect On Premises Network and Cloud (AWS)? (Part-2)

How to connect On Premises Network and Cloud (AWS)? (Part-1)

ကိုယ့်ရဲ့ ‌data center (on-prem) network နဲ့ AWS ချိတ်ဆက်ဖို့ လိုလာပြီဆိုရင် ချိတ်ဆက်နိုင်တဲ့ နည်း (၂) နည်း ရှိပါတယ်။ 1. Site-to-Site VPN (Virtual Private Network) 2. Direct connect Site-to-Site VPN - On-prem network နဲ့ AWS resources တွေ ချိတ်ဆက်တဲ့...

Dec 12, 20252 min read317
How to connect On Premises Network and Cloud (AWS)? (Part-1)
M

Myanmar Technical Blog

110 posts

Cloud, Linux, DevOps, Docker, Security အစရှိတဲ့ နည်းပညာများ အကြောင်းကို မြန်မာလို ပြန်လည်မျှဝေပေးမယ့် Blog ပဲဖြစ်ပါတယ်ခဗျာ...