Last active
November 30, 2015 04:35
-
-
Save harishvc/20e0e44cfd9416d2acea to your computer and use it in GitHub Desktop.
Read small chunks from big buffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Reference: http://pythoncentral.io/python-generators-and-yield-keyword/ | |
#Read small chunks from big buffer | |
def buffered_read(input): | |
#Read a big chunk of data | |
buffer = input | |
#Return small chunks on demand | |
for small_chunk in buffer: | |
print("Generator invoked ..") | |
yield small_chunk | |
def example2(): | |
result = buffered_read("12345") | |
for x in result: | |
print(x) | |
example2() | |
#output | |
Generator invoked .. | |
1 | |
Generator invoked .. | |
2 | |
Generator invoked .. | |
3 | |
Generator invoked .. | |
4 | |
Generator invoked .. | |
5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment