index
Python 3 教程
目录
pip install setuptools pip install wheel pip install twine pip show twine 在dist目录下生成gz文件 python3 setup.py sdist build 两种上传方式: python3 setup.py sdist upload 上传到pypi帐号 twine upload dist/* 或 python3 -m twine upload dist/* python3 -m twine upload --repository-url https://test.pypi.org/legacy/ dist/* 升级 python3 -m twine upload --skip-existing dist/*
no module named 'pip' 错误处理: py -2 -m ensurepip python -m ensurepip
python3安装
yum -y install bzip2-devel sqlite-devel openssl-devel readline-devel xz-devel xz-devel tk-devel gdbm-devel libffi-devel wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz ./configure --prefix=/usr/local/python3 ./configure --enable-shared --prefix=/usr/local/python3 //生成动态库 make && make install ln -s /usr/local/python3/bin/python3 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 ln -s /usr/local/python3/lib/libpython3.7m.so /usr/lib/libpython3.7m.so //动态库 #vim /etc/profile export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib pip3 install --upgrade pip pip3 install scrapy ln -s /usr/local/python3/bin/scrapy /usr/bin/scrapy
sqlite3安装冲突
wget https://www.sqlite.org/snapshot/sqlite-snapshot-201909031623.tar.gz ./configure --prefix=/usr/local/sqlite3 make && make install yum install sqlite-devel yum install libffi-devel yum install readline-devel yum install sqlite-devel yum install bzip2-devel yum install openssl-devel yum install gdbm-devel yum install libdbi-devel yum install ncurses-libs yum install zlib-devel yum install tkinter yum install tcl-devel tk-devel #yum install python3-tk 再安装编译python3 ln -s /usr/local/sqlite3/bin/sqlite3 /usr/bin/sqlite3 设置环境变量 echo "export LD_LIBRARY_PATH="/usr/local/sqlite3/lib"" >> /etc/bashrc 测试: python3 >import sqlite3 >sqlite3.sqlite_version
2018-03-15
Python之数据序列化(json、pickle、shelve)
>>> xInstance = X(1, 2, 3) >>> xInstance(1,2) _call_ with (1, 2)
_new,init和call,del_
__new__ 是一个静态方法,cls由解释器传值,返回参数 __init__有一个参数self,就是这个__new__返回的实例,__init__在__new__的基础上可以完成一些其它初始化的动作,__init__不需要返回值 若__new__没有正确返回当前类cls的实例,那__init__是不会被调用的,即使是父类的实例也不行 1. __init__(): 初始化某个类的一个实例。 2. __call__(): 使实例能够像函数一样被调用 3. __main__(): 命令行执行调用, 先调用__init__ 4. __str__(): print实例显示 5. __del__(): 删除实例 class X(object): def __new__(cls,*args, **kwargs): print "new %s"%cls return object.__new__(cls, *args, **kwargs) def __init__(self, a, b, range): self.a = a self.b = b self.range = range def __main__(self): print('command called!') def __call__(self, a, b): self.a = a self.b = b print('__call__ with ({}, {})'.format(self.a, self.b)) def __str__(self): print('x object') def __del__(self, a, b, range): del self.a del self.b del self.range
容器和生成器
class test(): def __init__(self,data=1): self.data = data def __next__(self): if self.data > 5: raise StopIteration else: self.data+=1 return self.data t = test(3) for i in range(3): print(t.__next__()) def fib(end = 1000): prev,curr=0,1 while curr < end: yield curr prev,curr=curr,curr+prev print(list(fib())) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
singleton
type() 不会认为子类是一种父类类型,不考虑继承关系。 isinstance() 会认为子类是一种父类类型,考虑继承关系。 不可变对象(immutable object):Number(int、float、bool、complex)、String、Tuple. 采用等效于“传引用”的方式。 可变对象(mutable object): List、dictionary.采用等效于“传值”的方式。
装饰器
代码分离,更加清晰 如果不定义wrapper, 装饰器只执行一次 def logger(fn): def wrapper(*args,**kwargs): if type(args[0]) != type(args[1]): print("类型不一致请重试") return z = fn(*args, **kwargs) f = open('C:\\Users\\Public\\Documents\\test.txt', 'w') f.write(str(z)) f.close() print("finish") return wrapper @logger # add1 = logger(add1) def add(x,y): ret = x + y return ret add(12,56)
类变量、实例变量、类方法、实例方法
class Foo: class_var = '' #类变量,相当于静态变量 def __init__(self): self._age = 0 self.instance_var = '' #实例变量 def __main__(self): #命令行调用python foo.py print('usage command line!') @classmethod def class_method(cls,parameter):#相当于静态类,cls代表类 print(cls.instance_var) print(class_var) @staticmethod def statis_method(paramter): print(self.instance_var) #不能访问 print(class_var) #不能访问 def method(self,parameter): #实例类,self代表当前类实例 print(Object.class_var) print(self,instance_var) @property def age(self): return self._age; @age.setter def age(self, value): if isinstance(value,str): self._age = Decimal(value) if isinstance(value, Decimal): self._age = value @age.deleter def age(self): raise AttributeError("cannot delete attribute") foo = Foo() print(foo.age) foo.age = 12 del foo.age class A: v = 22; a = A() a.v = 13 print(A.v) #22 print(a.v) #13 del a.v print(a.v) #22
xpath
from lxml import etree import requests html = requests.get('http://www.lzu.edu.cn').content.decode('utf-8') dom_tree = etree.HTML(html) links = doc_tree.xpath('//img/@src') for item in links: print(item) //代表相对路径,匹配可以是任意深度的节点 / 代表绝对路径,故对于网页来说,匹配从/html开始 “/html/body/div[@id='main']” div[text()='hello'] "li[2]" 表示取得第二个li节点 ”/html/body/*/span” “*”可以代替所有的节点名 “following-sibling::ul” 就是下一个ul节点
同时装Python3和Python2
CentOS7安装pip 1.yum -y install epel-release 2.yum install python-pip 3.pip install --upgrade pip ***for windows*** py -2 hello.py py -3 hello.py py -2 -m pip install XXXX py -3 -m pip install XXXX #! python2 # coding: utf-8 #! python2 #! python3 分别表示该代码文件第一行使用Python2或者Python3解释运行 py hello.py 直接运行 ***for linux*** 如果仅安装 python2 pip install sth # 如果安装有 python3, 则需要明确 pip 版本 py -2 -m pip install sth py -3 -m pip install sth
install pip for python3.x
1.首先安装setuptools wget --no-check-certificate https://pypi.python.org/packages/72/c2/c09362ab29338413ab687b47dab03bab4a792e2bbb727a1eb5e0a88e3b86/setuptools-39.0.1.zip#md5=75310b72ca0ab4e673bf7679f69d7a62 tar -zxvf setuptools-19.6.tar.gz cd setuptools-19.6.tar.gz python3 setup.py build python3 setup.py install 2.安装pip wget --no-check-certificate https://pypi.python.org/packages/c4/44/e6b8056b6c8f2bfd1445cc9990f478930d8e3459e9dbf5b8e2d2922d64d3/pip-9.0.3.tar.gz#md5=b15b33f9aad61f88d0f8c866d16c55d8 tar -zxvf pip-8.0.2.tar.gz cd pip-8.0.2 python3 setup.py build python3 setup.py install
显示双缓冲
def OnPaint(self, event): if _plat.startswith('linux'): dc = wx.PaintDC(self) dc.Clear() elif _plat.startswith('darwin'): pass elif _plat.startswith('win'): if USE_BUFFERED_DC: dc = wx.BufferedPaintDC(self, self._Buffer) else: dc = wx.PaintDC(self) dc.DrawBitmap(self._Buffer, 0, 0) dc.Clear()