Skip to content

Instantly share code, notes, and snippets.

@kapilgarg
kapilgarg / profile.ps1
Created May 14, 2024 18:37
Powershell utility functions. Thses can be added to powershell profile script and will be available in ps console
# Returns the size of file or folder in MB
function size($path){
$obj = Get-ChildItem $path -R | Measure-Object -Property Length -Sum
$item_size = $obj.Sum/(1024*1024)
Write-Host "$($item_size) MB"
}
#prints all the drive
function df(){
Get-PSDrive -PSProvider FileSystem
@kapilgarg
kapilgarg / translate-web-page.js
Last active March 10, 2024 05:42
Bookmarklet for translating a web page using google translate
// https://stackoverflow.com/questions/16446273/js-modify-bookmarklet-for-google-translate
// opens translated page in a new window
javascript: var t = ((window.getSelection && window.getSelection()) || (document.getSelection && document.getSelection()) || (document.selection && document.selection.createRange && document.selection.createRange().text));
var e = (document.charset || document.characterSet);
if (t != '') {
var url1 = 'http://translate.google.com/?text=' + t + '&hl=en&langpair=auto|sk&tbb=1&ie=' + e;
window.open(url1, '_blank', "GoogleTranslate", "height=200,width=200")
} else {
var url2 = 'http://translate.google.com/translate?u=' + encodeURIComponent(location.href) + '&hl=en&langpair=auto|sk&tbb=1&ie=' + e;
window.open(url2, '_blank', "GoogleTranslate", "height=200,width=200")
@kapilgarg
kapilgarg / progress_bar.py
Created January 16, 2024 10:12
sample code for a progress bar which is updating the value inline (increase / decrease)
import random
import time
while True:
n = random.randrange(1,10)
print("\r"+"|"*n, end="\x1b[1K")
time.sleep(.1)
# -*- coding: utf-8 -*-
"""
@author: KGarg
"""
import pandas as pd
def get_stock_price():
import requests
data_dict={}
symbols = ['MSFT','AAPL','NFLX']
@kapilgarg
kapilgarg / dag1.py
Last active January 16, 2024 09:36
from airflow.decorators import dag,task
import pendulum
import airflow1
@dag(dag_id='dag_tutorial_1', schedule=None, start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),catchup=False,tags=["example"],)
def airflow_dag_tutorial():
@task
def get_stock_price():
return airflow1.get_stock_price()
import json
from airflow.decorators import dag
import pendulum
from airflow.operators.dummy import DummyOperator
@dag(dag_id='dag_tutorial_1', schedule=None, start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),catchup=False,tags=["example"],)
def airflow_dag_tutorial():
"""
This is a simple data pipeline example which demonstrates the use of
the TaskFlow API using three simple tasks for Extract, Transform, and Load.
@kapilgarg
kapilgarg / pandas_example_3.py
Last active December 27, 2023 10:28
pandas Grouper
df_max_close_annual = df_msft.groupby(pd.Grouper(key='Date', freq='y')).agg({'Close':'max'})
df_max_close_annual.reset_index()
# OR set the index
df_msft.set_index('Date', inplace=True)
df_max_close_annual = df_msft.groupby(pd.Grouper(freq='y')).agg({'Close':'max'})
df_max_close_annual.reset_index()
@kapilgarg
kapilgarg / pandas_example_2.py
Created December 25, 2023 18:40
Aggregate dataframe by year
# get the max closing price for each year
df_max_close_annual = df_msft.groupby(df_msft.Date.dt.year).agg({'Close':'max'})
df_max_close_annual.reset_index()
import requests
import io
import pandas as pd
url = "https://query1.finance.yahoo.com/v7/finance/download/MSFT?period1=1545696000&period2=1703462400&interval=1d&events=history&includeAdjustedClose=true"
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',