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

Update python code example to more idiomatic way of updating an item #106

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

AndyTitu
Copy link
Contributor

This PR removes the by index item field updating and uses python built-in functions for providing correctness and a more authentic feel to the examples.

updated_item = await client.items.put(item)
# Update a field in your item (change the password)
updated_item = Item(**dict(item))
next(f for f in updated_item.fields if f.title == "password").value = "new_pass"

Choose a reason for hiding this comment

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

It will raise StopIteration exception if nothing is found.
We can handle it like this:

try:
    password_field = next(f for f in updated_item.fields if f.title == "password")
    password_field.value = "new_pass"
    updated_item = await client.items.put(updated_item)
except StopIteration:
    print("Password field not found")

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.

2 participants