资源共享网站怎么做,WordPress上传图片显示在页面代码,政务网站风格,沧州网站建设选网龙python 33个高级用法技巧
使用装饰器计时函数 装饰器是一种允许在一个函数或方法调用前后运行额外代码的结构。
import timedef timer(func):装饰器函数#xff0c;用于计算函数执行时间并打印。参数:func (function): 被装饰的函数返回:function: 包装后…python 33个高级用法技巧
使用装饰器计时函数 装饰器是一种允许在一个函数或方法调用前后运行额外代码的结构。
import timedef timer(func):装饰器函数用于计算函数执行时间并打印。参数:func (function): 被装饰的函数返回:function: 包装后的函数def wrapper(*args, **kwargs):包装函数计算并打印函数执行时间。参数:*args: 原函数的非关键字参数**kwargs: 原函数的关键字参数返回:原函数的返回值start_time time.time() # 记录开始时间result func(*args, **kwargs) # 执行原函数end_time time.time() # 记录结束时间print(fFunction {func.__name__} took {end_time - start_time} seconds)return result # 返回原函数的结果return wrappertimer
def example_function(x):示例函数等待 x 秒后返回 x。参数:x (int/float): 等待的秒数返回:int/float: 输入的 x 值time.sleep(x) # 模拟耗时操作return x# 调用被装饰的函数
result example_function(2)
print(result)Function example_function took 2.0022225379943848 seconds
2装饰器功能 装饰器 timer 计算并打印被装饰函数的执行时间。通过 wrapper 函数实现这一功能使得可以在不修改原函数代码的情况下添加额外的行为。 装饰器语法糖 timer 是装饰器的简洁语法用于将装饰器应用于函数。相当于手动将函数传递给装饰器并将返回值赋给原函数名。 包装函数 wrapper 函数接受任意数量的参数和关键字参数确保可以包装任何函数。在 wrapper 中可以在调用原函数之前或之后添加任何额外的代码这里是计算并打印执行时间。
使用生成器 生成器是一种特殊的迭代器通过 yield关键字逐个生成值。
通过使用装饰器可以在不修改原函数代码的情况下添加额外的行为。装饰器函数接受一个函数作为参数返回一个新的包装函数在调用原函数之前或之后执行额外的代码。装饰器提供了一种简洁而强大的方式来扩展函数的功能使代码更加模块化和可重用。
def countdown(n):生成从 n 到 1 的倒计时序列。参数:n (int): 倒计时的起始值生成:int: 当前倒计时的值while n 0:yield nn - 1# 使用生成器函数 countdown 进行倒计时
for i in countdown(5):print(i)5
4
3
2
1生成器函数 生成器函数使用 yield 关键字逐个生成值与常规的返回值函数不同它在每次生成值后暂停执行并保留其状态以便继续生成下一个值。在生成器函数 countdown 中while 循环每次生成当前的 n 值然后将 n 减少 1。 迭代生成器 使用 for 循环迭代生成器时循环会自动处理生成器的状态并在每次迭代时调用生成器函数的 __next__() 方法获取下一个值。当生成器函数不再生成新值时迭代结束。
通过使用生成器函数可以逐个生成序列中的值而不需要一次性创建整个序列。这种方法在处理大数据集或需要逐步生成数据时非常有用能够节省内存并提高效率。生成器函数使用 yield 关键字生成值并在每次生成后暂停执行保留其状态以便后续继续生成。
使用命名元组 命名元组是一种特殊的元组允许通过名称访问元素。
from collections import namedtuplePoint namedtuple(Point, [x, y])
p Point(10, 20)
print(p.x, p.y)
10 20使用全局变量 全局变量可以在多个函数之间共享数据。
global_var 0def increment():global global_varglobal_var 1increment()
print(global_var)
1使用局部变量 局部变量在函数内部定义只在函数内部有效。
def example():local_var Helloprint(local_var)example()
# print(local_var) # 这行会报错因为local_var是局部变量
Hello使用类方法 类方法是一种绑定到类而不是实例的方法。
class MyClass:class_var 0classmethoddef increment_class_var(cls):cls.class_var 1MyClass.increment_class_var()
print(MyClass.class_var)
1使用静态方法 静态方法是一种不依赖于类或实例的独立方法。
class MyClass:staticmethoddef static_method():print(This is a static method.)MyClass.static_method()
This is a static method.使用实例方法 实例方法是绑定到实例的方法可以访问实例的属性和方法。
class MyClass:def __init__(self, value):self.value valuedef display_value(self):print(self.value)obj MyClass(10)
obj.display_value()
10使用装饰器添加功能 装饰器可以在不修改原函数代码的情况下添加功能。
def decorator(func):装饰器函数用于在调用被装饰函数之前打印一条消息。参数:func (function): 被装饰的函数返回:function: 包装后的函数def wrapper(*args, **kwargs):包装函数打印一条消息然后调用原函数。参数:*args: 原函数的非关键字参数**kwargs: 原函数的关键字参数返回:原函数的返回值print(Function is called)return func(*args, **kwargs)return wrapperdecorator
def say_hello():打印 Hello 的函数print(Hello)# 调用被装饰的函数
say_hello()Function is called
Hello使用链式函数调用 链式函数调用允许连续调用多个方法。
class MyClass:def __init__(self, value):self.value valuedef increment(self):self.value 1return selfdef display_value(self):print(self.value)obj MyClass(10)
obj.increment().increment().display_value()
12使用自定义迭代器 自定义迭代器可以实现自己的迭代逻辑。
class MyIterator:def __init__(self, data):初始化 MyIterator 对象并设置初始数据和索引。参数:data (list): 要迭代的数据列表self.data dataself.index 0def __iter__(self):返回迭代器对象本身。返回:MyIterator: 迭代器对象return selfdef __next__(self):返回下一个数据元素。返回:int/float: 当前索引的数据元素抛出:StopIteration: 当没有更多元素时停止迭代if self.index len(self.data):result self.data[self.index]self.index 1return resultelse:raise StopIteration# 创建 MyIterator 对象并迭代打印每个元素
my_iter MyIterator([1, 2, 3])
for value in my_iter:print(value)1
2
3__iter__ 方法返回迭代器对象本身。__next__ 方法在每次迭代中被调用返回当前索引位置的元素并将索引加1。 当索引超出数据列表的长度时抛出 StopIteration 异常迭代结束。
使用类方法 类方法可以在不实例化类的情况下调用。
class MyClass:class_var 0classmethoddef increment_class_var(cls):cls.class_var 1MyClass.increment_class_var()
print(MyClass.class_var)
1使用属性装饰器 属性装饰器用于控制属性的访问和修改。
class MyClass:def __init__(self, value):初始化 MyClass 对象并设置初始值。参数:value (int/float): 初始值self._value valueproperty # 属性方法def value(self):获取 _value 的值。返回:int/float: 当前 _value 的值return self._valuevalue.setter # 属性的设置方法def value(self, new_value):设置 _value 的新值。参数:new_value (int/float): 新值self._value new_value# 创建一个 MyClass 对象初始值为 10
obj MyClass(10)# 获取并打印 _value 的值
print(obj.value) # 输出: 10# 设置 _value 的新值为 20
obj.value 20# 获取并打印新的 _value 的值
print(obj.value) # 输出: 2010
20property 装饰器 将方法转换为属性使得可以通过 obj.value 访问而不需要调用方法。这种方法使得属性访问看起来更自然与直接访问实例变量类似。 value.setter 装饰器 将方法转换为属性的设置方法使得可以通过 obj.value new_value 来设置属性的值。这种方法提供了一种控制属性值设置的机制可以在设置值之前进行验证或其他处理。
使用字典合并 合并两个字典。
dict1 {a: 1, b: 2}
dict2 {b: 3, c: 4}# 使用字典的update方法合并
dict1.update(dict2)
print(dict1)
print({**dict1, **dict2}){a: 1, b: 3, c: 4}
{a: 1, b: 3, c: 4}使用 Counter计数 Counter类用于计数可哈希对象。
from collections import Counterdata [apple, banana, apple, orange, banana, apple]
counter Counter(data)
print(counter)
Counter({apple: 3, banana: 2, orange: 1})使用 deque进行双端队列操作 deque是一种双端队列可以在两端高效地添加和删除元素。
from collections import dequed deque([1, 2, 3])
d.appendleft(0)
d.append(4)
print(d)
deque([0, 1, 2, 3, 4])使用 defaultdict defaultdict是一个带有默认值的字典。
from collections import defaultdictdd defaultdict(int)
dd[a] 1
print(dd)
defaultdict(class int, {a: 1})使用堆排序 使用 heapq模块进行堆排序。
import heapq# 初始化一个无序列表
data [3, 1, 4, 1, 5, 9, 2, 6, 5]# 使用 heapq.heapify 将列表转换为堆
heapq.heapify(data)# 使用 heapq.heappop 逐个弹出最小元素实现排序
sorted_data [heapq.heappop(data) for _ in range(len(data))]# 打印排序后的列表
print(sorted_data)[1, 1, 2, 3, 4, 5, 5, 6, 9]堆排序过程 heapq.heapify(data) 将列表 data 转换为最小堆最小元素在堆的根节点。heapq.heappop(data)逐个弹出最小元素重新调整堆结构使得次小元素成为新的根节点。通过列表推导式所有元素依次弹出并存入新的列表 sorted_data实现排序。 堆的性质 最小堆是一种完全二叉树结构满足父节点小于或等于子节点的性质。这种结构使得获取最小元素的时间复杂度为 O(1)插入和删除元素的时间复杂度为 O(log n)。
import heapq# 初始化一个无序列表
data [3, 1, 4, 1, 5, 9, 2, 6, 5]# 将所有元素取反构建最大堆
max_heap [-x for x in data]# 使用 heapq.heapify 将列表转换为堆
heapq.heapify(max_heap)# 使用 heapq.heappop 逐个弹出最大元素原值
sorted_data [-heapq.heappop(max_heap) for _ in range(len(max_heap))]# 打印排序后的列表
print(sorted_data)[9, 6, 5, 5, 4, 3, 2, 1, 1]通过使用 heapq 模块可以间接实现大顶堆和堆排序。虽然 heapq 主要支持最小堆但通过取反数的方法可以高效地实现最大堆排序。
使用 bisect进行二分查找 使用 bisect模块进行二分查找。
import bisect# 初始化一个有序列表
data [1, 2, 4, 4, 5]# 使用 bisect.insort 在合适的位置插入元素 3
bisect.insort(data, 3)# 打印插入后的列表
print(data)[1, 2, 3, 4, 4, 5]二分查找 bisect 模块使用二分查找算法在有序列表中找到元素应该插入的位置。二分查找的时间复杂度为 O(log n)比线性查找 O(n) 更高效。 插入元素 insort 函数不仅找到插入位置还会将元素插入到该位置。
使用 itertools生成排列组合
import itertools# 生成 ABC 字符串的长度为2的排列
permutations list(itertools.permutations(ABC, 2))
# 生成 ABC 字符串的长度为2的组合
combinations list(itertools.combinations(ABC, 2))print(Permutations:, permutations)
print(Combinations:, combinations)Permutations: [(A, B), (A, C), (B, A), (B, C), (C, A), (C, B)]
Combinations: [(A, B), (A, C), (B, C)]使用 itertools生成无限序列
import itertoolscounter itertools.count(start1, step2)
print(next(counter))
print(next(counter))
print(next(counter))
1
3
5使用 functools.partial 使用 partial函数创建部分参数的函数。
from functools import partialdef power(base, exponent):计算 base 的 exponent 次方。参数:base (int/float): 底数exponent (int/float): 指数返回:int/float: base 的 exponent 次方return base ** exponent# 使用 partial 函数创建一个新的函数 square固定 exponent 参数为 2
square partial(power, exponent2)# 计算 3 的平方
print(square(3)) # 输出: 9
9partial 函数 partial 函数用于固定一个函数的部分参数从而创建一个新的函数。 在这个例子中partial 被用来固定 power 函数的 exponent 参数为 2从而创建一个新的函数 square。 使用 functools.lru_cache 使用 lru_cache缓存函数结果提高性能。
from functools import lru_cachelru_cache(maxsizeNone)
def fibonacci(n):计算第n个斐波那契数使用lru_cache进行缓存以提高性能。参数:n (int): 需要计算的斐波那契数的索引返回:int: 第n个斐波那契数if n 2:return nreturn fibonacci(n-1) fibonacci(n-2)# 打印第10个斐波那契数
print(fibonacci(10))
55缓存的优势 在没有缓存的情况下递归计算斐波那契数会有大量的重复计算。例如计算 F(10) 会多次计算 F(9)、F(8) 等。使用 lru_cache 后每个值只计算一次然后存储在缓存中以后再需要相同值时直接从缓存中读取避免重复计算提高了性能。 递归过程 当计算 fibonacci(10) 时函数会递归调用 fibonacci(9) 和 fibonacci(8)依次类推直到调用 fibonacci(0) 和 fibonacci(1)。由于 lru_cache 的存在计算 fibonacci(10) 的整个过程中每个值只会计算一次并存储在缓存中。
使用 subprocess运行外部命令
import subprocessresult subprocess.run([echo, Hello, World!], capture_outputTrue, textTrue)
print(result.stdout)
Hello, World!使用 shutil进行文件操作
import shutil
# 创建一个文件 example.txt 并写入内容
with open(example.txt, w) as file:file.write(Hello, world!)
shutil.copy(example.txt, example_copy.txt)
print(File copied.)File copied.使用 pathlib处理文件路径
from pathlib import Path# 创建一个文件 example.txt 并写入内容
with open(example.txt, w) as file:file.write(Hello, world!)# 使用 pathlib 创建一个 Path 对象
p Path(example.txt)# 打印文件名
print(p.name) # 输出example.txt# 打印文件名不包括后缀
print(p.stem) # 输出example# 打印文件后缀
print(p.suffix) # 输出.txt# 打印文件的父目录
print(p.parent) # 输出.example.txt
example
.txt
.使用正则表达式匹配字符串 使用 re模块进行正则表达式匹配。
import repattern re.compile(r\d)
match pattern.search(The answer is 42)
print(match.group())
42使用内存映射文件 使用 mmap模块进行内存映射文件操作。
import mmap
# 创建一个文件 example.txt 并写入内容
with open(example.txt, w) as file:file.write(Hello, world!)
# 打开文件 example.txt 进行读写操作 (rb 表示读写二进制模式)
with open(example.txt, rb) as f:# 使用 mmap 模块创建内存映射对象with mmap.mmap(f.fileno(), 0) as mm:# 从内存映射对象中读取一行并将其解码为 UTF-8 字符串print(mm.readline().decode(utf-8))Hello, world!with mmap.mmap(f.fileno(), 0) as mm:
使用 mmap 模块创建一个内存映射对象。f.fileno() 返回文件的文件描述符0 表示将整个文件映射到内存中。with 语句确保内存映射对象在块结束时会自动关闭。
使用logging记录日志
import logginglogging.basicConfig(levellogging.INFO)
logging.info(This is an info message)
INFO:root:This is an info message使用 argparse解析命令行参数
import argparsedef main(nameDefault Name):print(fHello, {name}!)if __name__ __main__:parser argparse.ArgumentParser(descriptionExample script)parser.add_argument(name, typestr, nargs?, defaultDefault Name, helpYour name)args parser.parse_args()main(args.name)
Hello, Alice!使用 unittest进行单元测试
import unittestdef add(x, y):return x yclass TestAdd(unittest.TestCase):def test_add(self):self.assertEqual(add(2, 3), 5)if __name__ __main__:unittest.main(argv[], verbosity2, exitFalse)
test_add (__main__.TestAdd) ... ok----------------------------------------------------------------------
Ran 1 test in 0.002sOK使用 tqdm显示进度条
from tqdm import tqdm
import timefor i in tqdm(range(100)):time.sleep(0.01)
100%|██████████| 100/100 [00:0100:00, 97.88it/s]使用 pandas进行数据分析
import pandas as pddata {Name: [Alice, Bob, Charlie], Age: [25, 30, 35]}
df pd.DataFrame(data)
print(df)
Name Age
0 Alice 25
1 Bob 30
2 Charlie 35