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

Add string sets to helpers #55

Merged
merged 5 commits into from
May 19, 2020
Merged

Add string sets to helpers #55

merged 5 commits into from
May 19, 2020

Conversation

austinbyers
Copy link
Contributor

Background

Follow- up to #54 which adds string sets as well.

Changes

  • Support string sets
    • put_string_set - overwrite the entire set at once
    • add_to_string_set - add one or more elements to a set
    • remove_from_string_set - remove one or more elements from a set
    • reset_string_set - remove all entries in a set
  • Use ADD instead of SET for incrementing the counter. The AWS docs recommend SET, but ADD will work even if the attribute doesn't exist yet (unlike SET). This saves us an extra API call for new counter keys
  • Fixed a typo from Add counter helper functions #54

Testing

  • make fmt test lint

I added the test cases from #54 as well as the ones I used for string sets as some simple assert statements. I don't think we need the full unit testing library here, but I do want to keep the testing code around so (a) we can test changes and (b) users can see how we the functions are intended to be used

@austinbyers austinbyers requested a review from kostaspap as a code owner May 18, 2020 21:32
@austinbyers austinbyers changed the title Add string sets Add string sets to helpers May 18, 2020


def reset_counter(key: str) -> None:
"""Reset a counter to 0."""
kv_table().put_item(Item={'key': key, _COUNT_COL: 0})


def set_counter_expiration(key: str, epoch_seconds: int) -> None:
"""Configure the counter to automatically expire at the given time.
def set_key_expiration(key: str, epoch_seconds: int) -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Works for both string sets and counters (which can coexist under the same key), renaming for generality



def remove_from_string_set(key: str, val: Union[str,
Sequence[str]]) -> Set[str]:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

yapf made me do it this way, though I strongly disagree :P

@austinbyers austinbyers self-assigned this May 18, 2020
Makefile Outdated
@@ -13,7 +13,7 @@ deps-update:

lint:
yapf $(analysis_directories) --diff --parallel --recursive --style google
bandit -r $(analysis_directories)
bandit -r $(analysis_directories) --skip B101
Copy link
Contributor

Choose a reason for hiding this comment

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

What check is B101? Could you add a comment explaining why/what is being skipped, or use a long form name if that's possible (like in pylint)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Long-form isn't possible with bandit, but I can add a comment. It's just warning about the "assert" statements, which are ignored when compiling Python into bytecode

assert get_string_set('panther') == set()


if __name__ == '__main__':
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we include this and the tests in a separate file? IIRC this file is shown in the UI cc @nhakmiller

Copy link
Contributor

@nhakmiller nhakmiller May 18, 2020

Choose a reason for hiding this comment

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

After my most recent change this is no longer shown in the UI, it is saved as panther_oss_helpers and only accessible from the CLI (until if/when we add support for multiple globals in the UI).

Copy link
Contributor Author

@austinbyers austinbyers May 19, 2020

Choose a reason for hiding this comment

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

I'm happy to move this somewhere else in the future, I just wanted to quickly save it somewhere. I wasn't sure how best to do an "integration test" in this repo



def reset_counter(key: str) -> None:
"""Reset a counter to 0."""
kv_table().put_item(Item={'key': key, _COUNT_COL: 0})


def set_counter_expiration(key: str, epoch_seconds: int) -> None:
"""Configure the counter to automatically expire at the given time.
def set_key_expiration(key: str, epoch_seconds: int) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Thinking maybe better to rename to set_approx(imate)_key_expiration? The description clarifies that items will be deleted within 48hrs of expiration time, but thinking have it in the name will make it more obvious (few people read documentation :P )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know what you mean, but approx_expiration_time to me suggests the actual value saved to Dynamo may not be what you passed to the function. The expiration time is very precise and written to the table exactly as specified and the item is then guaranteed to expire. It's just the system may not honor it immediately

Typically, from what I've seen so far, the expiration happens within a few minutes. I think it's reasonable to assume people will not expect a distributed system to respond immediately

@austinbyers austinbyers merged commit 9b47214 into master May 19, 2020
@austinbyers austinbyers deleted the austin-sets branch May 19, 2020 18:19
egibs pushed a commit that referenced this pull request Jan 30, 2024
Co-authored-by: Evan Gibler <evan.gibler@panther.com>
egibs pushed a commit that referenced this pull request Jan 30, 2024
#1076)

Co-authored-by: akozlovets098 <95437895+akozlovets098@users.noreply.github.com>
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.

4 participants