Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit901f545

Browse files
committed
Update blog link and title in TheNavbar component to reflect new decorators article
1 parent81fe9ad commit901f545

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

‎docs/blog/python-decorators-for-beginners.md‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: 'Python Decorators - Simple Patterns to Level Up Your Code - Python Cheat
33
description:Decorators are one of Python's most elegant features, allowing you to modify the behavior of functions or methods in a clean and readable way.
44
date:Aug 16, 2025
55
updated:Aug 16, 2025
6-
tags:python, intermediate,beta
6+
tags:python, intermediate,decorators
77
socialImage:/blog/python-decorators.jpg
88
---
99

@@ -192,7 +192,6 @@ def retry(max_attempts=3):
192192
if attempt== max_attempts-1:
193193
print("All attempts failed!")
194194
raise
195-
returnNone
196195
return wrapper
197196
return decorator
198197

@@ -213,31 +212,47 @@ Sometimes you need to be gentle with APIs or databases:
213212

214213
```python
215214
import time
215+
import functools
216216

217217
def rate_limit(seconds):
218+
"""
219+
A decorator to limit how frequently a function can be called.
220+
"""
218221
def decorator(func):
219-
last_called = [^0]# Use list to store mutable value
222+
# Use a list to store a mutable float value for the last call time.
223+
# This allows the inner wrapper function to modify it.
224+
last_called_at = [0.0]
220225

221226
@functools.wraps(func)
222227
def wrapper(*args,**kwargs):
223-
elapsed = time.time()- last_called
224-
if elapsed< seconds:
225-
time.sleep(seconds- elapsed)
228+
# Calculate time elapsed since the last call
229+
elapsed = time.time()- last_called_at[0]
230+
wait_time = seconds- elapsed
231+
232+
# If not enough time has passed, sleep for the remainder
233+
if wait_time>0:
234+
time.sleep(wait_time)
226235

227-
last_called = time.time()
236+
# Update the last call time and execute the function
237+
last_called_at[0] = time.time()
228238
return func(*args,**kwargs)
229239

230240
return wrapper
231241
return decorator
232242

233-
@rate_limit(1)#Atmostonce per second
243+
@rate_limit(1)#Allow atmostone call per second
234244
def call_api():
235245
print(f"API called at{time.time():.2f}")
236246

237-
# These will be spaced out by 1 second each
247+
# Thesecallswill be spaced out by approximately 1 second each
238248
call_api()
239249
call_api()
240250
call_api()
251+
252+
# Expected Output:
253+
# API called at 1723823038.50
254+
# API called at 1723823039.50
255+
# API called at 1723823040.50
241256
```
242257

243258

‎src/components/layout/TheNavbar.vue‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ const isDark = useDark()
4040
</template>
4141
<template #message>
4242
<router-link
43-
to="/blog/python-pathlib-essentials"
43+
to="/blog/python-decorators-for-beginners"
4444
class="hover:text-sky-500"
4545
>
46-
Essential Path Operations
46+
Python Decorators Patterns
4747
</router-link>
4848
</template>
4949
</base-badge-notice>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp