Skip to content

Instantly share code, notes, and snippets.

View rahulrajaram's full-sized avatar

rahulrajaram rahulrajaram

View GitHub Profile
@rahulrajaram
rahulrajaram / .py
Created January 21, 2022 19:14
Printing distribution of floats over 1 second intervals
"""
This scripts loads a file that contains stringified floats expected
to be seconds since epoch listed in a comma-separated fashion as
follows:
"1638201720.793","1638201720.778","1638201720.770","1638201719.866","1638201710.152","1638201710.140","1638201709.727","1638201707.715","1638201704.069","1638201702.769","1638201702.757","1638201699.529","1638201699.363","1638201699.362","1638201689.308","1638201688.406","1638201686.458","1638201686.097","1638201685.893","1638201685.620","1638201685.350","1638201685.035","1638201684.650","1638201682.229","1638201679.695","1638201678.098","1638201677.825","1638201676.972","1638201676.640","1638201675.442","1638201674.020","1638201672.890","1638201672.862","1638201669.458","1638201669.364","1638201665.908","1638201665.606","1638201665.271","1638201665.138","1638201664.315","1638201664.298","1638201663.204","1638201662.234","1638201661.479","1638201661.368","1638201660.295","1638201660.256","1638201660.108","1638201660.097","1638201660.010","1638201659.797","163820165
@rahulrajaram
rahulrajaram / .zsh
Created October 19, 2020 00:51
Git Aliases
alias gbr="git branch"
alias gap="git add -p"
alias gca="git commit --amend"
alias gco="git checkout"
alias gcp="git cherry-pick"
alias gcom="git checkout master"
alias gdc="git diff --cached"
alias gmv="git mv"
alias gpo="git pull origin"
alias gpl="git pull"
@rahulrajaram
rahulrajaram / .txt
Created April 24, 2020 17:20
A note on my stance on squashing
There are situations where squashing commits is not favourable and is an anti-pattern.
Squashing is necessary when a series of commits represents an atomic unit of work, where an atomic unit of work is any work which if broken into two or more independent commits would render either or both of them functionally incomplete. as an extreme example, changing a single global variable name across two commits would likely break the first commit and so the two commits should be squashed. They should also be squashed because the two commits are thematically one (although thematic unity can be relative), but I view this as a less important reason. At any point, one should be able to go back to a given commit on master and ensure all tests pass.
Squashing should be avoided in situations where each commit in a given series is atomic, even if the commits deliver a feature in combination (unless, as alluded to above, some commits break the codebase in isolation). Incremental commits often represent the logical sequence o
@rahulrajaram
rahulrajaram / .py
Created October 6, 2019 04:43
`tail` implementation in Python
"""
Script implements:
tail FILE_NAME NUMBER_OF_LINES_TO_TAIL
"""
import sys
def seek_eof(file):
file.seek(0, 2)
@rahulrajaram
rahulrajaram / .js
Created August 31, 2019 08:18
List all S3 Objects using JavaScript SDK
/*
* File invokes s3#listObjects recursively to retrieve all objects
*/
const AWS = require('aws-sdk')
const s3 = new AWS.S3();
let args = {
'Bucket': ADD_BUCKET_NAME_HERE,
'Prefix': ADD_PATH_PREFIX_HERE_OR_REMOVE_THIS_ATTRIBUTE
};
@rahulrajaram
rahulrajaram / .c
Created May 8, 2019 07:01
Generic in C using macros
#include <string.h>
#include <stdio.h>
/*
* Macro function that initializes a struct object
* and returns it.
*
* For this to work, a `{type}Init` function must
* be defined.
*/
@rahulrajaram
rahulrajaram / .py
Created December 10, 2018 21:55
Save stack trace to file
import traceback
def print_stack_trace():
try:
raise Exception
except Exception:
with open('~/.my_important_stack_trace', 'w') as file:
file.write('{}'.format(repr(traceback.format_stack())))
@rahulrajaram
rahulrajaram / .rb
Created April 19, 2018 19:47
An example with procs, blocks and lambdas in Ruby
lambda_1 = -> { puts 'From lambda_1: Hello, World!' }
lambda_2 = -> { puts 'From lambda_2: Hello, World!' }
proc_1 = Proc.new { puts 'From proc_1: Hello, World!' }
proc_2 = Proc.new { puts 'From proc_2: Hello, World!' }
def proc_or_lambda_called(
proc_or_lamdba_1,
proc_or_lamdba_2
)
@rahulrajaram
rahulrajaram / .md
Last active October 23, 2017 02:30
Password generator using Python
import random
import string

# The pool of characters to pick the characters for your password from
password_char_set = string.ascii_letters + string.digits + string.punctuation

def random_character():
    return password_char_set[random.randint(0, len(password_char_set))]
@rahulrajaram
rahulrajaram / .md
Last active April 2, 2023 15:47
Python: Write to a file from multiple threads

I recently came across the need to spawn multiple threads, each of which needs to write to the same file. Since the file will experience contention from multiple resources, we need to guarantee thread-safety.

NOTE: The following examples work with Python 3.x. To execute the following programs using Python 2.7, please replace threading.get_ident() with thread.get_ident(). As a result, you would need to import thread and not threading.

  1. (The following example will take a very long time). It will create 200 threads, each of which will wait until a global lock is available for acquisition.
# threading_lock.py
import threading