Skip to content

Instantly share code, notes, and snippets.

View nooperpudd's full-sized avatar

Winton nooperpudd

  • China
View GitHub Profile
@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
@nooperpudd
nooperpudd / iterstools
Created October 6, 2013 11:58
python itertools examples
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from itertools import *
# imap
for i in imap(lambda x:x*2,xrange(5)):
print i
# starmap
values=[(1,2),(5,23),(8,9)]
@nooperpudd
nooperpudd / with 上下文管理
Created October 6, 2013 12:24
with context api examples
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class WithInContext(object):
"""docstring for WithInContext"""
def __init__(self,context):
print "WithInContext.__init__(%s)" % context
def do_something(self):
print "WithInContext.do_something"
@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 / 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 / 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 / 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 / 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 / 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)
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)