Skip to content

Instantly share code, notes, and snippets.

View kshirsagarsiddharth's full-sized avatar
🎯
Focusing

kshirsagarsiddharth

🎯
Focusing
View GitHub Profile
@kshirsagarsiddharth
kshirsagarsiddharth / readinto.py
Created August 2, 2020 09:35
reading binary data directly into nram through a file object
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")
@kshirsagarsiddharth
kshirsagarsiddharth / bytes.py
Created August 1, 2020 12:22
byte concatenation vs io.BytesIO
# 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):
@kshirsagarsiddharth
kshirsagarsiddharth / io.py
Created August 1, 2020 12:15
io.StringIO and io.BytesIO
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
@kshirsagarsiddharth
kshirsagarsiddharth / flattening.py
Created August 1, 2020 06:04
flattening the sequence
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
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:
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():
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:
@kshirsagarsiddharth
kshirsagarsiddharth / plt.py
Created July 31, 2020 11:51
pig latin translator
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():
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
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