用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/microwind/ai-skills --skill python命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | Python高级特性 |
| description | 当应用Python高级特性时,分析高级语法,优化代码结构,解决复杂问题。验证特性应用,设计优雅架构,和最佳实践。 |
| license | MIT |
Python提供了丰富的高级特性,包括装饰器、生成器、元类、异步编程等,这些特性能够显著提升代码的表达力和性能。不当的高级特性使用会导致代码难以理解、性能下降、维护困难。
核心原则: 好的Python高级代码应该优雅简洁、性能优良、可读性强、易于维护。坏的高级代码会过度抽象、性能损耗、难以调试。
始终:
触发短语:
问题: 过度使用装饰器导致性能下降
原因: 装饰器增加了函数调用开销
解决: 合理使用装饰器,避免嵌套过深
问题: 生成器使用不当
原因: 不理解生成器的惰性求值特性
解决: 正确理解生成器的工作原理
问题: 过度使用元编程
原因: 代码逻辑过于隐晦
解决: 保持代码简洁,避免过度抽象
问题: 链式调用过长
原因: 代码可读性差
解决: 合理拆分链式调用
# 基础装饰器
def timing_decorator(func):
"""计时装饰器"""
import time
import functools
@functools.wraps(func)
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"{func.__name__} 执行时间: {end_time - start_time:.4f}秒")
return result
return wrapper
# 带参数的装饰器
def retry(max_attempts=3, delay=1, exceptions=(Exception,)):
"""重试装饰器"""
import time
import functools
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
last_exception = None
for attempt in range(max_attempts):
try:
return func(*args, **kwargs)
except exceptions as e:
last_exception = e
if attempt < max_attempts - 1:
()
time.sleep(delay)
:
()
last_exception
wrapper
decorator
():
functools
collections OrderedDict
():
cache_dict = OrderedDict()
():
key = (args) + ((kwargs.items()))
key cache_dict:
cache_dict.move_to_end(key)
cache_dict[key]
result = func(*args, **kwargs)
cache_dict[key] = result
(cache_dict) > max_size:
cache_dict.popitem(last=)
result
():
cache_dict.clear()
():
{
: (cache_dict),
: max_size,
: (cache_dict.keys())
}
wrapper.cache_clear = cache_clear
wrapper.cache_info = cache_info
wrapper
decorator
():
functools
():
():
(, ):
PermissionError()
permission .user_permissions:
PermissionError()
func(, *args, **kwargs)
wrapper
decorator
:
():
.user_permissions = user_permissions
():
random
time
time.sleep()
random.random() < :
ConnectionError()
{
: user_id,
: ,
:
}
():
():
functools
inspect = ().inspect
():
signature = inspect.signature(func)
():
bound_args = signature.bind(*args, **kwargs)
bound_args.apply_defaults()
param_name, param_value bound_args.arguments.items():
param_name type_hints:
expected_type = type_hints[param_name]
(param_value, expected_type):
TypeError(
)
func(*args, **kwargs)
wrapper
decorator
():
# 自定义迭代器
class FibonacciIterator:
"""斐波那契数列迭代器"""
def __init__(self, max_count=None):
self.max_count = max_count
self.current = 0
self.a, self.b = 0, 1
self.count = 0
def __iter__(self):
return self
def __next__(self):
if self.max_count and self.count >= self.max_count:
raise StopIteration
if self.count == 0:
self.count += 1
return 0
result = self.b
self.a, self.b = self.b, self.a + self.b
self.count += 1
return result
# 生成器函数
def ():
():
n < :
i (, (n ** ) + ):
n % i == :
num =
:
is_prime(num):
num
num +=
():
():
(filename, ) f:
line f:
(line.strip())
():
num numbers:
num % == :
num
():
num numbers:
num *
():
total =
num numbers:
total += num
total
numbers = read_numbers()
even_numbers = filter_even(numbers)
doubled_numbers = multiply_by_two(even_numbers)
result = sum_numbers(doubled_numbers)
result
():
():
total =
:
value = total
value :
total += value
total
():
count =
total =
:
value =
value :
count +=
total += value
total / count count >
acc = accumulator()
(acc)
(acc.send())
(acc.send())
(acc.send())
:
acc.send()
StopIteration e:
(, e.value)
avg = average_calculator()
(avg)
num [, , , , ]:
avg.send(num)
:
avg.send()
StopIteration e:
(, e.value)
contextlib contextmanager
():
:
f = (filename, mode)
f
:
f.close()
():
sqlite3
conn =
:
conn = sqlite3.connect(connection_string)
conn
Exception e:
()
:
conn:
conn.close()
():
fib_iter = FibonacciIterator()
(, (fib_iter))
prime_gen = prime_generator()
first_10_primes = [(prime_gen) _ ()]
(, first_10_primes)
file_manager(, ) f:
f.write()
file_manager(, ) f:
content = f.read()
(, content)
coroutine_example()
# 基础元类
class SingletonMeta(type):
"""单例元类"""
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]
class Singleton(metaclass=SingletonMeta):
"""单例基类"""
def __init__(self):
self.value = 0
# 属性验证元类
class ValidatedMeta(type):
"""属性验证元类"""
def __new__(cls, name, bases, namespace):
# 创建类
new_class = super().__new__(cls, name, bases, namespace)
# 添加属性验证
if hasattr(new_class, '_validators'):
for attr_name, validator in new_class._validators.items():
setattr(new_class, attr_name,
ValidatedMeta.create_validated_property(attr_name, validator))
return new_class
@staticmethod
def create_validated_property(attr_name, validator):
"""创建验证属性"""
private_name =
():
(, private_name)
():
validator(value):
ValueError()
(, private_name, value)
(getter, setter)
():
():
new_class = ().__new__(cls, name, bases, namespace)
(new_class, ):
new_class._table_name = name.lower()
new_class._field_definitions = {}
field_name, field_type new_class._fields.items():
new_class._field_definitions[field_name] = {
: field_type,
: field_name
}
new_class
:
():
.field_type = field_type
.min_value = min_value
.max_value = max_value
.value =
():
instance :
.value
():
(value, .field_type):
TypeError()
.min_value value < .min_value:
ValueError()
.max_value value > .max_value:
ValueError()
.value = value
abc ABC, abstractmethod
():
():
():
():
.validate(data):
.process(data)
ValueError()
():
_validators = {
: x: (x, ) (x) > ,
: x: (x, ) < x < ,
: x: (x, ) x
}
():
.name =
.age =
.email =
():
():
(data, ) data
(metaclass=ORMMeta):
_fields = {
: ,
: ,
: ,
:
}
():
field_name ._fields:
(, field_name, kwargs.get(field_name))
:
name = ValidatedField(, min_value=)
price = ValidatedField((, ), min_value=)
stock = ValidatedField(, min_value=)
():
.name = name
.price = price
.stock = stock
():
s1 = Singleton()
s2 = Singleton()
(, s1 s2)
product = Product(, , )
(, product.name, product.price, product.stock)
user = UserModel(=, name=, email=, age=)
(, user._table_name, user._field_definitions)
import asyncio
import aiohttp
import time
# 基础异步函数
async def fetch_data(url):
"""获取数据"""
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
async def process_data(data):
"""处理数据"""
# 模拟数据处理
await asyncio.sleep(1)
return f"处理后的数据: {data[:50]}..."
# 异步上下文管理器
class AsyncTimer:
"""异步计时器上下文管理器"""
def __init__(self):
self.start_time = None
async def __aenter__(self):
self.start_time = time.time()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
elapsed = time.time() - self.start_time
()
:
():
.current = start
.end = end
.step = step
():
():
.current >= .end:
StopAsyncIteration
value = .current
.current += .step
asyncio.sleep()
value
():
i (start, end, step):
asyncio.sleep()
i
:
():
.semaphore = asyncio.Semaphore(max_concurrent)
.tasks = []
():
():
.semaphore:
coro
task = asyncio.create_task(limited_coro())
.tasks.append(task)
task
():
results = asyncio.gather(*.tasks, return_exceptions=)
results
():
task .tasks:
task.done():
task.cancel()
asyncio.gather(*.tasks, return_exceptions=)
():
queue = asyncio.Queue(maxsize=)
():
i (count):
item =
queue.put(item)
()
asyncio.sleep()
():
:
item = queue.get()
()
asyncio.sleep()
queue.task_done()
producers = [asyncio.create_task(producer(, ))
i ()]
consumers = [asyncio.create_task(consumer())
i ()]
asyncio.gather(*producers)
queue.join()
consumer consumers:
consumer.cancel()
:
():
.base_url = base_url
.timeout = aiohttp.ClientTimeout(total=timeout)
.session =
():
.session = aiohttp.ClientSession(timeout=.timeout)
():
.session:
.session.close()
():
.session:
RuntimeError()
url =
.session.get(url, params=params) response:
response.json()
():
.session:
RuntimeError()
url =
.session.post(url, json=data) response:
response.json()
():
AsyncTimer():
asyncio.sleep()
()
()
number AsyncCounter(, ):
()
manager = AsyncTaskManager(max_concurrent=)
tasks = [
manager.add_task(asyncio.sleep()),
manager.add_task(asyncio.sleep()),
manager.add_task(asyncio.sleep())
]
results = manager.wait_all()
(, results)
AsyncHttpClient() client:
:
posts = client.get(, params={: })
(, (posts))
Exception e:
()
():
asyncio.run(async_examples())
class Vector:
"""自定义向量类"""
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
# 字符串表示
def __str__(self):
return f"Vector({self.x}, {self.y}, {self.z})"
def __repr__(self):
return f"Vector({self.x}, {self.y}, {self.z})"
# 算术运算
def __add__(self, other):
if isinstance(other, Vector):
return Vector(self.x + other.x, self.y + other.y, self.z + other.z)
return NotImplemented
def __sub__(self, other):
if isinstance(other, Vector):
return Vector(self.x - other.x, self.y - other.y, self.z - other.z)
return NotImplemented
():
(scalar, (, )):
Vector(.x * scalar, .y * scalar, .z * scalar)
():
.__mul__(scalar)
():
(scalar, (, )) scalar != :
Vector(.x / scalar, .y / scalar, .z / scalar)
():
(other, Vector):
(.x == other.x
.y == other.y
.z == other.z)
():
.__eq__(other)
():
():
index == :
.x
index == :
.y
index == :
.z
:
IndexError()
():
index == :
.x = value
index == :
.y = value
index == :
.z = value
:
IndexError()
():
([.x, .y, .z])
():
((.x, .y, .z))
():
.x != .y != .z !=
():
(.x ** + .y ** + .z ** ) **
():
Vector(-.x, -.y, -.z)
:
():
._data = initial_data {}
._access_count = {}
():
._access_count[key] = ._access_count.get(key, ) +
._data[key]
():
._data[key] = value
._access_count[key] =
():
._data[key]
key ._access_count:
._access_count[key]
():
key ._data
():
(._data)
():
(._data)
():
name ._data:
._data[name]
AttributeError()
():
name.startswith():
().__setattr__(name, value)
:
._data[name] = value
():
name.startswith():
().__delattr__(name)
name ._data:
._data[name]
:
AttributeError()
():
(._data)
():
():
._access_count.copy()
:
():
.connection_string = connection_string
.connection =
():
()
.connection =
.connection
():
()
exc_type:
()
.connection =
:
():
.func = func
.call_count =
():
.call_count +=
()
result = .func(*args, **kwargs)
()
result
():
():
v1 = Vector(, , )
v2 = Vector(, , )
()
()
()
()
()
()
()
smart_dict = SmartDict({: , : })
smart_dict.email =
()
()
()
DatabaseConnection() conn:
()
():
a + b
()
result = add(, )
(add.call_statistics)