随笔分类 -  Python

关于Python相关知识的
python对象判断isintance/is等
摘要:isintance/type 的区别尽量使用isintance 内置的 type() 函数可以用来查询变量所指的对象类型。isinstance(1, int) 区别: class A: pass class B(A): pass isinstance(A(), A) # returns True t 阅读全文

posted @ 2021-01-04 23:30 思此狂 阅读(246) 评论(0) 推荐(0)

上下文管理contextlib
摘要:contextmanager from contextlib import contextmanager class Query(object): def __init__(self, name): self.name = name def query(self): print('Query inf 阅读全文

posted @ 2021-01-03 22:55 思此狂 阅读(79) 评论(0) 推荐(0)

让对象支持上下文管理
摘要:让我们的对象支持上下文管理(with语句) 为了让一个对象兼容 with 语句,你需要实现 __enter__() 和 __exit__() 方法 demo: class WithTest: def __enter__(self): print("enter") return self #这里需要r 阅读全文

posted @ 2021-01-02 21:16 思此狂 阅读(90) 评论(0) 推荐(0)

threading Event
摘要:通过threading.Event()可以创建一个事件管理标志,该标志(event)默认为False,event对象主要有四种方法可以调用: event.wait(timeout=None):调用该方法的线程会被阻塞,如果设置了timeout参数,超时后,线程会停止阻塞继续执行; event.set 阅读全文

posted @ 2020-12-19 22:48 思此狂 阅读(641) 评论(0) 推荐(0)

python中的eval 和 exec 和 execfile
摘要:#eval 函数用来计算python表达式并返回结果#exec 可以执行动态的python代码,不返回结果 def hello(): print("hello world") eval("hello()") 输出 hello world exec("hello()") 输出 hello world# 阅读全文

posted @ 2020-11-27 22:12 思此狂 阅读(344) 评论(0) 推荐(0)

python导入py文件模块
摘要:有时我们写了一些lib文件,想作为模块导入引用 python import导入模块时搜索模块文件路径在 sys.path 在root下执行的 t.py (print sys.path) ['/root', '/usr/lib64/python27.zip', '/usr/lib64/python2. 阅读全文

posted @ 2020-11-14 23:51 思此狂 阅读(1773) 评论(0) 推荐(0)

python堆栈追踪异常
摘要:以前常用的写法 try: print(a) except Exception, e: print("err exception is %s" % e) 输出 err exception is name 'a' is not defined 异常打印信息不明显 推荐 traceback 来追踪异常 i 阅读全文

posted @ 2020-11-14 23:44 思此狂 阅读(212) 评论(0) 推荐(0)

python-http-server
摘要:# -*- coding: UTF-8 -*- import time import os import sys import urllib from BaseHTTPServer import (HTTPServer, BaseHTTPRequestHandler) def close_std_f 阅读全文

posted @ 2020-10-11 23:55 思此狂 阅读(546) 评论(0) 推荐(0)

python的文件锁操作
摘要:相关链接: https://cloud.tencent.com/developer/section/1372550 import fcntl import time import sys class TestLock: def __init__(self, filename): self.filen 阅读全文

posted @ 2020-10-05 12:59 思此狂 阅读(410) 评论(0) 推荐(0)

关于 python 的类
摘要:查看类的属性 用 dir(Classname) 仅返回对象的属性的一个名字列表和 classname.__dict__ 返回一个字典,他的key是属性名, 键值是相应的属性对象的数据值 关于 __name__ #test15.pyclass Stuent(): def __init__(self,n 阅读全文

posted @ 2020-09-27 23:50 思此狂 阅读(94) 评论(0) 推荐(0)

jnija2模板渲染
摘要:cat temp.temp hello {{ name }} {% for love in love_sports %} my loves sport is {{ love }} {% endfor %} {% if is_boy %} i am a good boy {% else %} i am 阅读全文

posted @ 2020-09-25 16:39 思此狂 阅读(181) 评论(0) 推荐(0)

python multiprocessing
摘要:from multiprocessing import Process import os # 子进程要执行的代码 def run_proc(name): print('Run child process %s (%s)...' % (name, os.getpid())) if __name__= 阅读全文

posted @ 2020-09-15 17:38 思此狂 阅读(298) 评论(0) 推荐(0)

python 守护进程daemon
摘要:import time import os import sys def close_std_fd(): f = open(os.devnull, 'w') sys.stdin = f sys.stdout = f sys.stderr = f def daemon(func): pid = os. 阅读全文

posted @ 2020-09-14 16:05 思此狂 阅读(474) 评论(0) 推荐(0)

python bs4
摘要:# -*- coding: UTF-8 -*- #爬虫 import urllib2 #import bs4 import re import sys from bs4 import BeautifulSoup # import time reload(sys) sys.setdefaultenco 阅读全文

posted @ 2020-06-04 00:12 思此狂 阅读(136) 评论(0) 推荐(0)

python 队列Queue
摘要:from Queue import Queue qlist=Queue(maxsize=300) import threading for i in range(20): qlist.put("hello num%d" %(i)) def process_work(qlist): while Tru 阅读全文

posted @ 2020-05-27 23:45 思此狂 阅读(196) 评论(0) 推荐(0)

python 抛出捕获异常
摘要:res_json={ "id": 123456, "ip": "1.2.3.4" } res_str="vpc out limited" def return_val(): return res_str def create_vm(): try: response = return_val() re 阅读全文

posted @ 2020-05-27 22:20 思此狂 阅读(480) 评论(0) 推荐(0)

python 装饰器
摘要:#函数返回值+1 def make(func): def wrapper(*args,**kwds): return func(*args,**kwds) + 1 return wrapper @make def add(): return 4 print add() #输出5 #函数返回值+N d 阅读全文

posted @ 2020-05-22 20:47 思此狂 阅读(154) 评论(0) 推荐(0)

python urllib2
摘要:抓取网页图片保存本地 ,注意 open文件方式为 wb ,二进制形式打开文件。 阅读全文

posted @ 2019-11-18 17:08 思此狂 阅读(151) 评论(0) 推荐(0)

python 正则
摘要:import re #re.match(pattern, string, flags=0)#flag 标志位,常用的re.I 忽略大小写匹配#re.findall(r"jink",text,re.I) text='''11helloworld22nimenhaomashanxishengweinan 阅读全文

posted @ 2019-11-15 17:15 思此狂 阅读(217) 评论(0) 推荐(0)

Python collections
摘要:参考链接 https://docs.python.org/zh-cn/3/library/collections.html#collections.namedtuple collections是Python内建的一个集合模块,提供了许多有用的集合类。这个模块实现了特定目标的容器,以提供Python标 阅读全文

posted @ 2019-08-04 13:37 思此狂 阅读(215) 评论(0) 推荐(0)

导航