2021年2月1日 星期一

Python - What do << and >> mean?

1. "<<" Shift left for binary bit.

100 -> Decimal 100
    -> Binary 1100100
Result:
400 -> Decimal 400
    -> Binary  110010000


2. ">>" Shift left for binary bit.

100 -> Decimal 100
    -> Binary 1100100
Result:
25 -> Decimal 25
   -> Binary  11001



2021年1月17日 星期日

Sublime Text - How to create snippet code with quick key?

*Sublime version:3.2.2 build 3211 

Step1. Tools → Developer → New Snippet



Step2. Snippet sample file opened after "New Snippet".
    ⓵ Input snippet code
    ⓶ Input quick key (keyword)
    ⓷ Input Description




















Step3. Save your syntax file with ctrl+s
    Name Rule:<FileName>.sublime-snippet
    Save to User folder: ..\Sublime Text Build 3211 x64\Data\Packages\User\
    * You can create snippet folder below User if you want. Sublime will load snippet settings by file extension.



Step4. Snippet code quick key created. Let's try it!
    ⓵ Input quick key (step2's quick key)
     Quick key and description shows up


    ⓷ Select the quick function then snippet code created.






2020年5月29日 星期五

Ubuntu - New Disk Attached

1. Login with root or sudo on user account

2. List disk with command: fdisk -l
root@ub01-VM01:/home/ub01# fdisk -l
Disk /dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x64e55ca7

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 18970623 18968576    9G 83 Linux
/dev/sda2       18972670 20969471  1996802  975M  5 Extended
/dev/sda5       18972672 20969471  1996800  975M 82 Linux swap / Solaris

# New Disk /dev/sdb
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

3. Create partition on new disk with command: fdisk <new disk> → n→ p→ <Enter>→w
root@ub01-VM01:/home/ub01# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):<Enter to default>
First sector (2048-20971519, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519):

Created a new partition 1 of type 'Linux' and of size 10 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.


4. Format partition with command: mkfs -t ext3 /dev/sdb1

root@ub01-VM01:/home/ub01# mkfs -t ext3 /dev/sdb1
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 2621184 4k blocks and 655360 inodes
Filesystem UUID: 2350fe29-dc99-4a86-82cc-3b391b177957
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

5. Create mount point with command: mkdir /datadk

6. Mount disk with command: mount /dev/sdb1 /datadk
ub01@ub01-VM01:~$ sudo mount /dev/sdb1 /datadk
ub01@ub01-VM01:~$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              990892       0    990892   0% /dev
tmpfs             204132    3632    200500   2% /run
/dev/sda1        9204224 4434504   4279124  51% /
tmpfs            1020648     188   1020460   1% /dev/shm
tmpfs               5120       4      5116   1% /run/lock
tmpfs            1020648       0   1020648   0% /sys/fs/cgroup
tmpfs             204132      40    204092   1% /run/user/108
tmpfs             204132       0    204132   0% /run/user/1000
/dev/sdb1       10189112   23160   9641716   1% /datadk

7. Auto mount when boot up

a. Get uuid by command below:

    UUID by ls -lh /dev/disk/by-uuid

b. Edit /etc/fstab

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
UUID=<UUID> /data               ext4    errors=remount-ro 0       1

<file system>  Device UUID on 7.a

<mount point> Where to mount (mount to /data folder)

<type> ext4 or other type ntfs

<options> defaults

<dump> dump no=0 yes=1

<pass> self-examination no=0 yes=1


2020年4月25日 星期六

Python - How to sort dictionary? OrderedDict

#Import OrderedDict
from collections import OrderedDict

[OrderedDict Operation]
- Add Key/Value
D=OrderedDict()
D[1]=3
D[7]=1
D[2]=5
D[9]=7
#Dict status  OrderedDict([(1, 3), (7, 1), (2, 5), (9, 7)])

- Change key to Last
>>> D.move_to_end(1)
#Dict status  OrderedDict([(7, 1), (2, 5), (9, 7), (1, 3)])

- Pop Dict Item
>>> D.popitem()
(1, 3)
#Dict status  OrderedDict([(7, 1), (2, 5), (9, 7)])

- Pop first Dict Item
>>> D.popitem(last=False)
(7, 1)
#Dict status OrderedDict([(2, 5), (9, 7)])

2020年4月2日 星期四

Python3 - How to count items in list?

[Method 1] Count item in list.
# Sample list
sl=[1,2,3,1,2,3,4,5,8]  

# Count item 2
sl.count(2)
→ return 2

[Method 2] Count all item in list.
# Import defaultdict
from collections import defaultdict

# Sample list
sl=[1,2,3,1,2,3,4,5,8]   

# Create hash table
ht=defaultdict(int)

# Hash Table Status
defaultdict(<class 'int'>, {})

# Create hash table & count {items: <count>}
for x in sl:
    ht[x]+=1

# Hash Table Status
defaultdict(<class 'int'>, {1: 2, 2: 2, 3: 2, 4: 1, 5: 1, 8: 1})

# How many "2" in list?
ht[2]
→ return 2

2019年9月19日 星期四

Github commit without login - Add SSH key



  • Generate key if not exists :
          ssh-keygen
         <Recommend if root> ssh-keygen -t rsa -b 4096 -C "<git hub logon mail>"


  • Start ssh-agent :  eval `ssh-agent -s`

  • Add ssh key to ssh-agenssh-add test.rsa

  • Add key to github: User -> Setting -> SSH and GPG keys -> Add key

   

  • Test key works: ssh -T git@github.com

  • Edit remote repository url:
          Repository url: https://github.com/<Name>/<Repostory Name>/
          Command: git remote set-url origin git@github.com:<Name>/<Repostory Name>.git


  • It should be work, try commit now!

2017年11月23日 星期四

Windows - Bat - 變數輸入 + IF判斷


@echo off
set /p opt="1. option 2. option "
echo %opt%

if "%opt%" == "1" (
echo "good"
pause



if "%opt%" == "2" (
echo "bad"
pause
)

2016年12月21日 星期三

Python - Quick Note


  • Counters
      

  • Loop and remove(/insert) at same time for list (list移除不正確)
       

           ref. doc https://docs.python.org/3/tutorial/controlflow.html 4.2

  • int to str  → str(<數字>)
          
  • str to list → list("<文字>")
         
  • list to str →'', join(<List>)
     
  • reverse list → <List>.reverse()
     

  • reverse string [<begin>:<end>:<step>]
      

  • find max value for list→ max(<List>)
     

  • 確認List是否為空 → if not <List>
      

  • print不換行 → for 2.7 print("name"),   for 3.5 end=''
  • 不要空格workaround → sys.stdout.write('')
  • print int+str →需先將int轉換為str
      


  • print list in range → print A[3] to A[6] (not include A[7])
     
  • range with step
     

  • map, do something to all list member
     

  • operation to 2 list member in same place
       

  • zfill (only support str)
     

  • Binary to Decimal to Hexadecimal
    


  • lambda - simple function display
    

  • filter
    

  • reduce
   



  • Two list to dictionary
         Method 1:
     
         Method 2:
     

2016年6月1日 星期三

日文輸入法 - IME


  • Alt+shift  →切換成日文(切換輸入法)
  • alt+caps   →切換成片假名
  • ctrl+caps  →切換成平假
  • 促音         →重複最後一個  ex. さっき (sakki)
  • onaji      →々
  • kigou        →出現很多符號
  • ー             → 0旁邊的-
  • alt+~        →暫時切換成英文
  • ・              →/
  • 小字         →先打"L",大小寫均可

2012年8月6日 星期一

Bash Script - Vim 多行註解


[增加註解]

1. Esc to command mode
2. 游標移到要註解的第一行  
3. ctrl + v
4. 用上下來選取要標註解的範圍
5. I 
6. 加入要註解的符號 (ex: #, //)
7. ESC (ESC後才會加入註解)


[取消註解]
1. Esc to command mode
2. 移到要刪除註解的開頭
3. ctrl + v
4. 用上下來選取要移除註解的範圍
5. Delete

2012年7月26日 星期四

Bash Script - default value

#if input $1 then print $1
#if no input print pig
test=${1:-"pig"}
echo "$test"

2012年7月22日 星期日

Bash Script - 陣列及for迴圈範例


Ubuntu - 列出所有相關process, 排除字元, 捷取欄位, kill process

ps aux | grep <截取的字元> | grep -v <排除的字元> | awk '{print $2}' | xargs kill
awk '{print $2}'  → 印出第二個欄位  
xargs kill → 殺掉所有找到的process 

Ubuntu - MD5 generate and check

md5sum [filename] #產生md5
md5sum [filename] > test.md5 #將md5輸出至 test.md5中
md5sum -c test.md5 #驗證檔案的md5是否有改變 

Windows - Bat Script - for 迴圈 (由0印到10)


Ubuntu - Some Linux Command


Bash Script - Read file for bash


Bash Script - If...Else