Skip to content

Instantly share code, notes, and snippets.

View yashbhutoria's full-sized avatar
👨‍💻
Making things happen

Yash Bhutoria yashbhutoria

👨‍💻
Making things happen
View GitHub Profile
@yashbhutoria
yashbhutoria / error_script.py
Last active April 29, 2021 21:10
Scirpt to reproduce error with pyrfc while reading some SAP tables
import pyrfc
class SapWrapper:
def __init__(self, connection: pyrfc.Connection):
self._connection = connection
def _read_table(self, table_name: str):
results = self._connection.call(
"RFC_READ_TABLE",
QUERY_TABLE=table_name,
@yashbhutoria
yashbhutoria / cache_decorator.py
Created June 30, 2020 15:20
A simple decorator to cache function calls with same arguments. Useful to optimize recursive functions with repeating calls.
# The decorator to cache function calls
def cached(func):
cache = dict()
def decorator(*args):
if tuple(args) not in cache:
cache[tuple(args)] = func(*args)
return cache[tuple(args)]
return decorator
# Usage example
@yashbhutoria
yashbhutoria / fizz_buzz.py
Created June 15, 2020 16:20
Fizz Buzz solution using an approach similar to to Sieve of Eratosthenes
def mark_multiples(ls, num, marker):
for i in range(num, len(ls), num):
ls[i] = marker if isinstance(ls[i], int) else (ls[i] + marker)
def main():
size = 100
nums = list(range(size+1))
mark_multiples(nums, 3, "Fizz")
mark_multiples(nums, 5, "Buzz")
[print(num) for num in nums[1:]]
@yashbhutoria
yashbhutoria / helper.py
Last active February 7, 2020 18:39
Metaclass to ease creation of DTOs and hook compatibility with json.loads for easy deserialization
class Dto(type):
def __new__(self,name,bases,at_dict,attributes):
at_dict["_properties"] = attributes
def init(self,**kwargs):
for _property in attributes:
received_value = kwargs.get(_property.name)
if received_value is not None:
if isinstance(received_value,_property.type):
@yashbhutoria
yashbhutoria / extract_7z_to_bytes.py
Last active November 25, 2019 06:13
Extract 7z to bytes in memory using libarchive. In your case, you might be pulling it from a cloud storage and don't wan't to write in your secondary memory.
import libarchive.public #Not Secure?
# Reading the archived 7z file as bytes
with open('csvarchive.7z','rb') as f:
archive_bytes = f.read()
# The function that you might want to use
def archive_bytes_to_file_bytes(archive_bytes):
global entry_var
with libarchive.public.memory_reader(archive_bytes) as reader:
@yashbhutoria
yashbhutoria / CurriculumVitae.png
Last active July 17, 2020 23:55
Curriculum Vitae
CurriculumVitae.png