Skip to content

Instantly share code, notes, and snippets.

View shantanuo's full-sized avatar

Shantanu Oak shantanuo

  • oksoft
  • mumbai
View GitHub Profile
@shantanuo
shantanuo / mongo-ls.js
Created April 24, 2019 05:23 — forked from matteofigus/mongo-ls.js
A script to list all the collections and document count for a specific mongodb db
// Usage: mongo {Server without mongodb:// example 127.0.0.1:27017}/{DbName} [-u {Username}] [-p {Password}] < ./mongo-ls.js
var collections = db.getCollectionNames();
print('Collections inside the db:');
for(var i = 0; i < collections.length; i++){
var name = collections[i];
if(name.substr(0, 6) != 'system')
print(name + ' - ' + db[name].count() + ' records');
@shantanuo
shantanuo / tree.markdown
Created December 8, 2016 12:54 — forked from matthandlersux/tree.markdown
one-line tree datastructure in python

in python:

from pprint import pprint
from collections import defaultdict


# one-line definition of a tree:
def tree(): return defaultdict(tree)
@shantanuo
shantanuo / tree.md
Created December 8, 2016 12:48 — forked from hrldcpr/tree.md
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

@shantanuo
shantanuo / natural_language_time_periods.py
Created September 28, 2016 13:23 — forked from psychemedia/natural_language_time_periods.py
Simple python functions to give dates and date ranges in "natural time"; this week, next month, etc.
import datetime
from dateutil.relativedelta import relativedelta
def today(date=None,iso=False):
if date is None: date=datetime.date.today()
if iso: return date.isoformat()
else: return date
def yesterday(date=None,iso=False):
if date is None: date=today()
@shantanuo
shantanuo / mongo-dump-csv.sh
Created August 8, 2016 08:56 — forked from mderazon/mongo-dump-csv.sh
Export all of Mongodb collections as csv without the need to specify fields
OIFS=$IFS;
IFS=",";
# fill in your details here
dbname=DBNAME
user=USERNAME
pass=PASSWORD
host=HOSTNAME:PORT
# first get all collections in the database
@shantanuo
shantanuo / persist_cache_to_disk.py
Created June 17, 2016 13:24 — forked from tohyongcheng/persist_cache_to_disk.py
Persisting a cache in Python to disk using a decorator
def persist_cache_to_disk(filename):
def decorator(original_func):
try:
cache = pickle.load(open(filename, 'r'))
except (IOError, ValueError):
cache = {}
atexit.register(lambda: pickle.dump(cache, open(filename, "w")))
def new_func(*args):
@shantanuo
shantanuo / s3-curl-backups.md
Created April 25, 2016 13:47 — forked from jareware/s3-curl-backups.md
Simple, semi-anonymous backups with S3 and curl

⇐ back to the gist-blog at jrw.fi

Simple, semi-anonymous backups with S3 and curl

Backing stuff up is a bit of a hassle, to set up and to maintain. While full-blown backup suites such as duplicity or CrashPlan will do all kinds of clever things for you (and I'd recommend either for more complex setups), sometimes you just want to put that daily database dump somewhere off-site and be done with it. This is what I've done, with an Amazon S3 bucket and curl. Hold onto your hats, there's some Bucket Policy acrobatics ahead.

There's also a tl;dr at the very end if you just want the delicious copy-pasta.

Bucket setup

@shantanuo
shantanuo / download.py
Created January 31, 2016 07:38 — forked from peterbe/download.py
Download from bugzilla.mozilla.org, Upload to autocompeter.com. See http://www.peterbe.com/plog/a-quicksearch-for-bugzilla-using-autocompeter for context.
import datetime
import json
import os
import hashlib
import time
import requests
def bulk_upload(auth_key, documents):
@shantanuo
shantanuo / gist:66fc8c42c6896a103801
Created January 28, 2016 10:59 — forked from griggheo/gist:2698152
dynamodb batchwriteitem in boto
import os
import sys
import subprocess
import re
import optparse
import boto
dynamodb_conn = boto.connect_dynamodb(aws_access_key_id='MY_ACCESS_KEY_ID', aws_secret_access_key='MY_SECRET_ACCESS_KEY')
table_name = 'mytable'
dynamodb_table = dynamodb_conn.get_table(table_name)
Sl NO. District Assembly Constituency Winning Candidate Party Total Vote Runner-Up Candidate Party Total Vote
1 PASHCHIM CHAMPARAN 1-VALMIKI NAGAR Dhirendra Pratap Singh Alias Rinku Singh IND 66860 Irshad Hussain INC 33280
2 PASHCHIM CHAMPARAN 2-RAMNAGAR Bhagirathi Devi BJP 82166 Purnmasi Ram INC 64178
3 PASHCHIM CHAMPARAN 3-NARKATIAGANJ Vinay Varma INC 57212 Renu Devi BJP 41151
4 PASHCHIM CHAMPARAN 4-BAGAHA Raghaw Sharan Pandey BJP 74476 Bhishm Sahani JD(U) 66293
5 PASHCHIM CHAMPARAN 5-LAURIYA Vinay Bihari BJP 57351 Ran Kaushal Pratap Singh RJD 39778
6 PASHCHIM CHAMPARAN 6-NAUTAN Narayan Prasad BJP 66697 Baidyanath Prasad Mahto JD(U) 52362
7 PASHCHIM CHAMPARAN 7-CHANPATIA Prakash Rai BJP 61304 N. N. Sahi JD(U) 60840
8 PASHCHIM CHAMPARAN 8-BETTIAH Madan Mohan Tiwari INC 66786 Renu Devi BJP 64466