1 Star 0 Fork 0

toliong/pythonbasic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
basic.py 8.95 KB
一键复制 编辑 原始数据 按行查看 历史
toliong 提交于 2020-04-23 10:57 . xxx
#!/usr/bin/python3
# --*-- coding:utf8 --*--
# shutil 模块
# import os
# import shutil
# 复制文件文件夹 shutil.copy(source, destination)
# print(os.getcwd())
# #shutil.copy('aa.txt','bb.txt') # 复制文件,返回拷贝后文件的路径
# shutil.copytree('aa','bb') # 拷贝目录 this is a test
# shutil.move(source, destination) # 返回新位置的绝对路径;如果dest是文件夹,
# source将移动到dest中,名字不变
# #os.unlink(path) # 删除path
# #os.rmdir(dirpath) # dirpath必须为空
# #os.walk(path) # 遍历path及其子目录,返回目录名,子目录列表,子文件列表
# #shutil.rmtree(path) # 删除path,包含子目录,文件 【不可恢复】
# #import send2trash
# #send2trash.send2trash('aa.txt') # 删除文件到垃圾桶
# import random
# random.sample(alist,anum) # 从alist中随机选择anum个值
# #random.shuffle(variable) # 将variable打乱顺序
# print( 'YES')
# 字典
# dic6=dict.fromkeys(['key1','key2','key3'], 'test') # 生成的字典value值修改时,会全修改
# dic1={'1':1,'2':2}
# # dic1.setdefault(4,'4')
# # print(dic1)
# ## 字典 删
# # del dic1[1]
# # dic1
# # a = dic1.pop('1') # 删除kv ,并返回value
# # dic1.popitem() # 随机删除
# # dic1.clear()
# print(dic1)
# 字符串内置方法
# st = 'hello kitty {name}'
# st.count(str,index1,index2) # 统计个数
# st.capitalize() # 首字母大写
# st.center(50,'-') # 字符串放在50个'-'中间
# st.endwith('something') # 是否以something结尾
# st.startswith('sm') # 是否以sm开始
# st.expandtabs(tabsize=10) # tab转成10个空格
# st.find('s') # 返回s在st中的第一个位置的索引值
# st.format() # 格式化输出,参数赋值
# st.format_map() # 格式化输出,参数为字典
# st.index('i') # 元素的索引值,不存在则报错
# st.isalnum() # 是否包含字母或数字
# st.isalpha() # 是否是字母
# st.isdecimal() # 十进制字符。这种方法只存在于unicode对象。
# st.isdigit() # 是否是数字
# st.isnumeric() # 是否是数字,unicode对象。
# st.isidentifier() # 是否是非法变量名
# st.islower(),st.isupper() # 小写,大写
# st.isspace() # 是否是空格
# st.istitle() # 每个单词首字母是否大写
# st.upper(), st.lower() # 变成大写,变成小写
# st.swapcase() # 大小写转换
# st.ljust(50,'-') # 以'-'左填充50字符
# st.rjust(50,'-')
# st.strip() # 去掉两侧空字符
# st.lstrip(), st.rstrip() # 去掉左/右空字符
# st.replace('x','y',2) # st中x替换成y 替换2次
# st.rfind('x') # 最后一个元素x的索引值
# st.split(' ') # 以空格为分隔符,转换成列表
# st.rsplit('x',2) # 分隔符,分割次数
# python2 编码解码
# |---编码-encode-->---|
# gbk---| |---UTF-8
# |---<--decode-解码---|
# python3 默认字符集 是 UTF8
# import sys
# import os
# print(sys.getdefaultencoding())
# print('YES')
# print('local:', os.getcwd())
### 深浅拷贝
# http://www.cnblogs.com/yuanchenqi/articles/5782764.html
# --- 浅copy : 只复制第一层
# s = [[1,'2'],'str','yui',4]
# s1 = s.copy() # s2 = s[:] id(s) != id(s1)
# s2 = s # id(s) = id(s1)
# # s1 = s.copy() s1与s使用不同的内存空间; s2=s s2与s使用相同的内存空间;不管哪种赋值, 列表中的元素都是是用相同的内存空间
# s1[3] = 3
# s1[0][0] =2
# print(s) #=> [[2, '2'], 'str', 'yui', 4]
# print(s1) #=> [[2, '2'], 'str', 'yui', 3]
# # -- s和s1共用[1,'2']列表的指针。修改了[1,'2']列表内容,s[0]和s1[0]内容同时变化
# # 列表有可变元素,使用copy函数指向另外一个列表变量。在修改可变元素的子元素时,两个列表都会变化。
# # 列表执行copy,只会复制第一层元素; 子层使用相同的内存空间
# print(id(s))
# print(id(s1))
# print(id(s2))
# print(id(s[2]))
# print(id(s1[2]))
# # ---深copy: 全部克隆
# import copy
# s = [[1,'2'],'str','yui',4]
# s1 = copy.copy(s) # 浅拷贝
# s2 = copy.deepcopy(s) # 深拷贝,子元素使用不同的内存空间
# print(id(s[0]))
# print(id(s1[0]))
# print(id(s2[0]))
### 集合
# -- set: 唯一值,不能重复
# a = {'1','2','3'}
# b = set('123')
# c = {1:'a',2:'b'}
# print(a)
# print(b)
# print(a == b)
# print(type(c))
# a.add('x')
# a.remove('x')
# a.update('asdf') #update方法,如果参数是序列,则把序列中的每一项作为一个元素添加到集合中
# a.pop() # 随机删除一个
# # a.clear() # 清空
# # -- 集合的操作
# print('1' in a)
# print(a)
# a = {'1','2','3'}
# b = {'1','2','a'}
# a == b # 两个集合是否相等
# # a 与 b 的交集
# print( a & b)
# print(a.intersection(b))
# # a 与 b 的并集
# print( a | b )
# print( a.union(b))
# # a 与 b 的差集
# print( a ^ b ) # a与b 不相同的元素 print(a.symmetric_difference(b))
# print(a.difference(b)) # 在a 不在b print(a-b)
# print(b.difference(a)) # 在b 不在a print(b-a)
# print(a.issuperset(b)) # a是否是b的父集 a > b
# print(a.issubset(b)) # a是否是b的子集 a < b
### 函数
# -- 形式参数 ,必须参数
# def add(a,b):
# print(a+b)
# add(3,5)
# # -- 关键字参数
# def print_info(name, age):
# print('Name: %s' % name)
# print('Age: %d' % age)
# print_info(name='Yian', age=13)
# # -- 默认参数 默认参数只能放在最后
# def print_info_default(name, age, sex="F"):
# print('Name: %s' % name)
# print('Age: %d' % age)
# print('Sex: %s' % sex)
# print_info_default('R',12)
# print_info_default('H',13,'M')
# # -- 不定长参数
# def add1(*args):
# sum = 0
# for i in args: # args 是元组
# sum += i
# print(sum)
# add1(1,2,3,4,5)
# # -- 不定长,关键参数
# def person(name, age, **kw):
# print('name:', name, 'age:', age, 'other:', kw)
# person(name='R',age=13,sex='F',hobby='girl')
# # -- 综合
# # 参数*args接收无命名参数(非关键字参数), **kwargs接收关键字参数
# def print_info_zonghe(*args, **kwargs):
# print('args:', args)
# print('kwargs: ', kwargs)
# print_info_zonghe('R',13,'F',hight=188,weight='88')
# -- 函数作用域
# 范围: python内置作用域< 模块中的全局 < 外层作用域 < 局部作用域
# 局部变量不能修改全局变量(没有global参数)
# count = 1 # 全局变量
# def outer():
# # global count
# print(count)
# # count +=1 # 此注释行打开会报错,因为局部变量不能修改全局变量;如果需要修改,变量名前加global
# def outer():
# count = 10
# def inner():
# nonlocal count # 子函数修改函数中的变量时,要使用nonlocal声明
# count = 20
# print(count)
# inner()
# print(count)
# 递归函数
# -- 调用自己
# -- 有结束条件
# -- 但凡递归可以写的循环都可以
# -- 在有些时候,递归效率较低
# def fact(n):
# if n == 1:
# return 1
# return n*fact(n-1)
# print(fact(5))
# 内置函数
# abs(-11)
# all(['1']) # 序列中的每一个元素都为真,则True; 否则False
# bool()
# dict()
# enumerate()
# eval() # 计算器
# -- filter(函数,序列) 过滤器
# 遍历序列作为函数参数,如果False,则返回. 函数的返回值应该是True或False
# def oushu(x):
# return x % 2 == 1
# print(list(filter(oushu, [12,3,4,5,6,7,8,9])))
# # -- map(函数,序列)
# # 遍历序列的元素作用到函数分别作用到函数中
# def astr(xx):
# return 'aa'+xx
# print(list(map(astr, ['1','ghj','123','syuu']))) #--> ['1', 'ghj', '123', 'syuu']
# # -- reduce(函数, 序列)
# # 两个参数;遍历序列,除第一次执行,每次前面参数为前面一次执行的返回值,第二个参数为下一个序列元素
# from functools import reduce
# def add1(x,y):
# return x+y
# print(reduce(add1,range(101))) # => 5050
# # -- lambda 匿名函数
# lamda x,y:x*y
def outer(func):
def inner(username):
print('认证成功')
result = func(username)
print('日志添加成功')
return result
return inner
@outer
def f1(name):
print('%s 正在连接业务部门数据接口....' % name)
f1('Jack')
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/toliong/pythonbasic.git
git@gitee.com:toliong/pythonbasic.git
toliong
pythonbasic
pythonbasic
master

搜索帮助