.py
文件的第一行加上一个特殊的注释: 1 #!/usr/bin/env python3 2 3 print('hello, world')
然后,通过命令给hello.py
以执行权限:
$ chmod a+x hello.py
1 { 2 "cmd": ["/usr/local/bin/python3.5", "-u", "$file"], 3 "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", 4 "selector": "source.python" 5 }
1 { 2 "command": "repl_open", 3 "caption": "Python", 4 "id": "repl_python", 5 "mnemonic": "P", 6 "args": { 7 "type": "subprocess", 8 "encoding": "utf8", 9 //"cmd": ["python", "-i", "-u"],原来是这个 10 "cmd": ["/usr/local/bin/python3.5", "-i", "-u"], 11 "cwd": "$file_path", 12 "syntax": "Packages/Python/Python.tmLanguage", 13 "external_id": "python", 14 "extend_env": {"PYTHONIOENCODING": "utf-8"} 15 } 16 }
这里只对Python这个选项进行了cmd设置,可以依次在下方的所有children都设置成Python3.5版本
pip install --install-option="--prefix=/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages" lxml
iconv -t utf-8 index.html > index_.html
默认参数一定要用不可变对象,如果是可变对象,程序运行时会有逻辑错误!
要注意定义可变参数和关键字参数的语法:
*ar
gs
是可变参数,args接收的是一个tuple;
**
kw
是关键字参数,kw接收的是一个dict。
1 L = list(range(100)) 2 print(L[0:10]) 3 print(L[-10:]) 4 L = 'asdfgh' 5 print(L[::2])
fo
r
循环的对象都是Iter
able
类型;凡是可作用于nex
t()
函数的对象都是Ite
rator
类型,它们表示一个惰性计算的序列;
Iterable
通过iter()函数转换成Iterator
@property
和@xxx.setter
可以方便地设置get方法与set方法
1 class Screen(object): 2 @property 3 def width(self): 4 return self._width 5 6 @width.setter 7 def width(self, value): 8 self._width = value
pickle
或者json
实现序列化与反序列化
1 import pickle 2 d = dict(name='Bob', age=20, score=88) 3 f = open('/Users/JackMa/Desktop/Python/test3.py', 'wb') 4 pickle.dump(d, f) #序列化 5 f.close() 6 7 f = open('/Users/JackMa/Desktop/Python/test3.py', 'rb') 8 m = pickle.load(f) #反序列化 9 print(m) 10 f.close()
1 import json 2 3 print(json.dumps(d)) 4 json_str = '{"age": 20, "score": 88, "name": "Bob"}' 5 print(json.loads(json_str)) 6 7 class Student(object): 8 def __init__(self, name, age, score): 9 self.name = name 10 self.age = age 11 self.score = score 12 13 #对类的序列化用到下面的方法 14 def student2dict(std): 15 return { 16 'name': std.name, 17 'age': std.age, 18 'score': std.score 19 } 20 21 #对类的反序列化用到下面的方法 22 def dict2student(d): 23 return Student(d['name'], d['age'], d['score']) 24 25 s = Student('Bob', 20, 88) 26 #下面两种方法都可以序列化 27 print(json.dumps(s, default=student2dict)) 28 print(json.dumps(s, default=lambda obj:obj.__dict__)) 29 json_str = '{"age": 20, "score": 88, "name": "Bob"}' 30 print(json.loads(json_str, object_hook = dict2student)
头文件导入threading
创建新线程:t = threading.Thread()
线程开始:t.start()
等待线程执行完毕:t.join()
获取当前线程:threading.current_thread()
建立线程锁:lock = threading.Lock()
获得线程锁:lock.acquire()
释放线程锁:lock.release()
创建线程内的全局ThreadLocal对象:local = threading.local() ThreadLocal
最常用的地方就是为每个线程绑定一个数据库连接,HTTP请求,用户身份信息等,这样一个线程的所有调用到的处理函数都可以非常方便地访问这些资源。
1 import time, threading 2 3 local = threading.local() 4 5 def process_student(): 6 std = local.age 7 print('%s in %s' % (std, threading.current_thread().name)) 8 9 def process_thread(temp): 10 local.age = temp 11 process_student() 12 13 t1 = threading.Thread(target=process_thread, args=(12, )) 14 t2 = threading.Thread(target=process_thread, args=(123, )) 15 t1.start() 16 t2.start()