This file contains hidden or 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
| import os | |
| def read_into_buffer(filename): | |
| buffer = bytearray(os.path.getsize(filename)) | |
| with open(filename,'rb') as f: | |
| f.readinto(buffer) | |
| return buffer | |
| with open("example.bin",'wb') as f: | |
| f.write(b"This is the text written into a example binary file") |
This file contains hidden or 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
| # then why not simply use the above mentioned. | |
| # optimization and performance | |
| import io | |
| import time | |
| start = time.time() | |
| buffer = b"" | |
| for i in range(0,90000): |
This file contains hidden or 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
| import io | |
| s = io.StringIO() | |
| print(s.write("Hello World/n")) | |
| # ------->Output: 13 | |
| # adding to the memory buffer using print statement | |
| print("adding using the print",file = s) | |
| # get all of the data written in the file |
This file contains hidden or 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
| from collections.abc import Iterable | |
| def flatten(items, ignore_types = (str,bytes)): | |
| for x in items: | |
| if isinstance(x,Iterable) and not isinstance(x,ignore_types): | |
| yield from flatten(x) | |
| else: | |
| yield x | |
| items = [1, 2, [3, 4, [5, 6], 7], 8] | |
| # Produces 1 2 3 4 5 6 7 8 |
This file contains hidden or 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
| import random | |
| def bad_service_chatbot(): | |
| answers = ["We don't do that", | |
| "We will get back to you right away", | |
| "Your call is very important to us", | |
| "Sorry, my manager is unavailable"] | |
| yield "Can I help you?" | |
| s = "" | |
| while True: | |
| if s is None: |
This file contains hidden or 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
| def cities(): | |
| for city in ['Delhi','Mumbai','Pune','Hyderabad']: | |
| yield city | |
| def squares(): | |
| for val in range(10): | |
| yield val ** 2 | |
| def all_in_one(): | |
| for city in cities(): |
This file contains hidden or 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
| import random | |
| def bad_service_chatbot(): | |
| answers = ["We don't do that", | |
| "We will get back to you right away", | |
| "Your call is very important to us", | |
| "Sorry, my manager is unavailable"] | |
| yield "Can I help you?" | |
| s = "" | |
| while True: | |
| if s is None: |
This file contains hidden or 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
| def pig_latin_sentence(sent): | |
| output = [] | |
| for word in sent.split(): | |
| if word[0] in "aeiou": | |
| output.append(word + "ay") | |
| else: | |
| output.append(word[1:] + word[0] + "ay") | |
| return " ".join(output) | |
| def pl_translate(): |
This file contains hidden or 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
| def myfunc(): | |
| x = "" | |
| while True: | |
| print("Yielding {} and waiting".format(x)) | |
| x = yield x | |
| if x is None: | |
| break | |
| print("Got {}. Doubling".format(x)) | |
| x = x * 2 |
This file contains hidden or 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
| def myfunc(): | |
| x = "" | |
| while True: | |
| print("Yielding {} and waiting".format(x)) | |
| x = yield x | |
| if x is None: | |
| break | |
| print("Got {}. Doubling".format(x)) | |
| x = x * 2 |