from pprint import pprint
from collections import defaultdict
# one-line definition of a tree:
def tree(): return defaultdict(tree)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); |
Using Python's built-in defaultdict we can easily define a tree data structure:
def tree(): return defaultdict(tree)
That's it!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OIFS=$IFS; | |
IFS=","; | |
# fill in your details here | |
dbname=DBNAME | |
user=USERNAME | |
pass=PASSWORD | |
host=HOSTNAME:PORT | |
# first get all collections in the database |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
⇐ back to the gist-blog at jrw.fi
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import json | |
import os | |
import hashlib | |
import time | |
import requests | |
def bulk_upload(auth_key, documents): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |