Skip to content
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

docs(user): add query strings tutorial #2239

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

bssyousefi
Copy link
Contributor

Summary of Changes

Fill in the placeholder for Query Strings in the documentation. The placeholder only existed on WSGI tutorial, so the proposed change is just about that part of the documentation.

Related Issues

#475

Pull Request Checklist

This is just a reminder about the most common mistakes. Please make sure that you tick all appropriate boxes. But please read our contribution guide at least once; it will save you a few review cycles!

If an item doesn't apply to your pull request, check it anyway to make it apparent that there's nothing to do.

  • Applied changes to both WSGI and ASGI code paths and interfaces (where applicable).
  • Added tests for changed code.
  • Prefixed code comments with GitHub nick and an appropriate prefix.
  • Coding style is consistent with the rest of the framework.
  • Updated documentation for changed code.
    • Added docstrings for any new classes, functions, or modules.
    • Updated docstrings for any modifications to existing code.
    • Updated both WSGI and ASGI docs (where applicable).
    • Added references to new classes, functions, or modules to the relevant RST file under docs/.
    • Updated all relevant supporting documentation files under docs/.
    • A copyright notice is included at the top of any new modules (using your own name or the name of your organization).
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
  • Changes (and possible deprecations) have towncrier news fragments under docs/_newsfragments/, with the file name format {issue_number}.{fragment_type}.rst. (Run towncrier --draft to ensure it renders correctly.)

If you have any questions to any of the points above, just submit and ask! This checklist is here to help you, not to deter you from contributing!

PR template inspired by the attrs project.

Copy link
Member

@CaselIT CaselIT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, great work!

I've left a few suggestions

images = self._image_store.list(max_size)
doc = {
'images': [
{'href': '/images/'+image} for image in images
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{'href': '/images/'+image} for image in images
{'href': '/images/' + image} for image in images

def __init__(self, image_store):
self._image_store = image_store

def on_get(self, req, resp):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version should be a static one like the above that serializes to msgpack, since we refer to it as "static"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad 🥲 . You're totally right.

images = self._image_store.list(max_size)
doc = {
'images': [
{'href': '/images/'+image} for image in images
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{'href': '/images/'+image} for image in images
{'href': '/images/' + image} for image in images

]
return images

As you can see the method ``list`` has been added to ``ImageStore`` in order to return list of available images smaller than ``max_size`` unless it is not zero, in which case it will behave like there was no predicament of image size.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could use -1 for the "defaut" here?

return images

As you can see the method ``list`` has been added to ``ImageStore`` in order to return list of available images smaller than ``max_size`` unless it is not zero, in which case it will behave like there was no predicament of image size.
Let's try to save some binary data as images in the service and then try to retrieve their list. Execute the following commands in order to make 3 files as images with different sizes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since these are not really images, we could use "images" here and maybe say something like

Execute the following commands in order to simulate the creation of 3 files as images with different sizes. While these are not valid PNG files, they will work for this tutorial.

self._image_store = image_store

def on_get(self, req, resp):
max_size = int(req.params.get("maxsize", 0))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be useful to showcase get_param_as_int here, so we could do

Suggested change
max_size = int(req.params.get("maxsize", 0))
max_size = int(req.get_param_as_int("maxsize", default=0, min_value=1))

(I've added a min value but we may omit it too)

@bssyousefi
Copy link
Contributor Author

I have a question regarding a part of the code.

 _IMAGE_NAME_PATTERN = re.compile(
            '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.[a-z]{2,4}$'
        )

which I used from previous parts of the docs.
the string used as the pattern will raise error in python. I guess we should use one of the following ways

 _IMAGE_NAME_PATTERN = re.compile(
            '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}.[a-z]{2,4}$'
        )
 _IMAGE_NAME_PATTERN = re.compile(
            r'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.[a-z]{2,4}$'
        )

However, I didn't try to fix it here and I thought that maybe I should first report it as an issue and then resolve it in another PR. Am I right or should I try the fix on this PR since it's a small change?

@vytas7 vytas7 changed the title add query strings to docs/user/tutorial docs(user): add query strings tutorial Jul 17, 2024
Copy link

codecov bot commented Jul 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (4d04e9b) to head (6484d52).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2239   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           64        64           
  Lines         7725      7725           
  Branches      1071      1071           
=========================================
  Hits          7725      7725           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bssyousefi bssyousefi requested a review from CaselIT July 19, 2024 11:59
Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this contribution, looks good for the most of it.

We just need to clean up things a bit before merging (see my comments inline, as well as @CaselIT's remarks).

docs/user/tutorial.rst Outdated Show resolved Hide resolved

Let's go back to the ``on_get`` method and create a dynamic response. We can use query strings to set maximum image size and get the list of all images smaller than the specified value. We will use method ``get_param_as_int`` to set a default value of ``-1`` in case no ``maxsize`` query string was provided and also to enable a minimum value validation.

.. code:: python
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to update any example files on disk? And write tests for them too?

(See also: #2247.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll look into this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vytas7, I see that a sample usage of query params is being used in the example file for wsgi.

limit = req.get_param_as_int('limit') or 50

Are we looking for a more descriptive example?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi again, no, sorry I was not very clear!

No, it has nothing to do with things_advanced.py; what I meant was that we are tracking tutorial files in the Git tree as well, we even have some rudimentary tests for them. In this case it looks like you might need to update images.py with your additions, and potentially even revise how the file develops further in this tutorial.

@bssyousefi
Copy link
Contributor Author

I have a question regarding a part of the code.

 _IMAGE_NAME_PATTERN = re.compile(
            '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.[a-z]{2,4}$'
        )

which I used from previous parts of the docs. the string used as the pattern will raise error in python. I guess we should use one of the following ways

 _IMAGE_NAME_PATTERN = re.compile(
            '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}.[a-z]{2,4}$'
        )
 _IMAGE_NAME_PATTERN = re.compile(
            r'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.[a-z]{2,4}$'
        )

However, I didn't try to fix it here and I thought that maybe I should first report it as an issue and then resolve it in another PR. Am I right or should I try the fix on this PR since it's a small change?

@vytas7 Would you please also help me on this?

@bssyousefi bssyousefi requested a review from vytas7 August 27, 2024 02:20
CaselIT
CaselIT previously approved these changes Aug 27, 2024

Let's go back to the ``on_get`` method and create a dynamic response. We can use query strings to set maximum image size and get the list of all images smaller than the specified value. We will use method ``get_param_as_int`` to set a default value of ``-1`` in case no ``maxsize`` query string was provided and also to enable a minimum value validation.

.. code:: python
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi again, no, sorry I was not very clear!

No, it has nothing to do with things_advanced.py; what I meant was that we are tracking tutorial files in the Git tree as well, we even have some rudimentary tests for them. In this case it looks like you might need to update images.py with your additions, and potentially even revise how the file develops further in this tutorial.

@bssyousefi
Copy link
Contributor Author

@vytas7 I'm very sorry for the very long delay. I mistakenly thought that all the tests were passed and I didn't got time to get back to the project since I was a little bit busy.
Thanks for you patience.
I will be more available and responsible from now on.

@vytas7
Copy link
Member

vytas7 commented Sep 23, 2024

No worries, @bssyousefi 🙂
We are now focusing on getting 4.0 out the door, so your PR is not urgent. But it will be nice to have it done when ready.

@bssyousefi
Copy link
Contributor Author

I guess it's ready. I've updated the tests for the example look

CaselIT
CaselIT previously approved these changes Sep 23, 2024
Copy link
Member

@CaselIT CaselIT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, just a couple of minor suggestions

examples/look/look/images.py Show resolved Hide resolved
examples/look/tests/test_app.py Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants