Skip to content

Instantly share code, notes, and snippets.

View nooperpudd's full-sized avatar

Winton nooperpudd

  • China
View GitHub Profile
@nooperpudd
nooperpudd / opencv.py
Created November 3, 2017 08:22
opencv mask
import cv2
import numpy
image = cv2.imread("test.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert image to gray
# cv2.imshow("image",gray)
blur_image = cv2.GaussianBlur(gray, (5, 5), 0) # 平滑图像处理,高斯滤波
@nooperpudd
nooperpudd / group_by.py
Created August 23, 2017 14:36
group list
@register.filter
def group_by(l, arg):
"""
group the list l as the number of arg
l:list
arg: an integer number
return: an iter data
"""
for i in range(0, len(l), arg):
yield l[i:i + arg]
def single_task(lock_arg, lock_prefix="lock-", timeout=CeleryConfig.LOCK_DEFAULT_TIMEOUT):
"""
:param lock_arg: set the lock key of the task parameter
:param lock_prefix:
:param timeout: set the lock key timeout
Enforce only one celery task at a time.
"""
def task_exec(func):
@functools.wraps(func)
@nooperpudd
nooperpudd / single.py
Created August 23, 2017 06:20
redis single task lock
def single_task(lock_arg, lock_prefix="lock-", timeout=CeleryConfig.LOCK_DEFAULT_TIMEOUT):
"""
:param lock_arg: set the lock key of the task parameter
:param lock_prefix:
:param timeout: set the lock key timeout
Enforce only one celery task at a time.
"""
def task_exec(func):
@functools.wraps(func)
@nooperpudd
nooperpudd / common_keys.py
Last active August 23, 2017 06:17
dict common keys
def check_dict_common_keys(dict_list):
"""
check the dict common keys in the dict list
:param dict_list: [{},{}...]
:return: set()
"""
if len(dict_list) >= 2:
common_keys = set(dict_list[0].keys())
for dict_item in dict_list[1:]:
common_keys.intersection_update(set(dict_item.keys()))
@nooperpudd
nooperpudd / list_filter.py
Last active August 23, 2017 05:30
list filter remove momory profile and execute time profile
from memory_profiler import profile
from itertools import filterfalse
@profile(precision=1)
def test():
a = []
for i in range(100000):
a.append((10.23 + i, {"data": i}))
b = filterfalse(lambda x: float(x[0]) >= 1000.26, a)
@nooperpudd
nooperpudd / sort_domain.py
Created August 22, 2017 09:25
Sort domain
def sort_by_domain(sites):
sitebits = [site.lower().lstrip('http://').split('.') for site in sites]
for site in sitebits:
site.reverse()
print(sitebits)
return [('.'.join(reversed(site))) for site in sorted(sitebits)]
@nooperpudd
nooperpudd / asyncio_cmd.py
Created July 4, 2017 06:19
python3 asyncio command
import asyncio
import sys
async def execute(command, cwd=None, shell=True):
process = await asyncio.create_subprocess_exec(*command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=cwd,
shell=shell)
std_out, std_err = await process.communicate()
@nooperpudd
nooperpudd / dict_common_keys.py
Last active April 25, 2017 08:33
Check dict list common keys
def dict_common_keys(dict_list):
"""
check python dict list common keys
:param dict_list: [{},{}...]
:return:
"""
if len(dict_list) >= 2:
common_keys = set(dict_list[0].keys())
for dict_item in dict_list[1:]:
@nooperpudd
nooperpudd / xmpp.py
Created February 1, 2014 07:24
xmpp test the client . via sleekxmpp.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# encoding:utf-8
import logging
import sys
import logging
from optparse import OptionParser
from sleekxmpp import ClientXMPP