Update ubuntu  packages

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

A reboot may or may not be necessary (sudo shutdown -r 0)

git pull with out password

ในกรณีที่ git pull แล้วระบบขอ password เราสามารถใช้ credential.helper  มาช่วยได้

  • git config credential.helper store - stores the credentials indefinitely.
  • git config credential.helper 'cache --timeout=3600'- stores for 60 minutes

Python: Create text line with one line code

print("".join(["-" for i in range(10)]))

ANSII Color formatting for output in terminal.

ANSII Color formatting for output in terminal.
import sys
from termcolor import colored, cprint

text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
cprint('Hello, World!', 'green', 'on_red')

print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan')
print_red_on_cyan('Hello, World!')
print_red_on_cyan('Hello, Universe!')

for i in range(10):
    cprint(i, 'magenta', end=' ')

cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)

Enable mouse mode on Tmux

To use your mouse in this mode (called copy mode) press ^b + : and enter following:

setw -g mouse on

Run python functions in parallel

Python: How can I run python functions in parallel?
I researched first and couldn’t find an answer to my question. I am trying to run multiple functions in parallel in Python. I have something like this: files.py import common #common is a util c...

Python: Remove all query string parameters:

from urllib.parse import urljoin, urlparse

url = 'http://example.com/?a=text&q2=text2&q3=text3&q2=text4'
urljoin(url, urlparse(url).path)  # 'http://example.com/'

Remove empty strings from a list of strings

use filter:

str_list = filter(None, str_list)
str_list = filter(bool, str_list)
str_list = filter(len, str_list)
str_list = filter(lambda item: item, str_list)

use loop:

>>> strings = ["first", "", "second"]
>>> [x for x in strings if x]
['first', 'second']

Python datetime ที่ใช้บ่อย

from datetime import datetime

# date to string
dateStr = datetime.now().strftime("%Y%m%d-%H%M%S")

# string to date
datetime.strptime("2020-09-01", "%Y-%m-%d")

Python: คำนวนเวลาการทำงานของ code

then = datetime.now() # Random date in the past

now  = datetime.now() # Now
duration = now - then # For build-in functions
duration_in_s = duration.total_seconds()  

days    = divmod(duration_in_s, 86400) # Get days (without [0]!)
hours   = divmod(days[1], 3600) # Use remainder of days to calc hours
minutes = divmod(hours[1], 60) # Use remainder of hours to calc minutes
seconds = divmod(minutes[1], 1) # Use remainder of minutes to calc seconds
print("⏰ Time between dates: %d days, %d hours, %d minutes and %d seconds \n" % (days[0], hours[0], minutes[0], seconds[0]))

Python: Check and Make Dir

import os
from os import path

def makeDir(fullpath):
	if not path.exists(fullpath):

Python: remove file if exist

import os
from os import path

def removeFile(self):
	if os.path.exists(self.fullpath):

Python: หาวันย้อนหลังกลับไป x วัน

from datetime import datetime, timedelta, date

today = date.today()    
lastday = today    
lastday -= timedelta(days=2)    
year = lastday.year    
month = lastday.month    
days = lastday.day

print(year, month, days)