My Gists

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: import common #common is a util c...

Python: Remove all query string parameters:

from urllib.parse import urljoin, urlparse

url = ''
urljoin(url, urlparse(url).path)  # ''

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 ="%Y%m%d-%H%M%S")

# string to datetime
datetime.strptime("2020-09-01", "%Y-%m-%d")
datetime.strptime("2020-07-17T09:52:34.653Z", "%Y-%m-%dT%H:%M:%S.%fZ")

# timestamp to datetime
created = datetime.fromtimestamp(int(created) / 1000)

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

then = # Random date in the past

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 =    
lastday = today    
lastday -= timedelta(days=2)    
year = lastday.year    
month = lastday.month    
days =

print(year, month, days)

Python: regex หาตัวเลขกับจุด

re.findall(r"Test([0-9.]*[0-9]+)", text)

or, a bit shorter:

re.findall(r"Test([\d.]*\d+)", text)

By the way - you do not need to escape the dot in a character class. Inside [] the . has no special meaning, it just matches a literal dot. Escaping it has no effect.

Python selenium: scroll down แบบไม่รีบ

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
last_height = last_height - 1300 # i don't want to scroll to footer
print("last_height:", last_height)

for step_height in range(0, last_height, 100):
	# driver.execute_script("window.scrollTo(0, document.body.scrollHeight-500);")
    driver.execute_script(f"window.scrollTo(0, {step_height});")
    # print("step_height:", step_height)

BS4: print BeautifulSoup's string output ให้อ่านง่าย

use .prettify()


Conda Remove all packages in environment

conda env remove -n ENV_NAME

Ubuntu: finds the size recursively and puts it next to each folder name, all in the human format

du -hsc *

# if you want to get a sorted list of folders
du -hsc * | sort -hr

Python: Wait until page is loaded with Selenium WebDriver

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from import WebDriverWait
from import expected_conditions as EC
from import By

driver = webdriver.Firefox()
timeout = 5
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"

If you want remove all local changes - including files that are untracked by git - from your working copy, simply stash them:

git stash push --include-untracked

Generate Random Strings in Python

import random

def random_str(str_range):
    random_string = ''
    for _ in range(str_range):
        # Considering only upper and lowercase letters
        random_integer = random.randint(97, 97 + 26 - 1)
        flip_bit = random.randint(0, 1)
        # Convert to lowercase if the flip bit is on
        random_integer = random_integer - 32 if flip_bit == 1 else random_integer
        # Keep appending random characters using chr(x)
        random_string += (chr(random_integer))
    # print(random_string, len(random_string))
    return random_string

Ubuntu config file and create alias

sudo vi ~/.bashrc

alias ll='ls -l'

source ~/.bashrc

Install miniconda

chmod +x

Ubuntu set timezone

Check current timezone


The system timezone is configured by symlinking /etc/localtime to a binary timezone identifier in the /usr/share/zoneinfo directory.
Another option to view the current system’s timezone is find the file to which the symlink points to:

ls -l /etc/localtime
# or
cat /etc/timezone

Before changing the timezone, you’ll need to find out the long name for the timezone you want to use.

timedatectl list-timezones

Once you identify which time zone is accurate to your location, run the following command as sudo user:

sudo timedatectl set-timezone Asia/Bangkok

Python sort dict by key

orders = {
	'cappuccino': 54,
	'latte': 56,
	'espresso': 72,
	'americano': 48,
	'cortado': 41

sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True)

for i in sort_orders:
	print(i[0], i[1])

Sort dict by key multiple attributes

duplist = sorted(items, key=lambda x: (x['length'], x['question']), reverse=True)

Pandas  loop get data by filed name

df = pd.read_csv(FILENAME_CSV)

for num_row, row in df.iterrows():
  name = row['name']

Kill process with Port

kill $(lsof -i:5000 -t)