Skip to content

Instantly share code, notes, and snippets.

View nitinbhojwani's full-sized avatar

Nitin Bhojwani nitinbhojwani

View GitHub Profile
@nitinbhojwani
nitinbhojwani / fix_git_committer_name_and_email.sh
Created February 12, 2024 16:32
Fix user name and email of multiple git commits
# Fix user name and email of multiple git commits
git config --local user.name "Nitin Bhojwani"
git config --local user.email "nitinbhojwani@outlook.com"
git rebase -r <last-right-git-commit-hash> --exec 'git commit --amend --no-edit --reset-author'
@nitinbhojwani
nitinbhojwani / create_temp_file.py
Created November 18, 2021 13:26
Create temporary file in Python
import tempfile
def _create_temp_file_with_content(file_content):
fp = tempfile.NamedTemporaryFile(delete=False)
fp.write(file_content)
return fp.name
@nitinbhojwani
nitinbhojwani / resolve-conflicts-with-poetry-lock.sh
Created July 2, 2021 07:19
Resolve git merge conflicts with poetry.lock
# Pick the change from the branch just merged
git checkout --theirs poetry.lock
# Update poetry.lock from pyproject.toml without any update
poetry lock --no-update
# Yey! the conflict is resolved and poetry.lock is updated with correct hash.
@nitinbhojwani
nitinbhojwani / LinkedList.kt
Created February 7, 2021 05:41
Basic LinkedList implementation in Kotlin
class LinkedList<T>() {
private var head: Node<T>? = null
private var lastNodePointer: Node<T>? = null
public fun add(value: T) {
val node = Node<T>(value)
if (head == null) {
head = node
lastNodePointer = node
} else {
@nitinbhojwani
nitinbhojwani / GenericProcessor.java
Created August 1, 2020 13:11
Generics(Function and Class) in Java
/**
* Generic Function example
* GenericProcessor has a generic function that takes two generic arguments of type T
* One of them is the object of type T
* Another one is the object that processes objects of type T
*/
public class GenericProcessor {
private <T> void processObjectGivenProcessor(
T object,
Processor<T> processor) {
@nitinbhojwani
nitinbhojwani / chart.tf
Created July 14, 2020 07:42
Add detector to the chart in terraform sfx
# inside chart add other label and publish it to link the detector
B = alerts(detector_id='${signalfx_detector.detector_name_here.id}').publish(label='B')
@nitinbhojwani
nitinbhojwani / TestScheduler.kt
Created July 5, 2020 08:02
Test Scheduler using Kotlin and SpringFramework - tested for Target JVM 1.8
package com.example.org.schedules
import com.example.org.services.ConstructorInjection
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
/**
* TestScheduler to schedule a method to run with fixedDelay
@nitinbhojwani
nitinbhojwani / processpool_executor.py
Created April 8, 2020 12:31
Using processpool executor in Python3
import concurrent.futures
def test_function(n):
return n ** 2
# initiate a process pool executor
# by default, number of workers = number of CPU cores
executor = concurrent.futures.ProcessPoolExecutor(max_workers=10)
# 1. submit jobs to the executor and track in a list
@nitinbhojwani
nitinbhojwani / threadpool_execution.py
Created April 8, 2020 12:24
Using threadpool executor in Python3
import concurrent.futures
def test_function(n):
return n ** 2
# initiate a thread pool executor
executor = concurrent.futures.ThreadPoolExecutor(max_workers=10)
# 1. submit jobs to the executor and track in a list
jobs = []
@nitinbhojwani
nitinbhojwani / add_multiple_embedded_documents.py
Last active April 20, 2019 16:45
Mongoengine Add or Remove EmbeddedDocument in ListField of EmbeddedDocuments
# roles as Python list of dictionaries
roles = [
{'org': 'ABC', 'permissions':['RO', 'RW']},
{'org': 'DEF', 'permissions':['RO', 'RW']},
{'org': 'GHI', 'permissions':['RO', 'RW']},
{'org': 'JKL', 'permissions':['RO', 'RW']},
]
# get user object
user = User.objects(id=ObjectId('user_id')).first()