导图社区 python语法
Python解释器易于扩展,可以使用C或C++(或者其他可以通过C调用的语言)扩展新的功能和数据类型。 Python 也可用于可定制化软件中的扩展程序语言。Python丰富的标准库,提供了适用于各个主要系统平台的源码或机器码。
编辑于2021-04-26 14:34:07python
语法 >
...
换行
()
\ 字符串内换行
操作符
+ * [] [::] in
...
input
print(string,sep=' ',end='\n')
.
列表 字典 集合 × 推导式
.
123_456_789
.
__weakref__
__dict__ = None
__spec__
..
.
get - set 类型检查 - ...
语言特性
迭代器
Iterator
Iterable
__iter__() - __getitem__()
__iter__() - __reversed__()
iter() - reversed()
Iterator
next(iterable,None)
__next__
StopIteration
生成器 generator
yield
yield from
__getitem__()
String
zip() - chain()
itertools
islice
start,end - None -1×
chain
装饰器
decorator
例
备忘录
保存元数据
__name__
__doc__
__module__
__defaults__
__closure__ -> cell
[0].cell_contents
@typeassert
...
functools
@wraps(func) - update_wrapper(wrapper,func,(替换),(更新:dict))
wrapper_assignments - wrapper_updates
module name doc - dict
..
@
decorator
wrapper
classmethod staticmethod
带参 - 装饰器工厂
foo(args)(func)
可修改 - wrapper.set()
nonlocal
python2 - [] ???
* ** zip()
版本新特性
:=
.
@ __loader__ >
.
Meta path import for built-in modules.
All methods are either class or static methods to avoid the need to instantiate the class.
create_module
exec_module
find_module
find_spec
get_code
get_source
is_package
load_module
module_repr -> inspect
mro
assert ,
x if t1 else y if t2 else z
标准库 >
@ 内置 >
函数 >
help
.
str repr
ascii
ord - chr
memoryview
bytes bytearray (,encoding)
format
.
hash id
.
int float complex
bin oct hex
bool
all any
list tuple dict
set frozenset
.
max min sum(l,start)
pow abs divmod round
input print(,/,sep,end) open
.
breakpoint
compile
eval - exec : (,g,l)
__import__
迭代
len
.
range
enumerate
zip - zip(*)
[slice]
sorted
(,key=func,reverse)
.
iter
__iter__() - __getitem()__
reversed
next
StopIteration
filter map reduce
(func,iter)
reduce - functools
@
staticmethod
classmethod
property
.
@property
def x()
@x.setter
@x.deleter
.
property(fget,fset,fdel,doc)
.
.
hasattr
getattr
setattr
delattr
callable
isinstance - issubclass
.
locals globals vars dir
.
super
object
type
(className,(),{})
异常 >
BaseException
Exception
ArithmeticError
FloatingPointError
OverflowError
ZeroDivisionError
AssertionError
LookupError
IndexError
KeyError
RuntimeError
NotImplementedError
RecursionError
OSError
< 3.3
EnvironmentError
IOError
WindowsError
SyntaxError
IndentationError
TabError
SystemError
ImportError
ModuleNotFoundError
NameError
ReferenceError
TypeError
AttributeError
ValueError
UnicodeError
UnicodeDecodeError
UnicodeEncodeError
UnicodeTranslateError
StatisticsError
Warning
DeprecationWarning
PendingDeprecationWarning
UserWarning
.
keyword >
kwlist
iskeyword()
constants >
True False
None
NotImplemented
Ellipsis
__debug__
quit
exit
copyright
credits
license
.
py_compile >
compile()
compileall >
compile_dir()
文本
string >
capwords() - ' '
Template
("$var $$")
substitute({})
safe_substitute() - 保留$var
"%(var) %%" % {}
"{var} {{}}".format(**{})
(Template)
delimiter
idpattern - pattern
Formatter - format()
const
printable
ascii_letters
ascii_lowercase
ascii_uppercase
digits
hexdigits
octdigits
punctuation
whitespace
textwrap >
fill(s,width,/,initial_indent,subsequent_indent)
dedent() - min
indent(,prefix,predicate)
shorten(,width,placeholder="[...]")
@ re >
func
compile
match
search
findall
sub
subn
split
特殊字符
. \d\D \s\S \w\W
^ $ \A \Z
* + ?{m,n}
[] |
()
\1
?
aiLmsux
ascll
ignorecase
locale
multiline
dotall
verbose
:
< = !
#
P <>
中文 - [\u4e00-\u9fa5]
双字节 - [^\x00-\xff]
difflib >
url: https://docs.python.org/zh-cn/3/library/difflib.html#module-difflib .
class
SequenceMatcher
(isjunk,a,b,autojunk)
.
set_seqs()
set_seq1()
set_seq2()
find_longest_match(,,,,)
get_matching_blocks()
.
get_opcodes()
replace delete insert equal
get_grouped_opcodes(n)
.
ratio()
quick_ratio()
real_quick_ratio()
Differ
(linejunk,charjunk)
compare(,)
- + ' ' ?
HtmlDiff
make_file()
make_table()
func
.
context_diff()
get_close_matches(,l,/,n,cutoff)
ndiff()
restore(,1/2)
unified_diff()
diff_bytes()
bool
IS_LINE_JUNK()
IS_CHARACTER_JUNK()
数据结构
enum >
Enum
类变量 - 实例
.
.key
[key]
(value)
.
name
value
int
(,)
{}
.
for in
__members__
_generate_next_value_(name,start,count,last_values)
@unique
Enum(,'x x')
.
(IntEnum) - 有序
Flag
IntFlag
auto()
@ collections
ChainMap >
maps -> [{:},]
new_child() - 前插入
Counter >
([]/{:})
update()
elements() - 无序
most_common(n)
+ - & |
defaultdict >
(func,{:})
deque >
(maxlen=)
appendleft() extendleft() popleft()
rotate() 负左
namedtuple >
(className,'. .',/,rename)
_fields - rename to _0
_asdict() -> OrderedDict
_replace(key=value)
OrderedDict >
3.x+ dict 顺序确定
==
move_to_end(key,last)
abc >
Container
in
Hashable
Iterable
Iterator
Generator
Sized
Callable
Sequence
MutableSequence
ByteString
Set
MutableSet
Mapping
MutableMapping
ItemsView
KeysView
ValuesView
Awaitable
Coroutine
AsyncIterable
AsyncIterator
array >
array('',data)
tofile()
heapq >
[]
.
heappush()
.
heappop()
heapreplace()
heapify()
(n,) 1..n
nlargest()
nsmallest()
merge() -> 有序
bisect >
bisect() bisect_left()
insort() insort_left() : insertion
queue >
url: https://docs.python.org/zh-cn/3/library/queue.html#module-queue .
class
Queue
method
qsize()
empty()
full()
put()
put_nowait()
get()
get_nowait()
task_done()
join()
SimpleQueue
...
LifoQueue
PriorityQueue
Empty
Full
struct >
unpack('',bytes)
h - short
>h 大端法
i - int
weakref >
url: https://docs.python.org/zh-cn/3/library/weakref.html#module-weakref .
class
WeakKeyDictionary
keyrefs()
WeakValueDictionary
valuerefs()
WeakSet()
WeakMethod()
finalize()
method
__call_()
detach()
peek()
attr
alive
atexit
.
ReferenceType
ProxyType
CallableProxyType
ProxyTypes
func
getweakrefcount()
getweakrefs()
.
ref(,callback) - proxy
copy >
url: https://docs.python.org/zh-cn/3/library/copy.html#module-copy .
func
copy()
deepcopy()
pprint >
url: https://docs.python.org/zh-cn/3/library/pprint.html#module-pprint .
class
PrettyPrinter
method
pformat()
pprint()
isreadable()
isrecursive()
format()
func
pformat()
pp()
pprint()
isreadable()
isrecursive()
saferepr()
函数式
@ functools >
_gt_from_lt(self,other,NotImplemented = NotImplemented)
_convert = {a:[(b,func),],}
total_ordering
cmp_to_key(mycmp)
class K
return K
class partial
lru_cache()
partial(func,...)
@ itertools >
func
tee
class
accumulate
chain - cycle - repeat
.
combination - combinations_with_replacement
permutations
product
.
dropwhile - takewhile
filterfalse - compress
.
groupby
.
islice
.
zip_longest
.
count
startmap
_grouper
_tee
_tee_dataobject
@ operator >
url: https://docs.python.org/zh-cn/3/library/operator.html#module-operator .
< <= == != > >=
lt()
le()
eq()
ne()
gt()
ge()
bool
not()
__not__()
truth()
is_()
is_not()
. i-
+ -
add()
sub()
* @ **
mul()
matmul()
pow()
/ // %
truediv()
floordiv()
mod()
abs()
& | ^
and()
or_()
xor()
<< >>
lshift()
rshift()
+ -
neg()
pos()
.
index()
__index__()
inv()
__inv__()
invert()
__invert__()
seq
concat()
__concat__()
contains()
__contains__()
countOf()
__countOf__()
indexOf()
__indexOf__()
length_hint()
.
delitem()
__delitem__()
getitem()
__getitem__()
setitem()
__setitem__()
.
attrgetter()
itemgetter()
methodcaller()
@ contextlib >
url: https://github.com/python/cpython/tree/3.9/Lib/contextlib.py .
class
AbstractContextManager
AbstractAsyncContextManager
ContextDecorator
ExitStack
AsyncExitStack
@
contextmanager
asynccontextmanager
func
closing()
nullcontext()
suppress()
redirect_stdout()
redirect_stderr()
时间
time >
num
time() - 1970 1 1 0:0
monotonic() - 单调 起点未定义
clock() - 处理器
perf_counter() - 高精度
num struct_time str
.
gmtime() - UTC
localtime()
.
os.environ['TZ']
tzname timezone daylight:01
tm_
year mon yday mday wday hour min sec isdst
s_t -> num -> str
mktime()
ctime()
str -> s_t -> str
strptime()
strftime()
@ datetime >
.
MINYEAR 1
MAXYEAR 9999
object
timedelta ()
class
min max # max > - min
resolution - 1 microsecond
instance
days
seconds
microseconds
totol_seconds()
date (year,month,day)
constructor
today()
from timestamp(timestamp)
from ordinal(ordinal)
from iso format(date_string) -
YYYY-MM-DD
from iso calendar(year,week,day)
class
min max
resolution - 1 day
instance
year
month
day
replace() - set'
timetuple()
to ordinal()
weekday()
iso weekday()
iso calendar()
iso format()
ctime()
str f time()
datetime
time ()
tzinfo
timezone
calendar >
url: https://docs.python.org/zh-cn/3/library/calendar.html#module-calendar .
class
Calendar
.
iter-
weekdays()
month-
dates()
days()
days2()
days3()
days4()
month/year-dates/days/days2-calendar()
TextCalendar()
formatmonth()
formatyear()
prmonth()
pryear()
HTMLCalendar()
.
cssclasses
cssclass_noday
cssclasses_weekday_head
cssclass_month/year/ year_head
LocaleTextCalendar()
LocaleHTMLCalendar()
func
setfirstweekday()
firstweekday()
isleap()
leapdays()
weekday()
weekheader()
monthrange()
monthcalendar()
prmonth()
month()
prcal()
calendar()
timegm()
.
day_name
day_abbr
month_name
month_abbr
数学
decimal >
Decimal
(''/f/(s,(),e))
from_float()
int √ float ×
'Infinity' 'NaN' '0' '-...'
上下文
context = getcontext()
with localcontext()
c.copy()
setcontext(c) - 线程
c
attrs
Emax Emin
capitals
prec - 1-MAX_PREC
rounding
ROUND_
ceiling floor
half_even
down - up : 0
half_down
half_up
05up
flags - traps {}
create_decimal()
fractions >
url: https://docs.python.org/zh-cn/3/library/fractions.html#module-fractions .
Fraction
()
numerator=0 denominator=1
fraction
float/decimal/string
attr
numerator
denominator
method
self
as_integer_ratio()
limit_denominator()
magic
__floor__()
__ceil__()
__round__()
__round__(n)
static
from_float()
from_decimal()
random >
url: https://docs.python.org/zh-cn/3/library/random.html#module-random random
:
seed()
state = getstate()
setstate(state)
.
randbytes(n) :
.
randrange()
stop - 0 stop 1
start stop step
randint(a,b) : [a,b]
getrandbits() :
seq
choice()
choices() :
shuffle()
sample(,k)
分布
.
random() [0.0,1.0)
uniform(a,b) [a,b]/[b,a]
triangular(low,high,mode) :
gauss(mu,sigma)
-variate
beta(alpha,beta) :
expo(lambd)
gamma(alpha,beta) :
lognorm() :
normal() - gauss :
vonmises(mu,kappa) :
pareto(alpha) :
weibull(alpha,beta) :
.
Random() :
SystemRandom() :
math >
const
pi
e
isfinite()
nan
isnan()
inf
.
isclose(,,rel_tol=,abs_tol=)
.
trunc()
floor()
cell()
.
整数部分,小数部分 = modf()
.
m,e = frexp()
ldexp(m,e)
.
fabs()
copysign(,sign)
.
factorial()
1.0 √
1.1 ×
gamma(x) -> (x-1)!
lgamma() - 更精确
.
sum()
fsum() - 精确
符号 ???
%
fmod()
.
gcd()
指对数
指数
**
pow() - callback
sqrt()
exp()
对数
log(,e)
log10()
log2()
log1p() - 1 plus
expm1() - minus 1
三角
.
radians()
degrees()
sin() cos() tan()
arcsine() arccosine() arctangent()
sinh() cosh() tanh()
asinh() acosh() atanh()
c = hypot(a,b)
高斯误差函数
erf()
erfc() = 1 - erf()
statistics >
url: https://docs.python.org/zh-cn/3/library/statistics.html#module-statistics statistics
.
.
mean()
fmean()
geometric_mean()
harmonic_mean()
.
median()
median_low()
median_high() :
median_grouped()
.
mode()
multimode()
quantiles() :
:
.
pstdev()
pvariance()
.
stdev()
variance()
StatisticsError
NormalDist :
系统
os.path >
abspath()
basename()
commonpath()
commonprefix()
dirname()
exists()
lexists()
.
expanduser()
expandvars()
.
getatime()
getmtime()
getctime()
getsize()
bool
isabs()
isfile()
isdir()
islink()
ismount()
join()
normcase()
normpath()
realpath()
relpath()
samefile()
sameopenfile()
samestat()
split()
splitdrive()
splitext()
supports_unicode_filenames()
pathlib >
url: https://docs.python.org/zh-cn/3/library/pathlib.html#methods Path
PurePath
PureWindowsPath
WindowsPath
PurePosixPath
PosixPath
Path
('')
iterdir()
... / '' / ''
resolve()
open()
bool
exists()
is_dir()
init
('',...,'')
(p1,p2)
() - '.'
.
parts - (,)
root - parents - parent
anchor :
drive
name
stem :
suffix
suffixes
.
as_posix() : '\' -> '/'
as_uri()
bool
is_absolute()
is_relative_to() :
is_reserved()
nul - windows
.
joinpath()
match() : 通配符
relative_to()
改
with_name()
with_stem()
with_suffix()
glob >
glob()
list(iglob)
iglob()
_iglob
_iglob()
dirname,basename = os.path.split()
glob_in_dir(dirname,basename,dironly) = _glob1,_glob2
glob0()
_glob0()
glob1()
_glob1()
_glob2()
_iterdir()
os.scandir()
_ishidden()
has_magic()
'([*?[])'
fnmatch >
fnmatch()
fnmatchcase()
filter()
translate()
linecache >
getline()
clearcache()
checkcache()
lazycache()
tempfile >
class
TemporaryFile
NamedTemporaryFile
SpooledTemporaryFile
TemporaryDirectory
func
mkstemp()
mkdtemp()
gettempdir()
gettempdirb()
gettempprefix()
gettempprefixb()
.
tempdir
shutil >
copyfileobj()
copyfile()
copymode()
copystat()
copy()
copy2()
ignore_patterns()
copytree()
rmtree()
move()
disk_usage()
chown()
which()
.
make_archive()
get_archive_formats()
register_archive_format()
unregister_archive_format()
unpack_archive()
register_unpack_archive()
unregister_unpack_archive()
get_unpack_formats()
get_terminal_size()
filecmp >
func
cmp()
cmpfiles()
clear_cache()
class
dircmp
method
report()
report_partial_closure()
report_full_closure()
attr
left
right
left_list
right_list
common
left_only
right_only
common_dirs
common_files
common_funny
same_files
diff_files
funny_files
subdirs
const
default_ignores
mmap >
mmap
method
close()
.
find()
rfind()
flush()
madvise()
move()
seek()
tell()
.
size()
resize()
.
read()
read_byte()
readline()
write()
write_byte()
attr
closed
const
madv_
normal
random
sequential
willneed
dontneed
remove
dontfork
dofork
hwpoison
mergeable
unmergeable
soft_offline
hugepage
nohugepage
dontdump
dodump
free
nosync
autosync
nocore
core
protect
codecs >
url: https://docs.python.org/zh-cn/3/library/codecs.html#module-codecs .
.
encode()
decode()
.
getencoder()
getdecoder()
getincrementalencoder()
getincrementaldecoder()
getreader()
getwriter()
.
lookup()
.
lookup_error()
strict_errors()
replace_errors()
ignore_errors()
xmlcharrefreplace_errors()
backslashreplace_errors()
namereplace_errors()
.
register()
open()
.
iterencode()
iterdecode()
class
EncodedFile
CodecInfo
attr
name
encode
decode
incrementalencoder
incrementaldecoder
streamwriter
streamreader
IncrementalEncoder
encode()
reset()
getstate()
setstate()
IncrementalDecoder
decode()
reset()
getstate()
setstate()
StreamWriter()
write()
writelines()
reset()
StreamReader()
read()
readline()
readlines()
reset()
SteamReaderWriter()
StreamRecoder
const
bom
bom_
be
le
utf8
utf16
utf16_be
utf16_le
utf32
utf32_be
utf32_le
io >
url: https://docs.python.org/zh-cn/3/library/io.html#module-io .
class
.
StringIO
BytesIO
FileIO
.
RawIOBase
IOBase
func
close()
fileno()
flush()
.
readline()
readlines()
writelines()
.
seek()
tell()
truncate()
bool
isatty()
readable()
seekable()
writeable()
attr
closed
BufferedIOBase
.
TextIOBase
TextIOWrapper
line_buffering
write_through
reconfigure()
func
open()
open_code()
exception
BlockingIOError
UnsupportedOperation
const
default_buffer_size
压缩
zlib >
url: https://docs.python.org/zh-cn/3/library/zlib.html#module-zlib .
adler32()
compress()
compressobj()
crc32()
decompress()
decompressobj()
Compress
compress()
flush()
copy()
Decompress
attr
unused_data
unconsumed_tail
eof
method
decompress()
flush()
copy()
const
zlib_version
zlib_runtime_version
gzip >
url: https://docs.python.org/zh-cn/3/library/gzip.html#module-gzip .
func
open()
compress()
decompress()
class
GzipFile
peek()
bz2 >
url: https://docs.python.org/zh-cn/3/library/bz2.html#module-bz2 .
func
open()
compress()
decompress()
class
BZ2File
peek()
BZ2Compressor
compress()
flush()
BZ2Decompressor
method
decompress()
attr
eof
unused_data
needs_input
zipfile >
url: https://docs.python.org/zh-cn/3/library/zipfile.html#module-zipfile .
class
ZipFile
method
close()
getinfo()
infolist()
namelist()
open()
extract()
extractall()
printdir()
setpassword()
read()
testzip()
write()
writestr()
attr
filename
debug
comment
Path
attr
name
method
open()
iterdir()
is_dir()
is_file()
exists()
read_text()
read_bytes()
PyZipFile
writepy()
ZipInfo
method
is_dir()
attr
filename
date_time
compress_type
comment
extra
create_system
create_version
extract_version
reserved
flag_bits
volume
internal_attr
external_attr
header_offset
CRC
compress_size
file_size
func
bool
is_zipfile()
const
zip_stored
zip_deflated
zip_bzip2
zip_lzma
tarfile >
url: https://docs.python.org/zh-cn/3/library/tarfile.html#module-tarfile .
func
open()
is_tarfile()
exception
TarError
ReadError
CompressionError
StreamError
ExtractError
HeaderError
const
encoding
ustar_format
gnu_format
pax_format
default_format
class
TarFile
method
getmember()
getmembers()
getnames()
list()
next()
extractall()
extract()
extractfile()
add()
addfile()
gettarinfo()
close()
attr
pax_headers
TarInfo
classmethod
frombuf()
fromtarfile()
method
tobuf()
bool
isfile()
isreg()
isdir()
issym()
islnk()
ischr()
isblk()
isfifo()
isdev()
attr
name
size
mtime
mode
type
linkname
uid
gid
uname
gname
pax_headers
加密
hashlib >
const - lower
algorithms_guaranteed
algorithms_available
class
hash
attr
digest_size
block_size
name
method
update()
digest()
hexdigest()
copy()
shake
digest()
hexdigest()
blake2b
const
salt_size
person_size
max_key_size
max_digest_size
blake2s
const
...
func
pbkdf2_hmac()
scrypt()
hmac >
new()
digest()
HMAC
attr
digest_size
block_size
name
method
update()
digest()
hexdigest()
copy()
compare_digest()
secrets >
choice()
randbelow()
randbits()
token_bytes()
token_hex()
token_urlsafe()
compare_digest()
导入
importlib >
url: https://docs.python.org/zh-cn/3/library/importlib.html#module-importlib .
metadata >
pkgutil >
class
ModuleInfo
ImpImporter
ImpLoader
func
extend_path()
find_loader()
get_importer()
get_loader()
iter_importers()
iter_modules()
walk_packages()
get_data()
resolve_name()
modulefinder >
func
AddPackagePath()
ReplacePackage()
class
ModuleFinder
modules
report()
run_script()
runpy >
run_module()
run_path()
zipimport >
zipimporter
attr
archive
prefix
method
find_module()
get_code()
get_data()
get_filename()
get_source()
is_package()
load_module()
进程
subprocess >
func
run()
call()
check_call()
check_output()
class
CompletedProcess
args
returncode
stdout
stderr
check_returncode()
Popen
poll()
wait()
communicate()
send_signal()
terminate()
kill()
args
stdin
stdout
stderr
pid
returncode
STARTUPINFO
dwFlags
hStdInput
hStdOutput
hStdError
wShowWindow
lpAttributeList
const
devnull
pipe
stdout
std_input_handle
std_output_handle
std_error_handle
sw_hide
startf_use std handles
startf_use show window
create_new_console
create_new_process_group
above_normal_priority_class
below_normal_priority_class
high_priority_class
idle_priority_class
normal_priority_class
realtime_priority_class
create_no_window
detached_process
create_default_error_mode
create_breakaway_from_job
exception
TimeoutExpired
cmd
timeout
output
stdout
stderr
CalledProcessError
returncode
cmd
output
stdout
stderr
signal >
threading >
func
active_count()
current_thread()
excepthook()
get_ident()
get_native_id()
enumerate()
main_thread()
settrace()
setprofile()
stack_size()
const
timeout_max
class
Thread
(target,args,kwargs,daemon)
method
.
start()
run()
join()
bool
is_alive()
attr
ident
native_id
daemon
isDaemon()
setDaemon()
name
getName()
setName()
Lock
flag = acquire()
release()
locked()
RLock
acquire()
release()
Condition
acquire()
release()
wait()
wait_for()
notify()
notify_all()
Semaphore
acquire()
release()
BoundedSemaphore
Event
is_set()
set()
clear()
wait()
Timer
cancel()
Barrier
wait()
reset()
abort()
parties
n_waiting
broken
multiprocessing >
url: https://docs.python.org/zh-cn/3/library/multiprocessing.html#module-multiprocessing .
class
Process
(target,args,kwargs)
start()
daemon - 守护 : 主结束 子销毁
Queue
SimpleQueue
Pipe
Lock
Value
Array
Manager
Pool
... -> threading
func
active_children()
cpu_count()
current_process()
parent_process()
freeze_support()
get_all_start_methods()
get_context()
get_start_method()
set_executable()
set_start_method()
asyncio >
concurrent.futures >
url: https://docs.python.org/zh-cn/3/library/concurrent.futures.html#module-concurrent.futures .
class
Executor
submit()
map()
shutdown()
ThreadPoolExecutor
ProcessPoolExecutor
Future
cancel()
cancelled()
running()
result()
exception()
add_done_callback()
set_running_or_notify_cancel()
set_result()
set_exception()
func
wait()
exception
CancelledError
TimeoutError
BrokenExecutor
InvaildStateError
BrokenThreadPool
BrokenProcessPool
---
.
pickle
shelve
dbm
sqlite3
xml.etree.ElementTree >
url: https://docs.python.org/zh-cn/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree .
csv >
url: https://docs.python.org/zh-cn/3/library/csv.html csv
.
reader()
writer()
.
register_dialect()
unregister_dialect()
get_dialect()
list_dialects()
field_size_limit()
class
.
DictReader
DictWriter
.
DictDialect
unix_dialect
.
excel
excel_tab
Sniffer
sniff()
has_header()
const
.
quote_all
quote_minimal
quote_nonnumeric
quote_none
网络
ipaddress >
addr = ip_address()
addr
version
is_private
packed
socket >
url: https://docs.python.org/zh-cn/3/library/socket.html#module-socket .
exception
error
herror
gaierror
timeout
const
af_unix
af_inet
af_inet6
sock_stream
sock_dgram
sock_raw
sock_rdm
sock_seqpacket
sock_cloexec
somaxconn
af_can
pf_can
can_bcm
can_raw_fd_frames
can_raw_join_filters
can_isotp
can_j1939
af_packet
pf_packet
af_rds
pf_rds
sol_rds
sio_rcvall
sio_keepalive_vals
sio_loopback_fast_path
af_alg
sol_alg
af_vsock
ioctl_vm_sockets_get_local_cid
af_link
has_ipv6 - lower
bdaddr_any
bdaddr_local
hci_filter
hci_time_stamp
hci_data_dir
af_qipcrtr
func
socket()
socketpair()
create_connection()
create_server()
has_dualstack_ipv6()
fromfd()
fromshare()
close()
.
getfqdn()
get host name()
.
get addr info()
get name info()
.
get host by name()
get host by name_ex()
get host by addr()
get proto by name()
get serv by name()
get serv by port()
.
ntohl()
ntohs()
htonl()
htons()
.
inet_aton()
inet_ntoa()
inet_pton()
inet_ntop()
.
get default timeout()
set default timeout()
set hostname()
.
if_name index()
if_name to index()
if_index to name()
.
accept()
bind()
close()
connect()
connect_ex()
detach()
dup()
fileno()
.
get_inheritable()
get peername()
get sockname()
get sockopt()
get blocking()
get timeout()
ioctl()
listen()
makefile()
.
recv()
recvfrom()
recvmsg()
recvmsg_info()
recvfrom_into()
recv_into()
recv_fds()
.
send()
sendall()
sendto()
sendmsg()
sendmsg_afalg()
send_fds()
sendfile()
.
set_inheritable()
set blocking()
set timeout()
set sockopt()
shutdown()
share()
.
family
type
proto
.
SocketType
CMSG_LEN()
CMSG_SPACE()
selectors
select
socketserver
.
urllib.parse
urllib.request
urllib.robotparser
base64
http.server
http.cookies
webbrowser
uuid
json
None - null
dumps(separators=[', ',': '],sort_keys=True) - loads() # s - string
dump(,fp) - load(,fp)
xmlrpc.client
xmlrpc.server
smtplib
smtpd
mailbox
imaplib
.
argparse
getopt
readline
getpass
cmd
shlex
configparser
logging
fileinput
atexit
sched
.
gettext
locale
.
pydoc
doctest
unittest
trace
traceback
cgitb
@ pdb >
调用方式
import pdb;pdb.set_trace()
breakpoint() python3.7+
python -m pdb test.py
command
s step
n next
q quit
p print
p pprint
l list
profile
pstats
timeit
tabnanny
compileall
pyclbr
venv
ensurepip
.
site
sys
get-
sizeof()
refcount()
os >
.
getpid() - getppid()
os -> posix nt - os specific module
getenv()
from os.path import ...
.
makedirs
removedirs
renames
walk
_Environ(MutableMapping)
PathLike(abc.ABC)
@abc.abstractmethod
__fspath__
__subclasshook__
if
...
return NotImplemented
platform
resource
gc >
disable() - enable()
collect()
sysconfig
.
warnings
abc
dis >
dis()
show_code()
@ inspect >
getsource
signature()
parameters[]
name - kind - default
bind()
arguments[]
arguments
bing_partial() - 部分参数检查
@ Cython >
引用计数
PyObject
PyIntObject
PyStringObject
PyListObject
PyDictObject
PyCodeObject
PyFrameObject
PyFunctionObject
初始化
线程
模块
import
多线程
GIL
threading
内存管理
.
Block
Pool
arena
循环引用
垃圾收集
标记-清除
gc
字节码
before_async_with
binary - inplace
.
add
subtract
multiply
matrix_multiply
floor_divide
true_divide
modulo
subscr
power
.
and
or
xor
.
rshift
lshift
build
string
list
tuple
map
const_key_map
set
slice
call
function
function_ex
function_kw
method
co_iterable_coroutine
compare_op
contains_op
copy_dict_without_keys
delete
attr
deref
fast
global
name
subscr
dict
merge
update
dup
top
top_two
end_async_for
extended_arg
for_iter
format_value
get
aiter
anext
awaitable
iter
len
yield_from_iter
have
argument
import
from
name
star
is_op
jump
absolute
forward
if_false_or_pop
if_not_exc_match
if_true_or_pop
list
append
extend
to_tuple
load
assertion_error
attr
class
classderef
closure
const
deref
fast
global
method
name
make
function
map
add
match
class
keys
mapping
sequence
nop
null
pop
block
except
jump_if_false
jump_if_true
top
print_expr
push
raise_varargs
reraise
return_value
rot
two
three
four
set
add
update
setup
annotations
async_with
finally
with
store
attr
deref
fast
global
name
subscr
tos
unary
invert
negative
not
positive
unpack
ex
sequence
sequence
with_except_start
yield
from
value
框架 >
爬虫
.
@ requests >
r=requests.get(url,data)
data
user-agent
r.text
r.content.decode()
@ BeautifulSoup >
url: https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/#id72 文档
.
pip install beautifulsoup4
lxml - html5lib
.
from bs4 import BeautifulSoup
bs4.diagnose
diagnose() - 如何处理
解析错误
HTMLParser.HTMLParseError: malformed start tag
HTMLParser.HTMLParseError: bad end tag
soup=BeautifulSoup(''/fp,parser)
parse_only = SoupStrainer()
parser
'html5lib' 全补
'lxml' - 'lxml-xml' 正补 - 'xml'
'html.parser' 漏补
encode
from_encoding=''
exclude_encodings=[]
soup.contains_replacement_characters
.encode()
.
dammit=UnicodeDammit(',[]) - chardet cchardet
smart_quotes_to = Unicode - 'html/xml/ascii'
.original_encoding
.unicode_markup
UnicodeDammit.detwingle(') Windows-1252 -> utf8
soup - document
.
soup.prettify(encoding)
get_text(delimiter,strip=True)
导航
soup.title -> find()
find() -> find_all(limit=1)
.
find_parents - find_parent
find_next_siblings - find_next_sibling
find_previous_siblings - find_previous_sibling
find_all_next - find_next
find_all_previous - find_previous
find_all()
arguments
tag_name
attrs={}
recursive
string - getter
**kwargs
.
True '' []
re.compile() - callback
.
class_
limit
soup() = soup.find_all()
select() - select_one()-> css selector
.
parent - parents Q:还返回doucment和None?
contents - children 生成器 - descendants Q:B or D?
strings - stripped_strings
next_sibling - previous_sibling # 注意'\n'
next_siblings - previous_siblings
next_element - previous_element # DFS
next_elements - previous_elements
修改
soup.new_string('',type)
soup.new_tag()
append() insert() insert_before() insert_after()
clear() extract() - pop
decompose() - 销毁
replace_with()
wrap(soup.new_tag()) - unwrap() # in place
Class
Tag
.name - tag .string - text # 不唯一 -> None Q:修改好像又可以覆盖?
# 猜测是getter和setter方法分别起作用
attrs - []
.attrs
多值属性 - list - HTML标准
XML无多值属性
NavigableString
tag.string
特殊的NavigableString
Comment
.replace_with(CData('123')) Q:?
CData
ProcessingInstruction
Declaration
Doctype
@ Scrapy >
cmd
startproject .
genspider . .
-t
crawl
csvfeed
xmlfeed
.
crawl .
check
edit
.
view
fetch
shell
parse
settings
--get
version
bench
custom
project
items.py >
(Item)
. = Field()
pipelines.py >
.
process_item(,item,spider)
settings.py >
levels
scrapy source code
scrapy.settings.default_settings.py
project/settings.py
spider custom_settings
command -s
logging
log_level
debug
info
warning
error
critical
slient
log_file - standard error
logstats_interval 60s
bool
log_enabled true
log_stdout
stats
...
telnet .
performance
concurrent_requests *4/s
concurrent_requests_per_ip
concurrent_requests_per_domain
concurrent_items
download_timeout 180s -> 10s
download_delay 0s
randomize_download_delay True
dnscache_enabled True
stop early
closespider_
timeout
itemcount
pagecount
errorcount
http caching - offline
scrapy.contrib.httpcache
httpcache_
enabled False
.
dir
storage
DbmCacheStorage
dbm_module anydbm
policy
RFC2616Policy
.scrapy
httpcache
easy
...
crawling style
depth_priority 深度优先级 - BSF !
depth_limit
scheduler_
scrapy.squeue._Queue
disk_queue
PickleFifoDisk
memory_queue
FifoMemory
robotstxt_obey
feed
download media
amazon web service
aws_access_key_id
aws_secret_access_key
proxy
spiders
spider.py >
(Spider)
name
start_urls
allowed_domains
start_requests()
parse(,response)
response >
url meta
xpath() - css()
get()
yield
item
Request(,callback,meta)
FormRequest(,formdata)
(CrawlSpider)
rules = (Rule(LinkExtractor(allow),callback,follow),)
parse_item()
scrapy.cfg
@ Selenium >
url: https://www.selenium.dev/documentation/en/ 文档
url: http://www.python3.vip/tut/auto/selenium/skills_2/#%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6 上传文件 无input[type="file"]
url: http://www.python3.vip/tut/auto/selenium/skills_2/#%E8%87%AA%E5%8A%A8%E5%8C%96electron%E7%A8%8B%E5%BA%8F Electron
[selenium]
[webdriver]
ChromeOptions
add_experimental_option(,)
mobileEmulation - {deviceName:}
to_capbilities()
wd = Chrome('chromedriver.exe',desired_capabilities) / Edge
.
switch_to.
frame()
default_content(handle)
window()
alert.
accept() - dismiss() - send_keys()
text
e = find element(s) by x id class_name tag_name css_selector
.
send_keys()
click()
[de]select_by x value index visible_text - deselect_all
.
text
get_attribute()
outer/innerHTML
value
textContent
.
title
current_url
window_handles
current_window_handle
.
execute_script()
.
get()
.
get/set_window_size()
get_screenshot_as_file()
.
implicitly_wait()
[com]
[action_chains]
ac = ActionChains(wd)
move_to_element().perform()
.
@ xpath >
$x("...")
/ - // - ./
*
| : ,
[]
n - last() - position()
@attr text()
=
contains() - ends/starts-with()
::
preceding/following-sibling
.
@ Appery.io >
Web
@ Django >
资料
url: https://learning.oreilly.com/library/view/django-2-by/9781788472487/d612071b-efb8-48f8-a998-70f8d8f0c706.xhtml django 2 by example
url: https://docs.djangoproject.com/en/3.1/ django 3.1
project
django-admin
startproject project
manage.py >
startapp app
makemigrations - models -> migrate - installed_apps ???
.
runserver [url]
adding new file
createsuperuser
shell
.
--settings=project.settings
project
__init__.py
settings.py >
debug
allowed_hosts
installed_apps
django.contrib.
admin
auth
models.User
contenttypes
sessions
messages
staticfiles
app.apps.AppConfig
middleware
root_url_conf
databases
language_code
use_tz
urls.py
path('admin/',admin.site.urls)
path('',include('$app.urls',namespace=''))
wsgi.py - web server gateway interface
asgi.py - ...
app
__init__.py
admin.py >
from .models import $Model
.
.
admin.site.register(Model)
.
@admin.register(Model)
class ModelAdmin(admin.ModelAdmin):
.
list_display
list_filter
search_fields
ordering
raw_id_fields - lookup ???
.
prepopulated_fields = {'':('',),}
.
date_hierarchy
apps.py
sitemaps.py
feeds.py
migrations
__init__.py
templatetags
__init__.py
models.py >
django.db.models
django.utils
timezone.now
(Model)
Meta
ordering=(...)
db_table
default_manager_name
functions
get_absolute_url()
return reverse('app_url_namespace:view_url_name',args=[])
attrs
field .
ORM object-relational mapper
objects
default
create()
get()
DoesNotExist - MultipleObjectsReturned
all()
filter(...__...=...) - exclude()
e.g.
publish_year
author_username
title_startswith
order_by('[-]...')
custom
...Manager(models.Manager)
get_queryset()
obj
save() - delete()
forms.py >
django.forms
.
Form
ModelForm
.
ValidationError
form
is_valid()
cleaned_data
errors
.
as_p
as_ul
as_table
for field in form
field.label_tag
tests.py
urls.py >
urlpatterns
django.urls
path('<slug:post>/',view,name='')
...View.as_view()
views.py >
type
function(request,...)
...View(django.views.generic.ListView)
queryset - model
context_object_name
paginate_by
template_name - app/..._....html
# page_obj Q:how to set?
django.shortcuts
render(request,'template',{})
get_object_or_404 - get_list_or_404
templates >
.
base.html
pagination.html
Model
list.html
detail.html
.
{% tag %}
load
static ""
.
block block_name - endblock
extends ""
include "" with sub_name=self_name
.
for ... in ... endfor
if elif else endif
.
csrf_token - cross-site request forgery
url: https://owasp.org/www-community/attacks/csrf csrf
.
url "namespace:name" parameters
{{ variable }}
{{ variable|filter:...|... }}
truncatewords - 截断
linebreaks - \n -> <br >
django
db
models
RESTful API
部署
WSGI
.
django.get_version()
.
pagination >
django.core.paginator
page=request.GET.get('page') # 不仅是个数
page.
paginator=Paginator(objs,objs_per_page)
paginator.
page_obj=page(page) 1 - first
page_obj.
has_previous - previous_page_number
has_next - next_page_number
number - page.paginator.num_pages
num_pages
.
PageNotAnInteger
EmptyPage - out of range
field >
type
CharField
EmailField
SlugField
ForeignKey
TextField
DateTimeField
parameter
.
max_length
unique_for_type='key'
on_delete
CASCADE
related_name
default
choices ((),(),...) ???
widget
forms.Textarea
bool
.
auto_now_add - creating
auto_now - saving
primary_key
required
key
title
slug - SEO - url
author
body
publish - created - updated - status
old notes
custom
options
null
blank
choices
get_FOO_display
MedalType=models.TextChoices
choices=MedalType.choices
help_text
unique
verbose_name
send email >
settings
email_host
smtp.gmail.com
less secure apps
captcha:?
smtp.qq.com
email_port
email_host_user
email_password
email_use_tls - Transport Layer Security
email_use_ssl
.
email_backend = 'django.core.mail.backends.console.EmailBackend'
django.core.mail
send_mail(subject,message,from,to:[])
fail_silently
request
build_absolute_url() - include schema and hostname
@ Flask >
url: https://www.youtube.com/watch?v=LJSQrfKGnqQ&list=PLwIrqQCQ5pQno-sy3GBuVRcb7d3qKlv4l&index=6 黑马
abort(404/R..("")) : raise
Response()
request
data : json xml
form : = &
args
getlist(key)
cookies
headers
method
url
files
.save()
session
内存 ...
跨服务器 归属地
session_id cookie/url 隐私 有效期
Flask
app = Flask(__name__,static_url_path,static_folder,template_folder)
@app.errorhandler()
@app.route('/',methods = ['GET',POST'])
<:>
int
float
path - /
Converter(werkzeug.routing.BaseConverter)
__init__(url_map,regex)
super(Converter,self).__init__(url_map)
self.regex = regex
to_python(value)
to_url(value) - url_for
.
return 响应体 "666 good" {(,),}/{:,}
make_response()
status
headers[]
set_cookie(,,max_age:s)
delete_cookie() : 有效期
jsonify()
current_app - app
run(host,port,debug)
0.0.0.0
config
from_pyfile('config.cfg')
from_object(Config)
[key] = value
secret_key
url_map
converters
redirect()
url_for(,**kw)
.
set FLASK_APP = ...
flask run
.
escape
request
request.args.get()
科学计算
@ Scipy >
.
np.info
packages
cluster
constants
fftpack
integrate
interpolate
io
linalg
ndimage
odr
optimize
signal
sparse
spatial >
class
Delaunay
simplices
ConvexHull
simplices
Voronoi()
SphericalVoronoi()
HalfspaceIntersection()
.
delaunay_plot_2d()
convex_hull_plot_2d()
voronoi_plot_2d()
.
KDTree
query()
cKDTree
Rectangle()
.
tsearch()
distance_matrix()
minkowski_distance()
minkowski_distance_p()
procrustes()
geometric_slerp()
submodule
distance >
.
pdist()
cdist()
squareform()
directed_hausdorff()
is_valid_dm()
is_valid_y()
num_obs_dm()
num_obs_y()
numeric
braycurtis()
canberra()
chebyshev()
cityblock()
correlation()
cosine()
euclidean()
jensenshannon()
mahalanobis()
minkowski()
seuclidean()
sqeuclidean()
wminkowski()
bool
dice()
hamming()
jaccard()
kulsinski()
rogerstanimoto()
russellrao()
sokalmichener()
sokalsneath()
yule()
transform
special
@ stats >
url: https://docs.scipy.org/doc/scipy/reference/stats.html api
.
rv_continuous()
rv_discrete()
rv_histogram()
连续
alpha()
anglit()
arcsine()
argus()
beta()
betaprime()
bradford()
burr()
burr12()
cauchy()
chi()
chi2()
cosine()
crystalball()
dgamma()
dweibull()
erlang()
expon()
exponnorm()
exponweib()
exponpow()
f()
fatiguelife()
fisk()
foldcauchy()
foldnorm()
genlogistic()
gennorm()
genpareto()
genexpon()
genextreme()
gausshyper()
gamma()
gengamma()
genhalflogistic()
geninvgauss()
gilbrat()
gompertz()
gumbel_r()
gumbel_l()
halfcauchy()
halflogistic()
halfnorm()
halfgennorm()
hypsecant()
invgamma()
invgauss()
invweibull()
johnsonsb()
johnsonsu()
kappa4()
kappa3()
ksone()
kstwo()
kstwobign()
laplace()
laplace_asymmetric()
levy()
levy_l()
levy_stable()
logistic()
loggamma()
loglaplace()
lognorm()
loguniform()
lomax()
maxwell()
mielke()
moyal()
nakagami()
ncx2()
ncf()
nct()
norm()
norminvgauss()
pareto()
pearson3()
powerlaw()
powerlognorm()
powernorm()
rdist()
rayleigh()
rice()
recipinvgauss()
semicircular()
skewnorm()
t()
trapezoid()
triang()
truncexpon()
truncnorm()
tukeylambda()
uniform()
vonmises()
vonmises_line()
wald()
weibull_min()
weibull_max()
wrapcauchy()
多维
multivariate_normal()
matrix_normal()
dirichlet()
wishart()
invwishart()
multinomial()
special_ortho_group()
multivariate_t()
multivariate_hypergeom()
离散
bernoulli()
betabinom()
binom()
boltzmann()
dlaplace()
geom()
hypergeom()
logser()
nbinom()
nhypergeom()
planck()
poisson()
randint()
skellam()
zipf()
yulesimon()
.
describe()
gmean()
hmean()
kurtosis()
mode()
moment()
skew()
kstat()
kstatvar()
tmean()
tvar()
tmin()
tmax()
tstd()
tsem()
variation()
find_repeats()
trim_mean()
gstd()
iqr()
sem()
bayes_mvs()
mvsdist()
entropy()
median_absolute_deviation()
median_abs_deviation()
.
cumfreq()
itemfreq()
percentileofscore()
scoreatpercentile()
relfreq()
binned_statistic()
binned_statistic_2d()
binned_statistic_dd()
.
f_oneway()
pearsonr()
spearmanr()
pointbiserialr()
kendalltau()
weightedtau()
linregress()
siegelslopes()
theilslopes()
multiscale_graphcorr()
test
ttest_1samp()
ttest_ind()
ttest_ind_from_stats()
ttest_rel()
chisquare()
cramervonmises()
power_divergence()
kstest()
ks_1samp()
ks_2samp()
epps_singleton_2samp()
mannwhitneyu()
tiecorrect()
rankdata()
ranksums()
wilcoxon()
kruskal()
friedmanchisquare()
brunnermunzel()
combine_pvalues()
jarque_bera()
ansari()
bartlett()
levene()
shapiro()
anderson()
anderson_ksamp()
binom_test()
fligner()
median_test()
mood()
skewtest()
kurtosistest()
normaltest()
.
boxcox()
boxcox_normmax()
boxcox_llf()
yeojohnson()
yeojohnson_normmax()
yeojohnson_llf()
obrientransform()
sigmaclip()
trimboth()
trim1()
zmap()
zscore()
distance
wasserstein_distance()
energy_distance()
.
rvs_ratio_uniforms()
.
circmean()
circvar()
circstd()
.
chi2_contingency()
contingency.expected_freq()
contingency.margins()
fisher_exact()
plot-test
ppcc_max()
ppcc_plot()
probplot()
boxcox_normplot()
yeojohnson_normplot()
.
gaussian_kde()
warning
F_onewayConstantInputWarning()
PearsonRConstantInputWarning()
PearsonRNearConstantInputWarning()
SpearmanRConstantInputWarning()
计算机视觉
@ OpenCV >
url: https://www.bilibili.com/video/BV1oJ411D71z?p=6 啥都会一点的研究生
cv2
.
img = imread()
ndarray : h w c
.
imshow('',img)
waitKey(0)
destroyAllWindows()
imwrite(,img)
.
b,g,r = split(img)
img = merge((b,g,r))
add() : 255 -- + : 取余
αx + βy + γ
resize(,(,))
(0,0),fx,fy : 比例
.
vc = VideoCapture()
.
isOpened()
open,frame = read()
.
cvtColor(frame,color_bgr2gray)
convertScaleAbs()
.
addWeighted(img1,k1,img2,k2,b)
阈值
threshold(src,thresh,maxval,type)
thresh_
binary - 0 max
binary_inv - max 0
trunc - 0 阈值
tozero - 0 b
tozero_inv - a 0
平滑处理
.
均值滤波
blur(,(,))
方框滤波
boxFilter(,-1,(,),normalize=True)
高斯滤波
GaussianBlur(,(,),s)
c b c
b a b
c b c
中值滤波
medianBlur(,n)
腐蚀 膨胀
erode(,kernel,iterations = ) - 边白 -> 黑
dilate(,,)
开 闭
morphologyEx(,morph_open/close,kernel)
open : 去毛刺
梯度 = 膨胀 - 腐蚀
morph_gradient
礼帽 黑帽
礼帽 : 输入 - 开 : 刺 : tophat
黑帽 : 闭 - 输入 : 轮廓 : blackhat
图像处理
sobel
Gx 右 - 左
-1 0 +1
-2 0 +2
-1 0 +1
Gy 下 - 上
-1 -2 -1
.0 0 0
+1 +2 +1
Sobel(,ddepth,dx,dy,ksize=3)
10/01 不建议 11
scharr
x
-3 0 3
-10 0 10
-3 0 3
laplacian : 二阶导 噪音点敏感
0 1 0
1 -4 1
0 1 0
边缘检测