Skip to content

Instantly share code, notes, and snippets.

View tsileo's full-sized avatar

Thomas Sileo tsileo

View GitHub Profile
@tsileo
tsileo / interval.py
Created March 31, 2013 13:29
Convert seconds to human readable interval back and forth.
"""Convert seconds to human readable interval back and forth."""
from collections import OrderedDict
import re
interval_dict = OrderedDict([("Y", 365*86400), # 1 year
("M", 30*86400), # 1 month
("W", 7*86400), # 1 week
("D", 86400), # 1 day
("h", 3600), # 1 hour
("m", 60), # 1 minute
@tsileo
tsileo / backup_mongodb.py
Created March 21, 2013 17:59
I made a simple Python script using [bakthat](http://docs.bakthat.io), a backup utility I created, and [sh](http://amoffat.github.com/sh/), an Python subprocess interface, that makes **backing up [MongoDB](http://www.mongodb.org/) to Amazon Glacier or S3** an easier task.
import sh
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s;%(name)s;%(levelname)s;%(message)s")
from bakthat.helper import BakHelper
# Arguments for mongodump/restore command, leave empty if you want to backup your local mongo
# if you use authentication {"u": "user", "p": "password"}
MONGODUMP_PARAMS = {"u": "user",
"p": "password",
"oplog": True}
@tsileo
tsileo / interval_string.py
Created February 22, 2013 13:34
Convert relative interval string into seconds.
import re
def _interval_string_to_seconds(interval_string):
"""Convert internal string like 1M, 1Y3M, 3W to seconds"""
interval_exc = "Bad interval format for {0}".format(interval_string)
interval_dict = {"s": 1, "m": 60, "h": 3600, "D": 86400,
"W": 7*86400, "M": 30*86400, "Y": 365*86400}
interval_regex = re.compile("^(?P<num>[0-9]+)(?P<ext>[smhDWMY])")
seconds = 0
@tsileo
tsileo / mytweets.py
Created January 25, 2013 21:08
How to retrieve your Twitter data with Python (from scripts/command line, without setting up a web server) and Twitter REST API v1.1.
# -*- encoding: utf-8 -*-
from __future__ import unicode_literals
import requests
from requests_oauthlib import OAuth1
from urlparse import parse_qs
REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token"
AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize?oauth_token="
ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token"
@tsileo
tsileo / basic_conf.py
Created December 26, 2012 16:30
get_conf
import yaml
import os
def get_conf():
config_file = os.path.dirname(__file__) + "./config.yml"
if os.path.isfile(config_file):
with open(config_file, "r") as f:
return yaml.load(f)
return {}
@tsileo
tsileo / add_months.py
Created December 26, 2012 16:29
add_months
import calendar
from datetime import date
def add_months(sourcedate,months):
month = sourcedate.month - 1 + months
year = sourcedate.year + month / 12
month = month % 12 + 1
day = min(sourcedate.day,calendar.monthrange(year,month)[1])
return date(year,month,day)
@tsileo
tsileo / load_class.py
Created December 21, 2012 17:01
how to instantiate a python class from a string using imp.
import imp
def load_class(full_class_string):
"""
Dynamically load a class from a string
>>> klass = load_class("module.submodule.ClassName")
>>> klass2 = load_class("myfile.Class2")
"""
@tsileo
tsileo / convert_bytes.js
Created December 5, 2012 15:49
Convert bytes to human readble format
var convert_bytes = function(bytes, force_unit) {
var return_val = "";
$.each(['bytes','KB','MB','GB'], function(index, unit) {
if (force_unit == unit) {
new_size = Math.round(bytes*100)/100;
return [new_size, unit];
}
if (bytes < 1024.0 && force_unit != "TB") {
return_val = [Math.round(bytes*100)/100, unit];
}
@tsileo
tsileo / flask_tornado_websocket.py
Created December 5, 2012 09:24
Flask and Tornado Websocket Together
import logging
import tornado.escape
import tornado.ioloop
import tornado.websocket
import os.path
import uuid
#class MainHandler(tornado.web.RequestHandler):
# def get(self):
# self.render("index.html", messages=ChatSocketHandler.cache)
@tsileo
tsileo / data.json
Created November 24, 2012 21:20
Line Chart with Custom Tooltip
[{"date": "Jul-2011", "value": 9}, {"date": "Aug-2011", "value": 12}, {"date": "Sep-2011", "value": 12}, {"date": "Oct-2011", "value": 4}, {"date": "Nov-2011", "value": 5}, {"date": "Dec-2011", "value": 1}, {"date": "Jan-2012", "value": 11}, {"date": "Feb-2012", "value": 7}, {"date": "Mar-2012", "value": 7}, {"date": "Apr-2012", "value": 5}, {"date": "May-2012", "value": 7}, {"date": "Jun-2012", "value": 4}, {"date": "Jul-2012", "value": 6}, {"date": "Aug-2012", "value": 1}, {"date": "Sep-2012", "value": 7}, {"date": "Oct-2012", "value": 6}, {"date": "Nov-2012", "value": 6}]