-
Notifications
You must be signed in to change notification settings - Fork 0
Update documentations #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
abbd406
Fix total iteration reporting to include warmup
Suke0811 260d099
Update: add examples and documentation for lambda-based `condition_fn…
Suke0811 bc13b33
Update: dynamically load cheatsheet into `UnifiedSpin` docstring and …
Suke0811 811ffdf
Add: examples for lambda-based `condition_fn` and `fspin_cheatsheet.m…
Suke0811 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| # include *.md | ||
| include fspin_cheatsheet.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import time | ||
| import sys | ||
| import os | ||
|
|
||
| # Add the project root to sys.path so we can import fspin | ||
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) | ||
|
|
||
| from fspin import rate | ||
|
|
||
| def lambda_sync_example(): | ||
| print("--- Synchronous Lambda Condition Example ---") | ||
| counter = 0 | ||
|
|
||
| def work(): | ||
| nonlocal counter | ||
| counter += 1 | ||
| print(f"Iteration {counter}") | ||
| time.sleep(0.1) | ||
|
|
||
| # Initialize rate control at 10 Hz | ||
| rc = rate(freq=10, is_coroutine=False) | ||
|
|
||
| # Use a lambda function as the condition | ||
| # The loop will continue as long as the counter is less than 5 | ||
| rc.spin_sync(work, condition_fn=lambda: counter < 5) | ||
|
|
||
| print(f"Loop finished after {counter} iterations.") | ||
|
|
||
| async def lambda_async_example(): | ||
| print("\n--- Asynchronous Lambda Condition Example ---") | ||
| counter = 0 | ||
|
|
||
| async def work(): | ||
| nonlocal counter | ||
| counter += 1 | ||
| print(f"Iteration {counter}") | ||
| await asyncio.sleep(0.1) | ||
|
|
||
| # Initialize rate control at 10 Hz | ||
| rc = rate(freq=10, is_coroutine=True) | ||
|
|
||
| # Use a lambda function as the condition | ||
| # The loop will continue as long as the counter is less than 5 | ||
| await rc.spin_async(work, condition_fn=lambda: counter < 5) | ||
|
|
||
| print(f"Loop finished after {counter} iterations.") | ||
|
|
||
| if __name__ == "__main__": | ||
| import asyncio | ||
| lambda_sync_example() | ||
| asyncio.run(lambda_async_example()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,35 @@ | ||
| """ | ||
| fspin: A utility for running Python functions at a fixed rate. | ||
|
|
||
| The fspin library provides tools to execute functions or coroutines repeatedly | ||
| at a consistent frequency, supporting both synchronous and asynchronous workflows. | ||
|
|
||
| Main Features: | ||
| - @spin decorator for easy loop creation. | ||
| - spin context manager for scoped background loops. | ||
| - Automatic detection of sync vs async functions. | ||
| - Support for lambda functions as stop conditions (Preferred). | ||
| - High-precision rate control with deviation compensation. | ||
|
|
||
| Quick Start: | ||
| from fspin import spin | ||
| import time | ||
|
|
||
| counter = 0 | ||
| # Preferred usage: use a lambda for the condition | ||
| @spin(freq=10, condition_fn=lambda: counter < 5) | ||
| def my_loop(): | ||
| nonlocal counter | ||
| counter += 1 | ||
| print(f"Iteration {counter}") | ||
|
|
||
| my_loop() # Blocks until counter reaches 5 | ||
|
|
||
| For detailed documentation and best practices, run 'python -m fspin' | ||
| to view the full cheatsheet. | ||
| """ | ||
| from .rate_control import RateControl as rate | ||
| from .decorators import spin as spin_decorator # Original decorator | ||
| from .spin_context import spin as spin_context_manager # New context manager | ||
| from .loop_context import loop # Keep for backward compatibility | ||
| from .unified import spin # Unified entry point that intelligently selects between decorator and context manager | ||
| from .decorators import spin as spin_decorator | ||
| from .spin_context import spin as spin_context_manager | ||
| from .loop_context import loop | ||
| from .unified import spin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import os | ||
| import sys | ||
|
|
||
| def main(): | ||
| # Get the directory where this file is located | ||
| current_dir = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
| # Path to the cheatsheet (it should be bundled with the package) | ||
| # We look for it in the package directory or the project root | ||
| cheatsheet_path = os.path.join(current_dir, "fspin_cheatsheet.md") | ||
|
|
||
| # Fallback for development environment if not found in package dir | ||
| if not os.path.exists(cheatsheet_path): | ||
| cheatsheet_path = os.path.join(current_dir, "..", "fspin_cheatsheet.md") | ||
|
|
||
| if os.path.exists(cheatsheet_path): | ||
| with open(cheatsheet_path, "r", encoding="utf-8") as f: | ||
| print(f.read()) | ||
| else: | ||
| print("fspin Cheatsheet not found.") | ||
| print("Please check the online documentation at https://github.com/Suke0811/fspin") | ||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This lookup assumes
fspin_cheatsheet.mdis bundled alongside the installed package, butsetup.pydoesn’t setinclude_package_data/package_data, so wheels built via pip won’t contain that file even withMANIFEST.in. In that common install path,python -m fspinwill always fall through to “Cheatsheet not found,” so the advertised feature won’t work. Consider shipping the file as package data (or moving it intofspin/and loading viaimportlib.resources).Useful? React with 👍 / 👎.