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.

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

text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)
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
    
yourCodeHere()

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):
		os.makedirs(fullpath)

Python: remove file if exist

import os
from os import path

def removeFile(self):
	if os.path.exists(self.fullpath):
		os.remove(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)

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)
    time.sleep(0.07)

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

use .prettify()

print(new_listings[0].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 selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    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

wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda-latest-Linux-x86_64.sh
./Miniconda-latest-Linux-x86_64.sh

Ubuntu set timezone

Check current timezone

timedatectl

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