Skip to content

Instantly share code, notes, and snippets.

View rahulkp220's full-sized avatar
🎯
Focusing

Rahul rahulkp220

🎯
Focusing
View GitHub Profile
@rahulkp220
rahulkp220 / api.rs
Last active December 19, 2023 20:42
Connect JSON Placeholder API with Rust
use reqwest;
#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct Todo {
user_id: i32,
id: i32,
title: String,
completed: bool,
}
@rahulkp220
rahulkp220 / yield_from.py
Last active July 1, 2016 20:16
Describes how yield from keyword works and behaves in Python3
""" while reading about yield keyword, I got curious about the difference between a yield and a yield from keywords.
Here is a brief differentiation that I have shown.
"""
#a normal yield inside a function that takes a list as an input
def a_func(my_list):
yield my_list
#using yield from keyword on a similar function
def b_func(my_list):
yield from my_list
def duplicate(function):
def wrapper(*args,**kwargs):
return 2*function(*args,**kwargs)
return wrapper
def formatting(lowerscase = False):
def formatting_real(function):
def wrapper(*args, **kwargs):
if lowerscase:
def multiply(by = None):
def multiply_real_decorator(function):
def wrapper(*args,**kwargs):
return by * function(*args,**kwargs)
return wrapper
return multiply_real_decorator
@multiply(by = 3)
def adder(a,b):
return a + b
def double(function):
def wrapper(*args,**kwargs):
return 2 * function(*args,**kwargs)
return wrapper
@double
def adder(x,y):
return x + y
print adder(1,3)
class mimic_range:
def __init__(self,maximum):
self.maximum = maximum
def __iter__(self):
self.current = 0
return self
def next(self):
num = self.current
def BinarySearch(my_item,my_list):
found = False
bottom = 0
top = len(my_list)-1
while bottom<=top and not found:
middle = (bottom+top)//2
if my_list[middle] == my_item:
found = True
elif my_list[middle] < my_item:
bottom = middle + 1
#Its in python 3
def LinearSearch(my_item,my_list):
found = False
position = 0
while position < len(my_list) and not found:
if my_list[position] == my_item:
found = True
position += 1
return found
def example_function(arg1,arg2,arg3,*args):
print “arg1 is {0}”.format(arg1)
print “arg2 is {0}”.format(arg2)
print “arg3 is {0}”.format(arg3)
print “*arg is {0}”.format(args)
>>> example_function(1,2,3,4,5,6,7,8,9,10)
arg1 is 1
arg2 is 2
arg3 is 3
def sample(a,*args,**kwargs):
print “a is {}”.format(a)
print “*args is a tuple {}”.format(args)
print “**kwargs is a dictionary {}”.format(kwargs)
>>> sample(1,2,3,4,name=”rahul”,age=26)
a is 1
*args is a tuple (2, 3, 4)
**kwargs is a dictionary {‘age’: 26, ‘name’: ‘rahul’}