Skip to content

Instantly share code, notes, and snippets.

View hzhu212's full-sized avatar

Henry Zhu hzhu212

  • Beijing, China
View GitHub Profile
@hzhu212
hzhu212 / python.sublime-completions
Created October 25, 2018 07:50
sublime-completion: python
{
"scope": "source.python",
"completions":
[
{ "trigger": "python\t#!python", "contents": "#!python${1:3}" },
{ "trigger": "coding\tfile encoding", "contents": "# -*- coding: ${1:utf-8} -*-" },
{ "trigger": "doc\tblock doc", "contents": "\"\"\"${1:doc}\"\"\"" },
{ "trigger": "import\timport...", "contents": "import ${1:module}" },
@hzhu212
hzhu212 / markdown.sublime-completions
Last active October 26, 2018 04:52
sublime-completion: markdown
{
"scope": "text.html.markdown, text.html.multimarkdown.markdown",
"completions":
[
/**
* Primary
*/
{ "trigger": "center_block\tBlock-Center", "contents": "<div align=\"center\">\n$1\n</div>" },
{ "trigger": "mdi_tag\tImage", "contents": "<img src=\"${1:image_path}\" style=\"${2:max-width:600px;}\"><br/>" },
@hzhu212
hzhu212 / python_logging.py
Last active January 30, 2023 14:27
Python logging quick start
import logging
import os
import sys
def get_logger(name=None, file=None, stream=None, level=None, format=None, propagate=False):
"""make a logger"""
if stream is None:
stream = sys.stderr
@hzhu212
hzhu212 / python_configparser.py
Last active January 9, 2019 07:43
Python configparser quick start
import os
try:
import configparser
except ImportError:
import ConfigParser as configparser
def get_config(config_file, section_name=None):
"""
load configuration file (like .ini).
@hzhu212
hzhu212 / python_argparse.py
Last active May 13, 2021 12:19
Python argparse use cases
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--num', dest='num', required=True, type=int, default=0)
parser.add_argument(
'-j', '--job-name', dest='job_name', required=True,
@hzhu212
hzhu212 / shell.md
Last active April 21, 2019 07:58
shell 命令收集

获取本机的外网地址

# get outer ip 
curl ifconfig.me

获取当前正在执行的脚本的路径

@hzhu212
hzhu212 / python_datetime.md
Last active May 19, 2021 03:58
Python datetime usecase

Usage of datetime and some template code

import datetime
from typing import Generator, Union

from dateutil import parser as duparser


def as_datetime(x: Union[datetime.datetime, datetime.date, str, int, float]) -> datetime.datetime:
@hzhu212
hzhu212 / python_stream_io.md
Last active June 26, 2023 12:24
Python IO, stream, iterable

Convert iterable to stream or file-like object

import io


def iterable_to_stream(iterable, buffer_size=io.DEFAULT_BUFFER_SIZE):
    """
    Lets you use an iterable (e.g. a generator) that yields bytestrings as a read-only
 input stream.
@hzhu212
hzhu212 / python_rewrite_callback_as_generator.md
Last active March 9, 2024 08:46
turn functions with a callback into Python generator

Turn functions with a callback into Python generators

See the related question in stackoverflow.

Here is an example of how we can rewrite callback of ftplib.FTP.retrbinary as generator. See the related question in stackoverflow.

PS: Rewriting callback of ftplib.FTP.retrbinary is a bad idea, because the callback is blocking and might fill up your memory. In fact, urllib.urlopen has already done the job in an elegant and non-blocking way. So, this exemaple is just for showing howto.

Solution 1: using threading and queue

@hzhu212
hzhu212 / MySQL.sql
Created April 18, 2019 07:06
MySQL snippets
-- Copy table with indexes and triggers
CREATE TABLE newtable LIKE oldtable;
INSERT newtable SELECT * FROM oldtable;
-- copy table with just structure and data
CREATE TABLE tbl_new AS SELECT * FROM tbl_old;