Skip to content

Instantly share code, notes, and snippets.

class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums = sorted(nums)
res = []
n = len(nums)
for i in range(n-2):
@guyskk
guyskk / deferable.py
Created March 10, 2018 06:54
A idea of using contextvars to solve generator finalize problem
"""
contextvars is added in unreleased cpython, I build it from master branch
Python 3.8.0a0 (heads/master:3b20d345, Mar 10 2018, 13:23:11).
Also modify 2 lines in curio/kernel.py:
kernel.py Line 327:
> task._ctx = contextvars.copy_context()
kernel.py Line 827:
@guyskk
guyskk / pysql_format.py
Created November 16, 2017 06:20
Format SQL in python source code
"""
Requires:
pip install click sqlparse
"""
import re
from textwrap import indent, dedent
import click
import sqlparse
@guyskk
guyskk / validator_string.py
Created April 16, 2017 14:20
validator string DSL parser by pyparsing
# https://pythonhosted.org/pyparsing/
from pyparsing import *
# https://pyparsing.wikispaces.com/file/view/jsonParser.py
def make_keyword(kwd_str, kwd_value):
return Keyword(kwd_str).setParseAction(replaceWith(kwd_value))
TRUE = make_keyword("true", True)
FALSE = make_keyword("false", False)
NULL = make_keyword("null", None)
@guyskk
guyskk / classtree.py
Last active March 22, 2017 12:58
Print class tree, include all sub classes alive in Python VM
# coding:utf-8
from inspect import getclasstree
def classtree(cls, indent=0, fillchar='-'):
"""
Print class tree
Args:
cls: base class
@guyskk
guyskk / diagnose.py
Created January 30, 2017 14:35
print your platform information and package information
# coding:utf-8
import platform
def pkginfo():
for pkg in ['setuptools', 'pip', 'wheel',
'virtualenv', 'distutils', 'cython']:
try:
version = getattr(__import__(pkg), '__version__', 'Unknown')
print('{} = {}'.format(pkg, version))
@guyskk
guyskk / flask_context.py
Last active January 12, 2018 02:13
理解Flask上下文环境
import flask
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "hello"
@guyskk
guyskk / jookr.py
Last active October 26, 2022 15:42
import os
SQL = "INSERT INTO table (`value1`,`value2`,) VALUES ('%s','%s');\n"
def main():
for fname in os.listdir("."):
# step1
name, ext = os.path.splitext(fname)
if ext != ".txt":
def handle_default_optional_desc(some_validater):
def wrapped_validater(*args, **kwargs):
default = kwargs.pop("default", None)
optional = kwargs.pop("optional", False)
desc = kwargs.pop("desc", None)
origin_validater = some_validater(*args, **kwargs)
def validater(value):
if value is None:
if default is not None:
@guyskk
guyskk / init.vim
Created May 26, 2016 09:34
nvim config
if has('vim_starting')
set runtimepath+=~/.config/nvim/bundle/neobundle.vim/
endif
call neobundle#begin(expand('~/.config/nvim/bundle'))
NeoBundleFetch 'Shougo/neobundle.vim'
NeoBundle 'altercation/vim-colors-solarized'
NeoBundle 'scrooloose/nerdtree'
NeoBundle 'tpope/vim-fugitive'
NeoBundle 'mattn/emmet-vim'