Warm greetings! This lesson introduces data streams, which are essentially continuous datasets. Think of a weather station or a gaming application gathering data per second — both are generating data streams! We will master handling these data streams using Python, learning to access elements, slice segments, and even convert these streams into strings for easier handling.
In Python, data streams are commonly mirrored as lists. Python also facilitates additional data structures like tuples or dictionaries.
Consider a straightforward Python class named DataStream
. This class encapsulates operations related to data streams in our program:
Python1class DataStream: 2 def __init__(self, data): 3 self.data = data
To use it, we create a sample data stream as an instance of our DataStream
class, where each element is a dictionary:
Python1stream = DataStream([ 2 {"id": 1, "value": 100}, 3 {"id": 2, "value": 200}, 4 {"id": 3, "value": 300}, 5 {"id": 4, "value": 400}, 6])
To look into individual elements of a data stream, we use indexing. The get()
method we introduce below fetches the i
-th element from the data stream:
Python1class DataStream: 2 def __init__(self, data): 3 self.data = data 4 5 def get(self, i): 6 return self.data[i]
Here, we can see the get()
method in action:
Python1stream = DataStream([ 2 {"id": 1, "value": 100}, 3 {"id": 2, "value": 200}, 4 {"id": 3, "value": 300}, 5 {"id": 4, "value": 400} 6]) 7 8print(stream.get(2)) # It prints: {'id': 3, 'value': 300} 9print(stream.get(-1)) # It prints: {'id': 4, 'value': 400}
In essence, stream.get(2)
fetched us {'id': 3, 'value': 300}
— the third element (since indexing starts from 0
). At the same time, stream.get(-1)
fetches the last element, which is {'id': 4, 'value': 400}
.
Fetching a range of elements rather than a single one is facilitated by slicing. A slice data[i:j]
crafts a new list containing elements from position i
(inclusive) to j
(exclusive) in the data stream. We introduce a slice()
method to support slicing:
Python1class DataStream: 2 def __init__(self, data): 3 self.data = data 4 5 def get(self, i): 6 return self.data[i] 7 8 def slice(self, i, j): 9 return self.data[i:j]
Here's a quick usage example:
Python1stream = DataStream([ 2 {"id": 1, "value": 100}, 3 {"id": 2, "value": 200}, 4 {"id": 3, "value": 300}, 5 {"id": 4, "value": 400} 6]) 7 8print(stream.slice(1, 3)) # It prints: [{'id': 2, 'value': 200}, {'id': 3, 'value': 300}]
For better readability, we may wish to convert our data streams into strings. To ensure the conversion works consistently, we will create a custom string representation for our data elements. Have a look at the to_string
method in action:
Python1class DataStream: 2 def __init__(self, data): 3 self.data = data 4 5 def get(self, i): 6 return self.data[i] 7 8 def slice(self, i, j): 9 return self.data[i:j] 10 11 def to_string(self): 12 return '[' + ', '.join(str(item) for item in self.data) + ']'
To see it in action:
Python1stream = DataStream([ 2 {"id": 1, "value": 100}, 3 {"id": 2, "value": 200}, 4 {"id": 3, "value": 300}, 5 {"id": 4, "value": 400} 6]) 7 8print(stream.to_string()) # It prints: [{'id': 1, 'value': 100}, {'id': 2, 'value': 200}, {'id': 3, 'value': 300}, {'id': 4, 'value': 400}]
In later lessons, we'll expand data stream functionality to include more complex data stream handling capabilities.
In this lesson, we've explored data streams, discovered how to represent and manipulate them using native Python data structures, especially lists, and encapsulated operations on data streams with Python classes.
Now it's time to apply your newfound knowledge in the practice exercises that follow!