Skip to content

Instantly share code, notes, and snippets.

@yeiichi
yeiichi / dict_swap.py
Created November 8, 2020 07:18
Swaps key/value of a dict.
dict_inv = dict([(j, i) for i, j in original_dict.items()])
@yeiichi
yeiichi / load_data.sql
Last active November 8, 2020 10:26
Load data in a loca INFILE
LOAD DATA
LOCAL INFILE '~/your_path/foo.csv'
INTO TABLE your_table
CHARACTER SET 'utf8mb4'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
/*
@yeiichi
yeiichi / table_joiner.py
Created January 20, 2021 07:29
JOIN tables
#!/usr/bin/env python
# coding: utf-8
from pathlib import Path
import pandas as pd
tbl_lft = Path(input('Left table? >> '))
tbl_rgt = input('Right table? >> ')
df_lft = pd.read_csv(tbl_lft)
@yeiichi
yeiichi / url_extractor.py
Created January 20, 2021 07:32
Extract HREFs from target URL and save as a CSV file.
import os
import time
from pathlib import Path
from urllib.parse import urljoin, urlsplit
import pandas as pd
import requests
from bs4 import BeautifulSoup
@yeiichi
yeiichi / postgres_memo.sql
Last active June 17, 2021 07:27
postgres cheat sheet
-- https://stackoverflow.com/questions/1517635/save-pl-pgsql-output-from-postgresql-to-a-csv-file/1517692#1517692
-- This approach runs entirely on the remote server
Copy (Select * From foo) To '/tmp/test.csv' With CSV DELIMITER ',' HEADER;
-- The other approach is to do the file handling on the client side, i.e. in your application or script.
\copy (Select * From foo) To '/tmp/test.csv' With CSV
-- Import from csv
@yeiichi
yeiichi / my-quickstart.zsh
Last active August 23, 2021 01:10
Creates a new project root
#!/usr/bin/env zsh
# Cf. https://github.com/navdeep-G/samplemod
read "?New project name? >> " new_pj
cp -r $HOME/PycharmProjects/samplemod-master ./$new_pj/
mkdir ./$new_pj/$new_pj/
rm -r ./$new_pj/sample/
#/usr/bin/env python3
def str_normalizer(raw_str):
"""Blah"""
circ_nums = {
'=1=': '①', '=2=': '②', '=3=': '③', '=4=': '④', '=5=': '⑤',
'=6=': '⑥', '=7=': '⑦', '=8=': '⑧', '=9=': '⑨', '=10=': '⑩'
}
norm_str = re.sub(r'\N{VT}', '', raw_str) # Matches a vertical tab(VT).
norm_str = re.sub(r'([①②③④⑤⑥⑦⑧⑨⑩])', r'=\g<1>=', norm_str) # maru -> arabic
@yeiichi
yeiichi / trasher.zsh
Created August 23, 2021 01:05
Safe rm for zsh.
#!/bin/zsh
# Author: Eiichi YAMAMOTO
# Created: 20th August 2021
# Last Modified: 20th August 2021
# Description:
# Move a file or a directory to the Trash
mv $1 ~/.Trash/
@yeiichi
yeiichi / ggler.zsh
Last active February 11, 2026 02:04
Google search from command line
#!/usr/bin/env zsh
# Google Search from Command Line
# Usage: ggler <query>
display_usage() {
echo "Usage: ggler <query>"
echo "Example: ggler hello world"
}
@yeiichi
yeiichi / img-indexer.zsh
Last active September 3, 2021 06:19
Generate a sphinx index.rst file for an image directory.
#!/usr/bin/env zsh
header="
.. You can adapt this file completely to your liking, but it should at least
contain the root \`toctree\` directive.
===============================
$PWD
===============================