Skip to content

Instantly share code, notes, and snippets.

View sidchilling's full-sized avatar

Siddharth Saha sidchilling

View GitHub Profile
@sidchilling
sidchilling / go_routine.go
Created March 25, 2021 11:18
Go Routing question
package main
// Below is a function `sum()` that takes a number, runs a loop to add, and returns the result.
// In the main function, we call `sum()` once with a small number (e.g. 1000) and once
// with a large number (e.g. 1000000000) and prints the result. After that, the main()
// prints the string "All Done!"
// Your job is to convert the main() so that we call sum() concurrently, but wait for all the invocations
// to sum() to complete before printing "All Done".
@sidchilling
sidchilling / vfs_tests.py
Last active February 17, 2018 11:24
This is a python script which uses Splinter to automate tests for Viral Flash Deal App for ShopSocially. This was used for demo purpose on the technext talk on automating testing for single-page app
# This script tests the VFS
from splinter import Browser
def main(browser):
browser.visit(url = 'https://deals.shopsocially.com/flashsales/nikitaisawesome?cmp_id=516cfc502bba0163b6000002&offer_ref=nikitaisawesome_email')
# Press the unlock link
try:
try:
@sidchilling
sidchilling / tools.py
Created April 29, 2013 06:46
How to use the decorator package to write decorators
from decorator import decorator
def my_decorator_wrapper(func, first_name, last_name):
print 'You passed me the args: %s, %s' %(first_name, last_name)
return func(first_name, last_name)
my_decorator_wrapper = decorator(my_decorator_wrapper)
@my_decorator_wrapper
def decorated_function(first_name, last_name):
@sidchilling
sidchilling / arguments_to_decorators.py
Created April 29, 2013 06:45
Script to show how to use a decorator maker to pass arguments to a decorator
# To show how we can pass arguments to decorators. We will use the decorator maker pattern
# that we saw in the previous example
def decorator_maker_with_args(decorator_arg1, decorator_arg2):
print 'I make decorators. And I accept arguments: %s, %s' %(decorator_arg1,
decorator_arg2)
def my_decorator(func):
# This is the decorator. The ability to pass arguments here is because of closure
@sidchilling
sidchilling / decorator_maker.py
Created April 29, 2013 06:43
Decorator Maker - how to pass arguments to the decorators
# This script will show how a decorator maker works. This is required for understanding how
# we can pass arguments to the decorators
def decorator_maker():
print 'I make decorators. I get executed only once when you make or create a decorator'
def my_decorator(func):
print 'I am a decorator. I get executed only one when you decorate a function'
def wrapper():
@sidchilling
sidchilling / generic_decorator.py
Created April 29, 2013 06:42
Script to show how to make a generic decorator which will take all the arguments passed to the decorated function
# To make a generic decorator use *args and **kwargs
def a_decorator_passing_arbitrary_arguments(function_to_decorate):
# The wrapper accepts any arguments
def a_wrapper_accepting_arbitrary_arguments(*args, **kwargs):
print 'Do I have args?'
print args
print kwargs
function_to_decorate(*args, **kwargs)
return a_wrapper_accepting_arbitrary_arguments
@sidchilling
sidchilling / decorating_methods.py
Created April 29, 2013 06:41
Script to show how to decorate a method in Python
# Decorated methods is same as decorating functions. Just take 'self' into consideration
def method_friendly_decorator(method_to_decorate):
def wrapper(self, lie):
lie = lie + 3
return method_to_decorate(self, lie)
return wrapper
class Hermione(object):
@sidchilling
sidchilling / arguments_to_decorated_function.py
Created April 29, 2013 06:40
script to show how to pass arguments to a decorated function
# Passing arguments to the decorated function
# It's not magic, you just have to let the "wrapper" pass the argument
def a_decorator_passing_arguments(function_to_decorate):
def a_wrapper_accepting_arguments(arg1, arg2):
print 'I got args! Look: %s, %s' %(arg1, arg2)
function_to_decorate(arg1, arg2)
return a_wrapper_accepting_arguments
@sidchilling
sidchilling / cumulate_decorator.py
Created April 29, 2013 06:39
How to use more than one decorator to decorate a function
# More than one decorator on a function
# A decorator to make text bold
def make_bold(func):
def wrapper_1():
return '<b>%s</b>' %(func())
return wrapper_1
# A decorator to make text italic
def make_italic(func):
@sidchilling
sidchilling / manual_decorator.py
Created April 29, 2013 06:37
Script to show how to write a manual decorator
# Shows how to manually define and use a decorator
# A decorator is a function which expects another function as a parameter
def my_shiny_new_decorator(a_function_to_decorate):
# Inside, the decorator defines a function on the fly: "the wrapper".
# This function is going to be wrapped around the original function so it can execute
# code before and after the original function
def the_wrapper_around_the_original_function():