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

[SYNPY-1322] Object Orientated Programming Interfaces #1013

Merged
merged 34 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
625b0ba
OTEL Additions to what is/isn't a span
BryanFauble Nov 10, 2023
6105a71
>= dependencies
BryanFauble Nov 10, 2023
cebe138
Add in document and script around benchmarking
BryanFauble Nov 14, 2023
89cd7c7
Code review feedback
BryanFauble Nov 16, 2023
40250fb
Link to tutorial section
BryanFauble Nov 16, 2023
dd0eeb0
Adjust OTEL dependencies
BryanFauble Nov 16, 2023
03cf453
Mark test as flaky
BryanFauble Nov 16, 2023
dcc29f0
Publish branch to remote
BryanFauble Nov 16, 2023
77a8d6b
Import File earlier
BryanFauble Nov 16, 2023
b27cf12
Merge branch 'develop' into SYNPY-1322-OOP-POC
BryanFauble Nov 16, 2023
244c69f
Support for annotations,cache syn client instance
BryanFauble Nov 20, 2023
55ee852
Remove httpx
BryanFauble Nov 20, 2023
65a1c70
Merge branch 'develop' into SYNPY-1322-OOP-POC
BryanFauble Nov 28, 2023
57da4e1
Adding support for Table
BryanFauble Nov 30, 2023
5bdf4c4
Adding delete row note
BryanFauble Nov 30, 2023
1a11b17
Adding delete table
BryanFauble Nov 30, 2023
f988b5c
More example scripts
BryanFauble Nov 30, 2023
5d5f9f5
Remove comment
BryanFauble Nov 30, 2023
6b5b9af
Prevent creating instance for calling class method
BryanFauble Nov 30, 2023
86a474e
Correcting OTEL context propogation
BryanFauble Dec 1, 2023
dfbddcc
Add note about assuming annotation type
BryanFauble Dec 1, 2023
6b74371
Merge branch 'develop' into SYNPY-1322-OOP-POC
BryanFauble Dec 7, 2023
84a5369
Adding more verbose examples for current project interface
BryanFauble Dec 8, 2023
d370b46
simplify the annotation interface (#1022)
BryanFauble Dec 12, 2023
3aec4ff
[SYNPY-1345] Migrate to mkdocstrings (#1023)
BryanFauble Dec 12, 2023
93bacca
Making sticky nav tabs per team meeting
BryanFauble Dec 12, 2023
bbe905f
Merge branch 'develop' into SYNPY-1322-OOP-POC
BryanFauble Jan 16, 2024
410de22
Remove content not needed
BryanFauble Jan 16, 2024
be9f193
Remove content that is not needed
BryanFauble Jan 16, 2024
cc049b0
Merge develop and consistency changes
BryanFauble Jan 16, 2024
53e4c22
Merge branch 'develop' into SYNPY-1322-OOP-POC
BryanFauble Jan 17, 2024
8fef247
Correct for some changes
BryanFauble Jan 17, 2024
a19d77a
Clean up doc structure
BryanFauble Jan 22, 2024
377df74
Code review updates
BryanFauble Jan 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions synapseclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
SubmissionViewSchema,
MaterializedViewSchema,
Dataset,
delete_rows,
)
from .team import Team, UserProfile, UserGroupHeader, TeamMember
from .wiki import Wiki
Expand Down Expand Up @@ -69,6 +70,7 @@
# functions
"login",
"build_table",
"delete_rows",
"as_table_columns",
"check_for_updates",
"release_notes",
Expand Down
7 changes: 7 additions & 0 deletions synapseclient/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# These are all of the models that are used by the Synapse client.
from .annotations import set_annotations


__all__ = [
"set_annotations",
]
44 changes: 44 additions & 0 deletions synapseclient/api/annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
The purpose of this module is to provide any functions that are needed to interact with
annotations that are not cleanly provided by the synapseclient library.
"""
import json

from dataclasses import asdict

from typing import TYPE_CHECKING, Optional
from synapseclient import Synapse
from opentelemetry import context

if TYPE_CHECKING:
from synapseclient.models import Annotations


def set_annotations(
annotations: "Annotations",
synapse_client: Optional[Synapse] = None,
opentelemetry_context: Optional[context.Context] = None,
):
"""Call to synapse and set the annotations for the given input.

:param annotations: The annotations to set. This is expected to have the id, etag, and annotations filled in.
:param synapse_client: If not passed in or None this will use the last client from the `.login()` method.
:param opentelemetry_context: OpenTelemetry context to propogate to this function to use for tracing. Used
cases where concurrent operations need to be linked to parent spans.
:return: _description_
"""
annotations_dict = asdict(annotations)

# TODO: Is there a more elegant way to handle this - This is essentially being used
Copy link
Contributor

@BWMac BWMac Nov 21, 2023

Choose a reason for hiding this comment

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

There is another dataclass library that adds some more functionality. Its asdict function has an exclude argument that you can pass to leave out class attributes you don't want in your dictionary:

from dataclasses import dataclass
from dataclass_wizard.dumpers import asdict

@dataclass
class Foo:
    bar: str
    baz: int

foo = Foo('bar', 42)
print(asdict(foo, exclude=['baz']))

> {'bar': 'bar'}

You'd still be hard-coding the excluded values though. Not sure of any other ways aside from implementing an asdict method specific to the class that excludes attributes not used in the API.

Edit: if you used a base class you could potentially implement an asdict function that could be reused across all extending classes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea - but i'm starting to think in the other direction now. What I mean by this is specify only those things I want to include and have a dataclass -> json/dict mapping process to format the input for the REST API as the API is expecting.

# TODO: to remove any fields that are not expected by the REST API.
filtered_dict = {
k: v for k, v in annotations_dict.items() if v is not None and k != "is_loaded"
}

# TODO: This `restPUT` returns back a dict (or string) - Could we use:
# TODO: https://github.com/konradhalas/dacite to convert the dict to an object?
Copy link
Contributor

@BWMac BWMac Nov 21, 2023

Choose a reason for hiding this comment

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

I do like the look of dacite, but is that more efficient than just wrapping the response in the class object like we have done elsewhere?

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'm not sure yet - I'll have to give it some more thought.

One of the things we will need to do it at least have a thin translation layer between the REST api and the dataclasses because the names we are giving them in the python client are snake_case, vs the REST api is all in camelCase.

return Synapse.get_client(synapse_client=synapse_client).restPUT(
f"/entity/{annotations.id}/annotations2",
body=json.dumps(filtered_dict),
opentelemetry_context=opentelemetry_context,
)
Loading
Loading