-
Notifications
You must be signed in to change notification settings - Fork 14
feat(sdk): add util module with collect helper func; update spin-kv example to suit #139
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
+137
−15
Merged
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,31 +1,19 @@ | ||
| from typing import TypeVar, Tuple, List | ||
| from componentize_py_types import Result, Err | ||
| from componentize_py_async_support.streams import StreamReader | ||
| from componentize_py_async_support.futures import FutureReader | ||
| from spin_sdk import http, key_value | ||
| from spin_sdk import http, key_value, util | ||
| from spin_sdk.http import Request, Response | ||
| from spin_sdk.key_value import Store | ||
|
|
||
| class WasiHttpHandler030Rc20260315(http.Handler): | ||
| async def handle_request(self, request: Request) -> Response: | ||
| with await key_value.open_default() as a: | ||
| await a.set("test", bytes("hello world!", "utf-8")) | ||
| print(await get_keys(a)) | ||
| print(await util.collect(await a.get_keys())) | ||
| print(await a.exists("test")) | ||
| print(await a.get("test")) | ||
| await a.delete("test") | ||
| print(await get_keys(a)) | ||
| print(await util.collect(await a.get_keys())) | ||
|
|
||
| return Response( | ||
| 200, | ||
| {"content-type": "text/plain"}, | ||
| bytes("Hello from Python!", "utf-8") | ||
| ) | ||
|
|
||
| async def get_keys(store: Store) -> list[str]: | ||
| stream, future = await store.get_keys() | ||
| with stream, future: | ||
| keys = [] | ||
| while not stream.writer_dropped: | ||
| keys += await stream.read(max_count=100) | ||
| return keys |
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,26 @@ | ||
| """Module for general-purpose utility functions""" | ||
|
|
||
| from typing import TypeVar, Tuple, List | ||
| from componentize_py_types import Result, Err | ||
| from componentize_py_async_support.streams import StreamReader | ||
| from componentize_py_async_support.futures import FutureReader | ||
|
|
||
| T = TypeVar('T') | ||
| E = TypeVar('E') | ||
|
|
||
| async def collect(tuple: Tuple[StreamReader[T], FutureReader[Result[None, E]]]) -> List[T]: | ||
| """ | ||
| Collect all items from the StreamReader portion of the provided Tuple and return them in a List, | ||
| verifying the FutureReader result upon stream completion and, if it is error, raising it as an exception. | ||
| """ | ||
| stream = tuple[0] | ||
| future = tuple[1] | ||
| collected = [] | ||
| with stream, future: | ||
| while not stream.writer_dropped: | ||
| collected += await stream.read(128) | ||
| result = await future.read() | ||
| if isinstance(result, Err): | ||
| raise result | ||
| else: | ||
| return collected | ||
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.
Will this be usable for PostgreSQL and SQLite rows? The APIs there return
(list<column>, stream<row>, future<result>)rather than the mere 2-tuple ofget-keys- can users still call this ascollect(stream, future)or would they need to re-tuple the stream and future?Wow I explained that badly but hopefully makes sense.
Uh oh!
There was an error while loading. Please reload this page.
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.
You could do something like this:
Uh oh!
There was an error while loading. Please reload this page.
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.
So yes, you'd need to re-tuple, but the syntax for that is pretty simple.
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.
@itowlson merging this to get some docs URLs to point to for the new util module and collect helper, but please do file a follow-up if I or @dicej can attend to improvements here.