Skip to content

Instantly share code, notes, and snippets.

@keyurgolani
keyurgolani / print_stack_trace.py
Created February 12, 2018 20:05
Print Whole Stack Trace in Python without halting the execution
try:
# Do something
except Exception as e:
import traceback
import sys
exc_info = sys.exc_info()
traceback.print_exception(*exc_info)
del exc_info
# Handle Exception
@keyurgolani
keyurgolani / BinaryTreePrettyPrint.py
Created January 18, 2018 22:46
This code prints a binary tree in pretty fashion top down.
"""
Sample Output
[0]
[1] [2]
[3] [4] [5] [6]
[7] [8] [9] [ ] [ ] [ ] [ ] [ ]
"""
@keyurgolani
keyurgolani / Utility.kt
Last active June 4, 2017 23:27
Kotlin Utility Functions Added to Builtin Classes for convenience.
fun List<Int>.isEqualTo(other: List<Int>): Boolean {
if (this.size != other.size) {
return false
}
return (0..this.size - 1).all { this[it].equals(other[it]) }
}
fun List<Int>.isNotEqualTo(other: List<Int>): Boolean {
if (this.size != other.size) {
return true
@keyurgolani
keyurgolani / Lock.kt
Created June 1, 2017 20:57
Locking mechanism for multithreaded code in Kotlin.
data class Lock<T: Any>(private val obj: T) {
public fun acquire(func: (T) -> Unit) = synchronized (obj) {
func(obj)
}
}
@keyurgolani
keyurgolani / checkObjExists.js
Last active May 6, 2017 17:16
Check if the object passed exists - JavaScript - Is not null, not undefined and not empty
exists = function(obj) {
if (typeof obj !== 'undefined') {
if (obj !== null && obj !== undefined) {
if (typeof obj === 'string') {
return obj !== '';
} else if (typeof obj === 'number') {
return obj !== 0;
} else {
/* Node.js supports JSON.stringify(obj) syntax.
* For other versions of JS, this should be replaced with appropriate syntax.
@keyurgolani
keyurgolani / dao.js
Last active February 28, 2023 23:45
Node.js Data Access Object (DAO) Template with Self Implemented Connection Pooling
var mysql = require("mysql");
// Configure your logger here
var logger = require("../utils/logger");
// Add a properties file here with the configurations
// For properties file format visit https://github.com/steveukx/properties
var properties = require('properties-reader')('properties.properties');
function getConnection() {
var connection = mysql.createConnection({
host: properties.get('mysql.host'),
@keyurgolani
keyurgolani / snippets.cson
Created May 2, 2017 22:25
Atom Problem Solving Setup for Python
'.source.python':
'Snippet Name':
'prefix': 'setup'
'body': """
def answer():
pass
def main():
for case in range(int(raw_input())):
@keyurgolani
keyurgolani / UIColorWithHex
Created April 29, 2017 19:20
Extends UIColor in Swift to init a UIColor from given Hex Value or given RGB value with fixed 1.0 alpha value.
extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red value")
assert(green >= 0 && green <= 255, "Invalid green value")
assert(blue >= 0 && blue <= 255, "Invalid blue value")
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}
convenience init(rgb: Int) {
@keyurgolani
keyurgolani / console_confirmation.py
Created April 13, 2017 01:54
A simple python function that Prompts user on the standard console for confirmation. The user can choose one of multiple meanings either meaning Yes or No. Any other input will result in a bad input and the user will be prompted again. The user will be allowed to enter bad inputs exactly equal to the value of 'retries' parameter. Valid inputs fo…
def console_confirmation(question, retries=4, message='Bad answer! Please try again.'):
"""
Prompts user on the standard console for confirmation.
The user can choose one of multiple meanings either meaning Yes or No.
Any other input will result in a bad input and the user will be prompted again.
The user will be allowed to enter bad inputs exactly equal to the value of 'retries' parameter.
Valid inputs for Yes response are 'y', 'yes', 'sure', 'alright', 'right', 'all right', 'positive', 'certainly', 'for sure', 'indeed', 'agreed', 'absolutely', 'affirmative'
Valid inputs for No response are 'n', 'no', 'nope', 'na', 'nah', 'nada', 'negative', 'not really', 'not', 'no way', 'no ways'
"""
answer = raw_input(question + '\n')