-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathproduct_set_management.py
executable file
·183 lines (149 loc) · 6.7 KB
/
product_set_management.py
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
#!/usr/bin/env python
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This application demonstrates how to perform operations
on Product set in Cloud Vision Product Search.
For more information, see the tutorial page at
https://cloud.google.com/vision/product-search/docs/
"""
import argparse
# [START vision_product_search_delete_product_set]
# [START vision_product_search_list_product_sets]
# [START vision_product_search_get_product_set]
# [START vision_product_search_create_product_set]
from google.cloud import vision
# [END vision_product_search_delete_product_set]
# [END vision_product_search_list_product_sets]
# [END vision_product_search_get_product_set]
# [END vision_product_search_create_product_set]
# [START vision_product_search_create_product_set]
def create_product_set(
project_id, location, product_set_id, product_set_display_name):
"""Create a product set.
Args:
project_id: Id of the project.
location: A compute region name.
product_set_id: Id of the product set.
product_set_display_name: Display name of the product set.
"""
client = vision.ProductSearchClient()
# A resource that represents Google Cloud Platform location.
location_path = f"projects/{project_id}/locations/{location}"
# Create a product set with the product set specification in the region.
product_set = vision.ProductSet(
display_name=product_set_display_name)
# The response is the product set with `name` populated.
response = client.create_product_set(
parent=location_path,
product_set=product_set,
product_set_id=product_set_id)
# Display the product set information.
print('Product set name: {}'.format(response.name))
# [END vision_product_search_create_product_set]
# [START vision_product_search_list_product_sets]
def list_product_sets(project_id, location):
"""List all product sets.
Args:
project_id: Id of the project.
location: A compute region name.
"""
client = vision.ProductSearchClient()
# A resource that represents Google Cloud Platform location.
location_path = f"projects/{project_id}/locations/{location}"
# List all the product sets available in the region.
product_sets = client.list_product_sets(parent=location_path)
# Display the product set information.
for product_set in product_sets:
print('Product set name: {}'.format(product_set.name))
print('Product set id: {}'.format(product_set.name.split('/')[-1]))
print('Product set display name: {}'.format(product_set.display_name))
print('Product set index time: ')
print(product_set.index_time)
# [END vision_product_search_list_product_sets]
# [START vision_product_search_get_product_set]
def get_product_set(project_id, location, product_set_id):
"""Get info about the product set.
Args:
project_id: Id of the project.
location: A compute region name.
product_set_id: Id of the product set.
"""
client = vision.ProductSearchClient()
# Get the full path of the product set.
product_set_path = client.product_set_path(
project=project_id, location=location,
product_set=product_set_id)
# Get complete detail of the product set.
product_set = client.get_product_set(name=product_set_path)
# Display the product set information.
print('Product set name: {}'.format(product_set.name))
print('Product set id: {}'.format(product_set.name.split('/')[-1]))
print('Product set display name: {}'.format(product_set.display_name))
print('Product set index time: ')
print(product_set.index_time)
# [END vision_product_search_get_product_set]
# [START vision_product_search_delete_product_set]
def delete_product_set(project_id, location, product_set_id):
"""Delete a product set.
Args:
project_id: Id of the project.
location: A compute region name.
product_set_id: Id of the product set.
"""
client = vision.ProductSearchClient()
# Get the full path of the product set.
product_set_path = client.product_set_path(
project=project_id, location=location,
product_set=product_set_id)
# Delete the product set.
client.delete_product_set(name=product_set_path)
print('Product set deleted.')
# [END vision_product_search_delete_product_set]
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
subparsers = parser.add_subparsers(dest='command')
parser.add_argument(
'--project_id',
help='Project id. Required',
required=True)
parser.add_argument(
'--location',
help='Compute region name',
default='us-west1')
create_product_set_parser = subparsers.add_parser(
'create_product_set', help=create_product_set.__doc__)
create_product_set_parser.add_argument('product_set_id')
create_product_set_parser.add_argument('product_set_display_name')
list_product_sets_parser = subparsers.add_parser(
'list_product_sets', help=list_product_sets.__doc__)
get_product_set_parser = subparsers.add_parser(
'get_product_set', help=get_product_set.__doc__)
get_product_set_parser.add_argument('product_set_id')
delete_product_set_parser = subparsers.add_parser(
'delete_product_set', help=delete_product_set.__doc__)
delete_product_set_parser.add_argument('product_set_id')
args = parser.parse_args()
if args.command == 'create_product_set':
create_product_set(
args.project_id, args.location, args.product_set_id,
args.product_set_display_name)
elif args.command == 'list_product_sets':
list_product_sets(args.project_id, args.location)
elif args.command == 'get_product_set':
get_product_set(args.project_id, args.location, args.product_set_id)
elif args.command == 'delete_product_set':
delete_product_set(
args.project_id, args.location, args.product_set_id)