Skip to content

Instantly share code, notes, and snippets.

View leafsummer's full-sized avatar
🎯
Focusing

LeafSummer leafsummer

🎯
Focusing
View GitHub Profile
@leafsummer
leafsummer / file_lock.py
Created April 17, 2019 14:32
[file-lock]
import time
import errno
import threading
import weakref
__all__ = ["lock_file"]
try:
import fcntl
@leafsummer
leafsummer / proxy_request.py
Created April 17, 2019 13:48
[proxy_requests]The ProxyRequests class first scrapes proxies from the web. Then it recursively attempts to make a request if the initial request with a proxy is unsuccessful.
#! /usr/bin/python3
import requests
import re
from requests.auth import HTTPBasicAuth
class ProxyRequests:
def __init__(self, url):
self.sockets = []
self.url = url
@leafsummer
leafsummer / hackhttp.py
Created April 17, 2019 13:43
[hackhttp]Hackhttp is an HTTP library, written in Python.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Queue
import urlparse
import httplib
import ssl
import zlib
import gzip
import StringIO
import re
@leafsummer
leafsummer / subquery_in_from.py
Created March 21, 2019 05:57
[peewee subquery in from clause]
#!/usr/bin/python
# coding:utf-8
from peewee import SQL
from peewee import QueryCompiler
from models import Blog
compiler = QueryCompiler('"', '?', {}, {})
@leafsummer
leafsummer / longestsubstring.py
Created February 19, 2019 03:24
[length of longest sub string]
def longest_substring(s):
res, start, n = 0, 0, 0, len(s)
maps = {}
for i in range(n):
start = max(start, maps.get(s[i], -1)+1)
res = max(res, i-start+1)
maps[s[i]] = i
return start, start+res-1, res
@leafsummer
leafsummer / tool_func.py
Last active December 11, 2018 07:33
[tool function]
# -*- coding: utf-8 -*-
def first(iterable, default=None, key=None):
"""
Return first element of `iterable` that evaluates true, else return None
(or an optional default value).
>>> first([0, False, None, [], (), 42])
42
@leafsummer
leafsummer / local.py
Created December 6, 2018 09:52
[context-local object]
# -*- coding: utf-8 -*-
# flake8: noqa
"""
werkzeug.local
~~~~~~~~~~~~~~
This module implements context-local objects.
:copyright: (c) 2011 by the Werkzeug Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
@leafsummer
leafsummer / colorize.py
Created December 6, 2018 07:54
[colorize utils and colorizingstreamhandler]
# -*- coding: utf-8 -*-
import logging
def is_python_version(*versions):
for version in versions:
if (sys.version_info[0] == version[0] and
sys.version_info >= version):
return True
@leafsummer
leafsummer / text_handle.py
Created December 6, 2018 07:50
[change text to unicode, compat python2 or python3]
import sys
PY2 = sys.version_info[0] == 2
if not PY2:
# Python 3.x and up
text_type = str
string_types = (str,)
def as_text(v):
if v is None:
@leafsummer
leafsummer / cached_property.py
Created November 30, 2018 10:34
[python description for cache the property]
class cached_property(object):
"""
Decorator that converts a method with a single self argument into a
property cached on the instance.
Optional ``name`` argument allows you to make cached properties of other
methods. (e.g. url = cached_property(get_absolute_url, name='url') )
"""
def __init__(self, func, name=None):
self.func = func