This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2, zlib | |
def GetContent(url, gzip=False, charset=None, headers=None): | |
"用于解析url内容" | |
try: | |
msg = None | |
if headers: | |
request = urllib2.Request(url=url, headers=headers) | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# encoding:utf-8 | |
import logging | |
import sys | |
import logging | |
from optparse import OptionParser | |
from sleekxmpp import ClientXMPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:]: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer