-
-
Save Larry-u/f72dd4ed343f945dd6f0bd97e05eb140 to your computer and use it in GitHub Desktop.
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
'''=============================插入根目录到path==========================''' | |
ROOT = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..") | |
sys.path.insert(0, os.path.join(ROOT)) | |
'''=============================format时间为x天x小时x分==========================''' | |
import time | |
import datetime | |
'{:0>8}'.format(str(datetime.timedelta(seconds=round(time.time() - main_st)))) | |
'''=============================python多进程处理==========================''' | |
from multiprocessing import Pool | |
def f(x): | |
return x*x | |
if __name__ == '__main__': | |
with Pool(5) as p: | |
print(p.map(f, [1, 2, 3])) | |
'''=============================ignore warnings==========================''' | |
import warnings | |
with warnings.catch_warnings(): | |
warnings.simplefilter("ignore") | |
# do some operations that will throw warnings | |
'''=============================read first N lines==========================''' | |
# from a file | |
with open("file_name") as myfile: | |
head = [next(myfile) for x in range(N)] | |
'''=============================dict to object==========================''' | |
# convert | |
class objectview(object): | |
def __init__(self, d): | |
self.__dict__ = d | |
# dotdict, accessing dict item using dot.key | |
class dotdict(dict): | |
"""dot.notation access to dictionary attributes""" | |
def __getattr__(self, attr): | |
return self.get(attr) | |
__setattr__ = dict.__setitem__ | |
__delattr__ = dict.__delitem__ | |
'''=============================conditional decorator==========================''' | |
# python conditional decorator | |
class conditional_decorator(object): | |
def __init__(self, dec, condition): | |
self.decorator = dec | |
self.condition = condition | |
def __call__(self, func): | |
if not self.condition: | |
# Return the function unchanged, not decorated. | |
return func | |
return self.decorator(func) | |
# @functools.lru_cache(maxsize=256, typed=True) | |
@conditional_decorator(lambda func: functools.lru_cache(maxsize=256, typed=True)(func), hasattr(functools, 'lru_cache')) | |
def _compile_pattern(pat): | |
return re.compile(pat).match |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment