@@ -41,7 +41,7 @@ def handle_stream_errors(chunk):
41
41
42
42
43
43
class BaseIterator (ABC ):
44
- """Abstract base class for creation of new iterators.
44
+ """Abstract base class for Inference Streaming iterators.
45
45
46
46
Provides a skeleton for customization requiring the overriding of iterator methods
47
47
__iter__ and __next__.
@@ -53,45 +53,75 @@ class BaseIterator(ABC):
53
53
2. Needs to implement logic in __next__ to:
54
54
2.1. Concatenate and provide next chunk of response from botocore.eventstream.EventStream.
55
55
While doing so parse the response_chunk["PayloadPart"]["Bytes"].
56
- 2.2. Perform deserialization of response chunk based on expected response type.
57
- 2.3. If PayloadPart not in EventStream response, handle Errors .
56
+ 2.2. If PayloadPart not in EventStream response, handle Errors
57
+ [Recommended to use `iterators.handle_stream_errors` method] .
58
58
"""
59
59
60
- def __init__ (self , stream ):
60
+ def __init__ (self , event_stream ):
61
61
"""Initialises a Iterator object to help parse the byte event stream input.
62
62
63
63
Args:
64
- stream : (botocore.eventstream.EventStream): Event Stream object to be iterated.
64
+ event_stream : (botocore.eventstream.EventStream): Event Stream object to be iterated.
65
65
"""
66
- self .stream = stream
66
+ self .event_stream = event_stream
67
67
68
68
@abstractmethod
69
69
def __iter__ (self ):
70
- """Abstract __iter__ method, returns an iterator object itself"""
70
+ """Abstract method, returns an iterator object itself"""
71
71
return self
72
72
73
73
@abstractmethod
74
74
def __next__ (self ):
75
- """Abstract __next__ method, is responsible for returning the next element in the
76
- iteration"""
77
- pass
75
+ """Abstract method, is responsible for returning the next element in the iteration"""
76
+
77
+
78
+ class ByteIterator (BaseIterator ):
79
+ """A helper class for parsing the byte Event Stream input to provide Byte iteration."""
80
+
81
+ def __init__ (self , event_stream ):
82
+ """Initialises a BytesIterator Iterator object
83
+
84
+ Args:
85
+ event_stream: (botocore.eventstream.EventStream): Event Stream object to be iterated.
86
+ """
87
+ super ().__init__ (event_stream )
88
+ self .byte_iterator = iter (event_stream )
89
+
90
+ def __iter__ (self ):
91
+ """Returns an iterator object itself, which allows the object to be iterated.
92
+
93
+ Returns:
94
+ iter : object
95
+ An iterator object representing the iterable.
96
+ """
97
+ return self
98
+
99
+ def __next__ (self ):
100
+ """Returns the next chunk of Byte directly."""
101
+ # Even with "while True" loop the function still behaves like a generator
102
+ # and sends the next new byte chunk.
103
+ while True :
104
+ chunk = next (self .byte_iterator )
105
+ if "PayloadPart" not in chunk :
106
+ # handle API response errors and force terminate.
107
+ handle_stream_errors (chunk )
108
+ # print and move on to next response byte
109
+ print ("Unknown event type:" + chunk )
110
+ continue
111
+ return chunk ["PayloadPart" ]["Bytes" ]
78
112
79
113
80
114
class LineIterator (BaseIterator ):
81
- """
82
- A helper class for parsing the byte stream input and provide iteration on lines with
83
- '\n ' separators.
84
- """
115
+ """A helper class for parsing the byte Event Stream input to provide Line iteration."""
85
116
86
- def __init__ (self , stream ):
87
- """Initialises a Iterator object to help parse the byte stream input and
88
- provide iteration on lines with '\n ' separators
117
+ def __init__ (self , event_stream ):
118
+ """Initialises a LineIterator Iterator object
89
119
90
120
Args:
91
- stream : (botocore.eventstream.EventStream): Event Stream object to be iterated.
121
+ event_stream : (botocore.eventstream.EventStream): Event Stream object to be iterated.
92
122
"""
93
- super ().__init__ (stream )
94
- self .byte_iterator = iter (self .stream )
123
+ super ().__init__ (event_stream )
124
+ self .byte_iterator = iter (self .event_stream )
95
125
self .buffer = io .BytesIO ()
96
126
self .read_pos = 0
97
127
@@ -105,7 +135,8 @@ def __iter__(self):
105
135
return self
106
136
107
137
def __next__ (self ):
108
- """
138
+ r"""Returns the next Line for an Line iterable.
139
+
109
140
The output of the event stream will be in the following format:
110
141
111
142
```
@@ -146,8 +177,9 @@ def __next__(self):
146
177
continue
147
178
raise
148
179
if "PayloadPart" not in chunk :
149
- # handle errors within API Response if any .
180
+ # handle API response errors and force terminate .
150
181
handle_stream_errors (chunk )
182
+ # print and move on to next response byte
151
183
print ("Unknown event type:" + chunk )
152
184
continue
153
185
self .buffer .seek (0 , io .SEEK_END )
0 commit comments