Skip to content

Instantly share code, notes, and snippets.

View ashwinrs's full-sized avatar
💭
🍦

Ashwin S ashwinrs

💭
🍦
View GitHub Profile
@ashwinrs
ashwinrs / migrate_collection.py
Created June 13, 2017 21:19
Copy a collection between two mongodbs
#!/usr/bin/python3
from pymongo import MongoClient
from datetime import datetime, timedelta
from pprint import pprint
mongo_parameters = {
'source_mongo': '',
'db' : '',
'dest_mongo' : ''
}
@ashwinrs
ashwinrs / execute_shell_command.py
Last active June 15, 2017 00:37
Execute shell command from python
import subprocess
def execute_binary(args):
popen = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout,stderr = popen.communicate()
if popen.returncode != 0:
print("Process exited non zero: {0}".format(popen.returncode))
return False
return True
@ashwinrs
ashwinrs / s3_get.py
Last active June 20, 2017 19:08
Download s3 file using python3
#!/usr/bin/python3
import boto3
def get_s3_object(key, keyid, bucket, s3_file_location, local_file):
client = boto3.client('s3',aws_access_key_id=keyid,aws_secret_access_key=key)
client.download_file(bucket, s3_file_location, local_file)
return
@ashwinrs
ashwinrs / string_tokenizer.cpp
Last active July 17, 2017 15:48
String tokenizer in C++
// Based on - https://stackoverflow.com/a/53878/437894
#include <vector>
#include <string>
using namespace std;
vector<string> stringTokenizer(string input, char delimiter){
vector<string> result;
if(!input.length())
return result;
@ashwinrs
ashwinrs / EnumUtils.java
Created December 13, 2017 18:08
Gets enum from a string
/**
* A common method for all enums since they can't have another base class
* @param <T> Enum type
* @param c enum type. All enums must be all caps.
* @param string case insensitive
* @return corresponding enum, or null
*/
public static <T extends Enum<T>> T getEnumFromString(Class<T> c, String string) {
if( c != null && string != null ) {
try {
@ashwinrs
ashwinrs / PrettyPrintJSON.java
Created February 1, 2018 22:03
Pretty Print a JSON in Java
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(GSON.toJson(<Object>));
String prettyJsonString = gson.toJson(je);
System.out.println(prettyJsonString);
@ashwinrs
ashwinrs / IterateOverJSONUsingGson.java
Last active January 9, 2023 23:54
Iterate over elements in a JSON using GSON in Java
final JsonObject jsonObject = GSON.toJsonTree(<Object>).getAsJsonObject();
for(Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
System.out.println("Key = " + entry.getKey() + " Value = " + entry.getValue() );
}
@ashwinrs
ashwinrs / build.gradle
Last active February 2, 2018 02:17
Finding dependency tree in gradle build system
// https://stackoverflow.com/a/41784667/437894
apply plugin: 'project-report'
@ashwinrs
ashwinrs / build.gradle
Created March 28, 2018 21:32
How to include local jars in build.gradle
dependency {
compile files ("../../x/y/z/libs/abs.jar")
}
@ashwinrs
ashwinrs / AddAProperty.js
Created May 9, 2018 23:54
Add a new property to an object when creating a new object
/*
arrOfObj = [{"name":"Ash"},{"name":"tim"},{"name":"david"}]
*/
var result = arrOfObj.map(function(el) {
var o = Object.assign({}, el);
o.isActive = true;
return o;
})
console.log(result)