-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path__init__.py
More file actions
224 lines (155 loc) · 5.08 KB
/
__init__.py
File metadata and controls
224 lines (155 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""
!!! warning
The Sift Client is experimental and is subject to change.
# Sift Client Library
This library provides a high-level Python client for interacting with Sift APIs. It offers both synchronous and
asynchronous interfaces, strong type checking, and a Pythonic API design.
## Installation
```bash
pip install sift-stack-py
```
## Getting Started
### Initializing the Client
You can initialize the Sift client with your API key and service URLs:
```python
from sift_client import SiftClient
from datetime import datetime
# Initialize with individual parameters
client = SiftClient(
api_key="your-api-key",
grpc_url="your-sift-grpc-url",
rest_url="your-sift-rest-url"
)
# Or use a connection configuration
from sift_client.transport import SiftConnectionConfig
config = SiftConnectionConfig(
api_key="your-api-key",
grpc_url="your-sift-grpc-url",
rest_url="your-sift-rest-url"
)
client = SiftClient(connection_config=config)
```
The `SiftConnectionConfig` provides access to additional configuration options such as `use_ssl` and `cert_via_openssl`.
### Using Synchronous and Asynchronous APIs
The Sift client provides both synchronous and asynchronous versions of all APIs. You can choose the one that best fits
your application's needs.
#### Synchronous API
The synchronous API is perfect for scripts, notebooks, and applications that don't need asynchronous operation:
```python
# Get an asset by ID
asset = client.assets.get(asset_id="asset123")
# List assets with filtering
assets = client.assets.list_(
name_contains="example",
created_after=datetime(2023, 1, 1),
include_archived=False
)
# Find a single asset matching criteria
asset = client.assets.find(name="my-asset")
```
#### Asynchronous API
The asynchronous API is ideal for high-performance applications and services that need to make concurrent API calls:
```python
import asyncio
async def get_asset_async():
# Get an asset by ID asynchronously
asset = await client.assets_async.get(asset_id="asset123")
# Running Sync within async also works
some_other_asset = client.assets.get(asset_id="asset456")
return asset
# Run in an async context
asset = asyncio.run(get_asset_async())
```
### Working with Sift Types
Sift types (like `Asset`, `Run`, etc.) are immutable Pydantic models that provide a convenient interface for working
with Sift resources.
#### Accessing Properties
```python
# Get an asset
asset = client.assets.get(asset_id="asset123")
# Access properties
print(f"Asset name: {asset.name}")
print(f"Created on: {asset.created_date}")
print(f"Tags: {', '.join(asset.tags)}")
print(f"Is archived: {asset.is_archived}")
```
#### Using Methods on Sift Types
Sift types have convenient methods for common operations. These methods use the synchronous API internally.
**Using these methods will update the instance in-place.**
```python
# Get an asset
asset = client.assets.get(asset_id="asset123")
# Archive the asset
asset.archive(archive_runs=True)
# Update the asset
asset.update({
"tags": ["updated", "example"]
})
```
> **Note:** Type methods only work with the synchronous API. If you need to use the asynchronous API, you should use the
> resource APIs directly.
#### Creating Update Models
For more complex updates, you can create update models (instead of a key-value dictionary):
```python
from sift_client.types.asset import AssetUpdate
# Create an update model
update = AssetUpdate(tags=["new", "tags"])
# Apply the update
asset = client.assets.update(asset="asset123", update=update)
# Or using the asset method
asset = client.assets.get(asset_id="asset123").update(update)
```
## Advanced Usage
### Working with Tags
Tags are a powerful way to organize and filter your assets:
```python
# Add tags when updating an asset
asset.update({
"tags": ["production", "model-v1", "trained"]
})
# Filter assets by tags
production_assets = client.assets.list_(
tags=["production"]
)
```
### Filtering Assets
The client provides various ways to filter different Sift types:
```python
# Filter by name (exact match)
assets = client.assets.list_(name="my-model")
# Filter by name (contains)
assets = client.assets.list_(name_contains="model")
# Filter by name (regex)
assets = client.assets.list_(name_regex="model-v[0-9]+")
# Filter by creation date
assets = client.assets.list_(
created_after=datetime(2023, 1, 1),
created_before=datetime(2023, 12, 31)
)
# Filter by modification date
assets = client.assets.list_(
modified_after=datetime(2023, 6, 1)
)
# Include archived assets
all_assets = client.assets.list_(include_archived=True)
# Limit the number of results
recent_assets = client.assets.list_(
limit=10,
order_by="modified_date desc"
)
```
"""
import logging
import sys
from sift_client.client import SiftClient
from sift_client.transport import SiftConnectionConfig
__all__ = [
"SiftClient",
"SiftConnectionConfig",
]
logger = logging.getLogger("sift_client")
logging.basicConfig(
level=logging.ERROR, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
handler = logging.StreamHandler(sys.stdout)
logger.addHandler(handler)