Skip to content

Instantly share code, notes, and snippets.

View ashwin's full-sized avatar

Ashwin Nanjappa ashwin

View GitHub Profile
@ashwin
ashwin / date_time.cpp
Created January 13, 2015 04:13
Get and print current date and time in C++
// Example output from this code:
// Tue Jan 13 12:09:53 2015 (from ctime)
// 20150113_12_09_53 (from strftime)
// 20150113_12_09_53 (from put_time)
#include <ctime>
#include <chrono>
#include <iomanip>
// Get current time
@ashwin
ashwin / disable_caps_lock.reg
Created January 12, 2015 09:52
Registry file to disable caps lock in Windows
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,00,00,3a,00,00,00,00,00
@ashwin
ashwin / matherror.cpp
Created January 5, 2015 02:26
How to check for math error in C++
// Checking for math error in C++
#include <cerrno>
#include <cmath>
#include <cstring>
#include <iostream>
std::cout << log(0) << std::endl;
if (errno)
std::cout << std::strerror(errno) << std::endl;
@ashwin
ashwin / multithread.py
Created December 31, 2014 01:53
Execute function in parallel across multiple input data items
# Class to execute a function in parallel across multiple data
# Adapted from code in Sec 9.5 of book Python Cookbook (2 Ed)
import threading
import time
import Queue
class MultiThread(object):
def __init__(self, function, argsVector, commonArgs, maxThreads=5, queue_results=False):
@ashwin
ashwin / struct_pack.py
Created December 29, 2014 09:47
Converting to bytes and writing to binary file in Python
# Sample code to write data of certain format to binary file
# For details of parameters passed to pack method see:
# https://docs.python.org/2/library/struct.html
import struct
# Assume these are values of fields of a class/struct in C/C++
a = 1 # unsigned int
b = 2 # signed char
c = -1 # signed int
@ashwin
ashwin / const.py
Created December 23, 2014 08:19
Alex Martelli's recipe to define constant in Python
# Helps define a constant in Python
# From Section 6.3: Defining Constants
# Python Cookbook (2 Ed) by Alex Martelli et al.
#
# Usage:
# import const
# const.magic = 23 # First binding is fine
# const.magic = 88 # Second binding raises const.ConstError
class _const:
@ashwin
ashwin / emplace_back.cpp
Created October 9, 2014 15:26
Sample code to demonstrate emplace_back operation of vector
// Sample code to demonstrate emplace_back operation of vector
#include <iostream>
#include <vector>
struct Creature
{
Creature() : is_mammal_(false), age_(0), population_(0)
{
std::cout << "Default constructor called\n";
@ashwin
ashwin / how_to_use_shared_ptr.cpp
Last active August 29, 2015 14:07
Sample C++ code to illustrate usage of std::shared_ptr
// Sample C++ code to illustrate usage of std::shared_ptr
// Compile with --std=c++11 option
#include <iostream>
#include <memory>
class Foo
{
public:
@ashwin
ashwin / CompilingQtProgram.cmake
Last active August 29, 2015 14:07
Sample CMakeLists.txt to compile a program that uses Qt
# Check if Qt4 is present
find_package(Qt4 REQUIRED)
# Add Qt headers for compiling
include(
${QT_USE_FILE}
)
# Add Qt definitions for compilation
add_definitions(
@ashwin
ashwin / ranger-cd.fish
Created September 28, 2014 13:50
Change directory in fish after closing Ranger
# Add this to your ~/.config/fish/config.fish
function ranger-cd
set tmpfile "/tmp/pwd-from-ranger"
ranger --choosedir=$tmpfile $argv
set rangerpwd (cat $tmpfile)
if test "$PWD" != $rangerpwd
cd $rangerpwd
end
end