Skip to content

Instantly share code, notes, and snippets.

View kemingy's full-sized avatar
☁️
🈚 🌕

Keming kemingy

☁️
🈚 🌕
View GitHub Profile
@kemingy
kemingy / docker-compose.yml
Created March 23, 2018 01:52
To keep one of the container running...
broken_server:
image: python3.5
command: bash -c "tail -f /dev/null"
links:
- redis
ports:
- "8000:5000"
redis:
image: redis
@kemingy
kemingy / Josephus.py
Last active March 29, 2018 08:57
Josephus problem.
# link node
class LinkNode:
def __init__(self, x):
self.value = x
self.next = None
# build link list
def build_link_list(nodes):
if not nodes:
head = None
@kemingy
kemingy / Trie.py
Created June 14, 2018 09:16
Trie tree
from collections import namedtuple
Word = namedtuple('Word', ['value', 'freq'])
class Trie:
def __init__(self):
self.root = Node('', '')
def add_word(self, path, word, freq=1):
if not isinstance(path, str):
@kemingy
kemingy / file_browser.py
Created July 7, 2018 12:55
file browser writen in Python with Flask Blueprint
import os
from flask import Flask, Blueprint, send_file, render_template, abort, jsonify
from time import ctime
SIZES = ['B', 'K', 'M', 'G']
CURRENT_PATH = os.path.abspath(os.path.curdir)
bp = Blueprint(
'file_browser',
__name__,
@kemingy
kemingy / auto_deploy_docs.md
Last active March 21, 2020 21:19
Auto deploying docs to GitHub Pages with Travis CI

Auto-Deploying Documents to GitHub Pages with Travis CI

There is already some gists talks about this topic using bash. However, Travis now support deploy in .travis.yml , which is super easy to set up. (Deploying to GitHub Pages is experimental now [2018.07.25])

I will show you an example of Python & Sphinx.

Create .travis.yml File

Here is a python example.

@kemingy
kemingy / jekyll_category_archive.html
Created August 3, 2018 03:50
Category archive example with jekyll-archives
{%- for page in site.archives -%}
{%- if page.type == 'category' -%}
<h2>{{ page.title }}</h2>
<ul class="posts">
{%- assign date_format = site.date_format | default: "%b %-d, %Y" -%}
{%- for post in page.posts -%}
<li>
<span class="post-date">{{ post.date | date: date_format }}</span>
<a href="{{ post.url | relative_url }}" class="post-link">{{ post.title }}</a>
</li>
@kemingy
kemingy / auto_deploy_blog.md
Created August 6, 2018 11:41
Auto deploy blog generated by jekyll with jekyll-archives

Auto-Deploying Blogs to GitHub Pages with Travis CI

GitHub Pages doesn't support jekyll-archives now PR. We can use Travis CI to build it and push to gh-pages branch.

There is already some gists talks about this topic using bash. However, Travis now support deploy in .travis.yml , which is super easy to set up. (Deploying to GitHub Pages is experimental now [2018.07.25])

Create .travis.yml File

Here is an example.

@kemingy
kemingy / escape_colon_in_yaml.md
Created August 6, 2018 15:19
escape colon in yaml

How to escape colon in YAML?

This is a bug/feature about YAML parser.

If you want to add something like echo "baseurl: blog/" >> _config.yml, this won't work because colon will be parsed into =>. Even adding quotes around it fails.

To do it, we can split it into two lines.

printf "baseurl:" &gt;&gt; _config.yml
@kemingy
kemingy / scrapy_sogou_pinyin.py
Created September 10, 2018 16:29
scrapy demo for sogou pinyin
# -*- coding: utf-8 -*-
import scrapy
from urllib.parse import urlparse, parse_qs
from os import path
class PhraseSpider(scrapy.Spider):
name = 'phrase'
start_urls = ['https://pinyin.sogou.com/dict/cate/']
saved_dir = './phrase'
@kemingy
kemingy / sogou_parser.py
Created September 10, 2018 16:40
decode sogou phrase
import os
import struct
from collections import namedtuple
Phrase = namedtuple('Phrase', ['word', 'pinyin', 'freq'])
OFFSET_PINYIN = 0x1540
OFFSET_HAN = 0x2628
class Sogou: