Skip to content

Commit 83ef9f1

Browse files
adamtheturtleclaude
andcommitted
Extract shared image utilities to reduce duplication
Move _ImageType alias and _get_image_data function to new _image_utils module and import from both vws.py and query.py, eliminating duplicate code. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
1 parent cdc9e43 commit 83ef9f1

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

src/vws/_image_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Image utility functions shared across VWS modules."""
2+
3+
import io
4+
from typing import BinaryIO
5+
6+
from beartype import beartype
7+
8+
ImageType = io.BytesIO | BinaryIO
9+
10+
11+
@beartype
12+
def get_image_data(image: ImageType) -> bytes:
13+
"""Get the data of an image file."""
14+
original_tell = image.tell()
15+
image.seek(0)
16+
image_data = image.read()
17+
image.seek(original_tell)
18+
return image_data

src/vws/query.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
"""Tools for interacting with the Vuforia Cloud Recognition Web APIs."""
22

33
import datetime
4-
import io
54
import json
65
from http import HTTPMethod, HTTPStatus
7-
from typing import Any, BinaryIO
6+
from typing import Any
87

98
from beartype import BeartypeConf, beartype
109
from urllib3.filepost import encode_multipart_formdata
1110
from vws_auth_tools import authorization_header, rfc_1123_date
1211

12+
from vws._image_utils import ImageType as _ImageType
13+
from vws._image_utils import get_image_data as _get_image_data
1314
from vws.exceptions.cloud_reco_exceptions import (
1415
AuthenticationFailureError,
1516
BadImageError,
@@ -25,18 +26,6 @@
2526
from vws.reports import QueryResult, TargetData
2627
from vws.transports import RequestsTransport, Transport
2728

28-
_ImageType = io.BytesIO | BinaryIO
29-
30-
31-
@beartype
32-
def _get_image_data(image: _ImageType) -> bytes:
33-
"""Get the data of an image file."""
34-
original_tell = image.tell()
35-
image.seek(0)
36-
image_data = image.read()
37-
image.seek(original_tell)
38-
return image_data
39-
4029

4130
@beartype(conf=BeartypeConf(is_pep484_tower=True))
4231
class CloudRecoService:

src/vws/vws.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"""Tools for interacting with Vuforia APIs."""
22

33
import base64
4-
import io
54
import json
65
import time
76
from datetime import date
87
from http import HTTPMethod, HTTPStatus
9-
from typing import BinaryIO
108

119
from beartype import BeartypeConf, beartype
1210

11+
from vws._image_utils import ImageType as _ImageType
12+
from vws._image_utils import get_image_data as _get_image_data
1313
from vws._vws_request import target_api_request
1414
from vws.exceptions.custom_exceptions import (
1515
ServerError,
@@ -45,18 +45,6 @@
4545
from vws.response import Response
4646
from vws.transports import RequestsTransport, Transport
4747

48-
_ImageType = io.BytesIO | BinaryIO
49-
50-
51-
@beartype
52-
def _get_image_data(image: _ImageType) -> bytes:
53-
"""Get the data of an image file."""
54-
original_tell = image.tell()
55-
image.seek(0)
56-
image_data = image.read()
57-
image.seek(original_tell)
58-
return image_data
59-
6048

6149
@beartype(conf=BeartypeConf(is_pep484_tower=True))
6250
class VWS:

0 commit comments

Comments
 (0)