Skip to content

Instantly share code, notes, and snippets.

View bhavaniravi's full-sized avatar
🎯
Focusing

Bhavani Ravi bhavaniravi

🎯
Focusing
View GitHub Profile
@bhavaniravi
bhavaniravi / configure_form_fields.py
Created December 7, 2017 03:46
Django configure form field with bootstrap form-elements
def configure_fields(fields):
"""
Configures form field with bootstrap style
Input forms fields
Output formatted form fields
"""
for field in fields:
if isinstance(fields[field].widget, Textarea):
fields[field].widget.attrs['rows'] = '3'
fields[field].widget.attrs['class'] = 'form-control'
@bhavaniravi
bhavaniravi / test_data_generator.py
Last active December 7, 2017 03:51
Creates formset data to replicate data posted by Django forms. This helps to generate data while writing test cases
def instantiate_formset_data(formset_class, data, instances=[], initial=None, action="add"):
"""
Input:
Formset_class
data - Python Dict type form data
Instances - [] if its a CreateForm, or list of instances in case of EditForm
Initial - Initial data if any
Action = define edit/Add action
Output: data --> Formset style Data
@login_required
def addtoprofile(request, count):
count = int(count)
formsets = (Form1, Form2, Form3)
try:
Form = Form[count]
except IndexError:
return redirect("/")
next_url = "Next"
if count == len(formsets):
@bhavaniravi
bhavaniravi / pandas-pivot-table-subtotal.py
Created July 11, 2018 11:08
Script to calculate pandas subtotal
table = pd.pivot_table(df, values='Quantity', index =['Country','Region'], columns=["Year","Requester"], aggfunc=[np.sum])
result_df = table
for i in range(2):
df2 = table.groupby(level=i).sum()
if i==0:
tot_sum = df2.sum(axis=i)
df2 = pd.concat([df2,pd.DataFrame([tot_sum]).rename({0:"Grand"})]).sort_index(level=[0])
df2.index = pd.MultiIndex.from_arrays([df2.index.values + '_Total',
len(df2.index) * [''],
len(df2.index) * ['']])
@bhavaniravi
bhavaniravi / pandas_playbook.ipynb
Created December 28, 2018 06:42
Pandas - Where to started?
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bhavaniravi
bhavaniravi / App.js
Created April 20, 2019 06:44
Blog gist Integrate with frontend
import React, {Component} from 'react';
import TodoList from '../ui/TodoList';
import StateProvider from './StateProvider';
class App extends Component {
render() {
return (
<StateProvider>
<TodoList/>
</StateProvider>
@bhavaniravi
bhavaniravi / react_button_cheetsheet.js
Created May 19, 2019 09:07
CheatSheet to create a Button Component in React
import React from 'react'
import styled, { css } from 'styled-components'
const Button = styled.button`
position: absolute;
height: 10%;
width: 10%;
top: 50%;
left:50%;
@bhavaniravi
bhavaniravi / youtube_timestamp.js
Created March 25, 2020 22:16
A script to calculate timestamp of youtube playlist
ele = document.getElementsByClassName("style-scope ytd-thumbnail-overlay-time-status-renderer")
for(i=0, i<ele.length, i+=){
if (ele[i].tagName == "span"){
label = ele[i].getAttribute("aria-label")
times = parseFloat(ele[2].textContent.replace(":", ".").trim())
}
}
@bhavaniravi
bhavaniravi / video_time.py
Created March 30, 2020 04:33
Find length of videos in a folder
from mutagen.mp4 import MP4
duration = 0
for file in os.listdir():
mp4 = MP4(file)
duration += mp4.info.length
print (duration)
@bhavaniravi
bhavaniravi / unfollow_in_linkedin.js
Created June 28, 2020 10:46
A Script to unfollow Linkedin followers
// Linkedin Unfortunately follows every collection, once in a while I use this script to clean it up
a = document.getElementsByClassName("follows-recommendation-card__follow-btn")
b = Array.from(a)
b.forEach((x) => {
x.click()
})