Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: jimmc414-claude-code-plugin-marketplace-apply-decorator-wrap3description: apply-decorator-wrap4---56# apply-decorator-wrap78## When to Use9- Adding caching/memoization10- Timing function execution11- Logging function calls12- Input validation13- Retry logic14- Any cross-cutting concern1516## When NOT to Use17- Behavior is specific to one function18- Would obscure function's purpose19- Simple inline code is clearer2021## The Pattern2223Decorators wrap functions to add behavior before, after, or around the original.2425```python26def timing(func):27 """Decorator to time function execution."""28 def wrapper(*args, **kwargs):29 start = time.time()30 result = func(*args, **kwargs)31 elapsed = time.time() - start32 print(f"{func.__name__} took {elapsed:.3f}s")33 return result34 return wrapper3536@timing37def slow_function():38 time.sleep(1)39 return "done"4041# Equivalent to: slow_function = timing(slow_function)42```4344## Example (from pytudes)4546```python47# Memoization decorator (ngrams.py)48def memo(f):49 """Memoize function f."""50 table = {}5152 def fmemo(*args):53 if args not in table:54 table[args] = f(*args)55 return table[args]5657 fmemo.memo = table # Expose cache58 return fmemo5960@memo61def segment(text):62 """Optimal word segmentation."""63 if not text:64 return []65 candidates = ([first] + segment(rest)66 for first, rest in splits(text))67 return max(candidates, key=word_prob)6869# Using functools for cleaner decorators70from functools import cache, lru_cache, wraps7172@cache # Built-in memoization73def fibonacci(n):74 if n <= 1:75 return n76 return fibonacci(n-1) + fibonacci(n-2)7778@lru_cache(maxsize=1000) # Limited cache size79def expensive_lookup(key):80 ...8182# Reusable decorator with parameter83cache = lru_cache(None) # Alias for unlimited cache8485@cache86def expressions(numbers):87 ...8889@cache90def segment(text):91 ...92```9394## Key Principles951. **Wrapper preserves signature**: Use `@functools.wraps`962. **Return wrapper**: Decorator returns the wrapped function973. **Expose internals**: Attach cache/state as attribute984. **Stack decorators**: Multiple decorators apply bottom-up995. **Decorator factories**: `@lru_cache(n)` returns decorator100101---102> Converted and distributed by [TomeVault](https://tomevault.io/claim/jimmc414) — claim your Tome and manage your conversions.103<!-- tomevault:4.0:skill_md:2026-04-13 -->
Run npx skillmds@latest add tomevault-io/jimmc414-claude-code-plugin-marketplace-apply-decorator-wrap in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
apply-decorator-wrap It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
tomevault-io (@tomevault-io) published this skill. Their other Agent Skills are listed on their SkillMD profile.