Skip to content

Instantly share code, notes, and snippets.

rahulrajaram rahulrajaram

Block or report user

Report or block rahulrajaram

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
rahulrajaram / .py
Created Oct 6, 2019
`tail` implementation in Python
View .py
Script implements:
import sys
def seek_eof(file):, 2)
rahulrajaram / .js
Created Aug 31, 2019
List all S3 Objects using JavaScript SDK
View .js
* File invokes s3#listObjects recursively to retrieve all objects
const AWS = require('aws-sdk')
const s3 = new AWS.S3();
let args = {
rahulrajaram / .c
Created May 8, 2019
Generic in C using macros
View .c
#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 / .py
Created Dec 10, 2018
Save stack trace to file
View .py
import traceback
def print_stack_trace():
raise Exception
except Exception:
with open('~/.my_important_stack_trace', 'w') as file:
rahulrajaram / .rb
Created Apr 19, 2018
An example with procs, blocks and lambdas in Ruby
View .rb
lambda_1 = -> { puts 'From lambda_1: Hello, World!' }
lambda_2 = -> { puts 'From lambda_2: Hello, World!' }
proc_1 = { puts 'From proc_1: Hello, World!' }
proc_2 = { puts 'From proc_2: Hello, World!' }
def proc_or_lambda_called(
rahulrajaram / .md
Last active Oct 23, 2017
Password generator using Python
View .md
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 / .md
Last active Nov 14, 2019
Python: Write to a file from multiple threads
View .md

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.
import threading
rahulrajaram / .py
Last active Oct 24, 2017
Python print colored STDOUT
View .py
'black': '30',
'red': '31',
'green': '32',
'yellow': '33',
'blue': '34',
'magenta': '35',
'cyan': '36',
'white': '37',
rahulrajaram / .md
Last active Sep 30, 2018
Ruby: Where is the method defined?
View .md

Ruby: Different ways to define methods in

The Ruby Object Model

One of the central aspects of  the Ruby programming language is the Ruby object model, as per which everything (but for constructs such as methods, and keywords) in Ruby -- ​​classes, instances, lambdas, procs, strings, numbers, decimals, hashmaps -- is an object. This renders a kind of uniformity to Ruby that few other languages offer.

At the very same time, the object model can also become confusing. In particular, the following two questions can sometimes become difficult to answer, but being able to readily answer which can set you apart as a Ruby programmer.

  1. What is self in a given context?
  2. If I define a method here, where will it go? That is, which object is it going to be defined on?
rahulrajaram / .cpp
Last active Aug 13, 2017
Tail recursive function to find arithmetic sum in C++/C
View .cpp
TL;DR: Compile the program as follows:
g++ add.cpp -O2 && ./a.out
Suppose you want to compute the sum of the series:
You can’t perform that action at this time.